Automatically backup a running Raspberry Pi

For a long time I fought with the problems of Raspberry Pi’s and their ability to destroy the data on the SD card if abruptly powered down. Usually I sought to do this by making backup copies of the SD card data but, as we all know, when things get changed/added/deleted things can change and take time to put right and that all important manual backup never quite gets done until it is too late. As it turns out there is a brilliant solution to automatically backup a running Raspberry Pi to network attached storage that really does work!

Even with the advent of the Raspberry Pi 5 and NVMe storage increasing reliability a good backup strategy is invaluable. And besides. I still find a use for all those old Raspberry Pi 2’s and 3’s that were sitting in the drawer gathering dust.

With 7 Raspberry Pi’s running full time monitoring my internet connection, UPS’s and other activities I decided it was finally time to sort this out once and for all. After a bit of searching the internet I stumbled across a great page from spookyghost.

In my case I am not using a TrueNAS but will be using a Netgear ReadyNAS instead. As the ReadyNAS is on my internal home network and I have a dedicated NFS share setup for backup images I simply allowed Everyone READ/WRITE access at the File Access level. At the Network Access level I specified the IP address of the Raspberry pi and Set READ/WRITE and ROOT ACCESS to true for each device I was going to backup to this share.

For Snapshots I use Smart Weekly snapshots so these are taken at midnight on Friday night and kept for 8 weeks. More than enough for my purposes and a good compromise on storage size, however, this can be customised and changed as required.

Next I created a directory on the ReadyNAS share for each of the Raspberry Pi’s I intended to backup.

The remainder is based upon the article by spookyghost with a few minor tweaks I found necessary to make things work for my setup.

Firstly create the mount point for the directory we are going to mount the ReadyNAS directory we are going to store the backups on

sudo mkdir /mnt/pibk
sudo chown -R myusername:myusername /media/pibk
sudo chmod 777 /media/pibk

Next we need to update fstab by adding the following:

<IP_address of ReadyNAS>:data//Disk_Images /mnt/pibk/ nfs rw,nouser  0 0

Disk_Images is the name of the volume I am using to store the backup images in and each backup will be stored in a separate directory on this volume.

Now type from the command line

sudo mount -a

This should mount the drive and you should be able to change into /mnt/pibk and create directories and files here. If you cant you need to fix this issue before moving on.

At this point we are pretty much following the instructions from here. I followed these instructions exactly for the initial install, however, the version of image-files.zip stored there is not the latest. This version worked perfectly for me until I replaced my SD card with an NVMe drive and then the backup would fail. Switching to the latest version of image-utils.zip here resolved all my issues and I would recommend using this version. Locate the first post in this thread and download the latest file attached to this post. Be warned there appear to be numerous github repositories of this software but the latest version is maintained on this thread.

Copy image-utils.zip into your home directory then:

mkdir -p ~/image-files && mv ~/image-utils.zip ~/image-files && cd ~/image-files && sudo apt install unzip && unzip ~/image-files/image-utils.zip && rm ~/image-files/image-utils.zip
sudo chown root:root ~/image-files/image* && sudo chmod +x ~/image-files/image*
sudo mkdir -p /usr/local/bin/ && sudo mv ~/image-files/* /usr/local/bin/ && sudo mv /usr/local/bin/README.txt /usr/local/bin/image-readme.txt

Now launch the application to install gdisk

sudo image-backup

Prewss “y” if prompted to install gdisk and when you are prompted for the Image File to Create? press CTRL-C to abort

Now we can run image-utils to create our initial backup. If you are using subdirectories like I am then replace <pi_subdirectory> with the correct sub-directory.

sudo image-backup --initial /mnt/pibk/<pi_subdirectory>/backup.img,,5000

This may take some time and once it is complete you should have a backup file named backup.img in the appropriate directory on your NAS volume.

Now we can edit crontab to schedule the regular weekly backups. Be sure to run this under sudo

sudo crontab -e

And add the following to your crontab:

# Incremental Raspberry Pi for every Friday at 4am
50 3 * * 5 /usr/bin/mount -a
0 4 * * 5 /usr/local/bin/image-backup /mnt/pibk/<pi_subdirectory>/backup.img

I added two entries to my crontab.
1. At 3:50am I run a mount command to ensure that the NFS share is mounted
2. At 4:00am I run the incremental backup. Again be sure to set <pi_subdirectory> as appropriate

Thats it! now you should be able to use the backup.img file to write your image to an SD card/NVMe card using your favourite image writing software, put it in your Raspberry Pi and it should boot from the restored image

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top