Hackviking He killed Chuck Norris, he ruled dancing so he took up a new hobby…


Automated Raspberry Pi Backup – complete image

I love my Raspberry Pi projects and I run a lot of specialist "mini" servers at home doing everything from torrent sharing of Linux distros to media streaming and media playing. But all Raspberry Pi's and other single board computers that rely on SD-cards sooner or later comes to a point where they trash the card and doesn't boot again.

Every time I run into that situation without remembering exactly what was running and how on the particular Raspberry Pi. I want backups, not just the backup I usually do right after installation but a last night backup or similar. So I put up an NFS share on my NAS to store the backups, it will work just as well with a USB stick connected directly to the Raspberry Pi. Here is a step by step guide how I automated the backups on all my Raspberry Pi's. This script will create a complete image of the SD-card while the Raspberry Pi is running. You can just write that image to a new SD-card and pop it into the Pi and it will be like nothing happened!

  1. Create the folder /mnt/backup
  2. Edit fstab with
    sudo /etc/fstab

    Add the share on the NAS for mounting /mnt/backup nfs rsize=8192,wsize=8192,timo=14,intr 0 0
  3. Test the mount
    sudo mount -a
  4. Download my backup script from Github
    wget https://github.com/kallsbo/BASH-RaspberryPI-System-Backup/raw/master/system_backup.sh

    I placed the script on my NAS share so all the Raspberry Pi's could use the same script file.

  5. Make the script file executable
    chmod +x system_backup.sh
  6. Add it to crontab. Since the script needs to be executed as root we need to edit the root crontab.
    sudo crontab -e

    Just add the line (with correction for the path to the script)

    0 3 * * * /mnt/backup/system_backup.sh

The script is a simple bash script that you can look at and download from Github - BASH-RaspberryPI-System-Backup. The script has two variables, backup_path and rentention_days. The first is the path where to store the backup and the second is the number of days to save it. They can have defaults set in the script but you can override them by supplying command line arguments like:

sudo ./system_baskup.sh /mnt/otherpath 7

That will give you the backup path of /mnt/otherpath and a retention of 7 days. The next few lines (until line 38) just checks if it's running as root and checks for command line arguments. Then we touch the file /boot/forcefsck this creates a blank file at that path and forces the Raspberry Pi to check the filesystem on the next boot. This is so the backup image, if used, will be checked for filesystem inconsistencies on first boot.

Then comes the magic sauce, creating a complete copy of the SD-card:

dd if=/dev/mmcblk0 of=$backup_path/$HOSTNAME.$(date +%Y%m%d).img bs=1M

Then the script removes the /boot/forcefsck file so the force file is only in the backup image and not on the currently running SD-card. Then it looks for backups older then the number of retention days and deletes them. Since the script includes the hostname in the filename and only looks for it's own hostname you can have several Raspberry Pi's backing up to the same location without being worried about them deleting each others backups.

Posted by Kristofer Källsbo

Comments (13) Trackbacks (1)
  1. Hi, thanks for this. i ran it from the command line and i get:

    pi@pi:~ $ dd: failed to open ‘/myNAS/backup/pi.20180406.img’: No such file or directory
    find: `/myNAS/backup/pi.*.img’: No such file or directory

    it’s the first time it’s run so nothing will be there. it then appears to hang. any ideas or is it actually compiling the backup in the background (ileft it for hours and nothing happened). Cheers, Don

  2. Make sure that the folder exists and that you can write to it. It has to be mounted. Just try writing a text file from nano in the folder to check…

  3. thanks for the speedy reply. i can create files on the NAS from the Pi. It is mounted OK. i don’t know what the problem might be. cheers, Don

  4. Are you running the script with the right credentials? Check message log for errors

  5. Thanks for a solution, I will use it. A lot of typos in the text
    timo=14 (timeo=14)
    sudo ./system_baskup.sh /mnt/otherpath 7 (backup.sh)

  6. Thanks for this! I’m testing it out now and it seems it is going to backup the whole 64GB sd card.. so far its backed up about 12GB and my total used space on the card is about 4.9GB. Is this correct that it will backup the empty space as well? If so, is there a way to reduce that?

  7. Yes the whole SD card will be imaged. There is no way for the dd command to determine what is empty space and not. I did experiment with some empty space removal of the image it self after backup but ran into inconsistent behavior so I dropped it.

  8. Hi,
    Is this process of backup doable also for running applications like WordPress which using MariadDB for database. I mean is it possible to create proper image which could be also properly restored while the Raspberry Pi is running the whole time?

  9. Haven’t tried with that specific combo of software but the backup should be consistent. I only tried it with a MySQL used by my Kodi boxes and it worked just fine. I recommend you try it out.

  10. Thank you for this, Kristofer.

  11. Thanks again for this work, Kristofer. The script worked fine to create the image on an NFS mount (NFS share from a rasPi NAS server with a hard drive). Then I split the file and cp them to my mac laptop from the NFS server. Then I cat files.* > original.img and burn the SD. Boots fine, great! I think the full dd of the SD in the rasPi probably works it more than my regular production does (rasPi email server). So, I have the script in /etc/cron.weekly and then I do file backups of the email directories more often.

    (/usr/bin/split –bytes=1G mail2.YYYYMMDD.img mail2.YYYYMMDD.split)&
    then, from the NAS server I copy all the files to my laptop where I do
    cat mail2.YYYYMMDD.split* > mail2.YYYYMMDD.img and burn the new SD.

  12. Hi Kristofer,

    Great solution. Couple of points:

    1. You still have a typo in your NFS mount options (timeo, not timo)
    2. You can make the backup much smaller and faster if you pipe through gzip as you go. (Gzip buffers writes, whereas dd does not)

    # Perform backup and pipe through gzip to improve speed/size
    dd if=/dev/mmcblk0 bs=1M | /bin/gzip > $backup_path/$HOSTNAME.$(date +%Y%m%d).img.gz

    Rock on!

  13. This is in my fstab: /mnt/backup nfs rsize=8192,wsize=8192,timo=14,intr 0 0

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.