Automatic Raspberry Pi Backups

You've put a lot of work into configuring a Pi, your services are running and it's all working perfectly and now you want to make sure that you don't have to repeat  all that work should it ever break.

Now we can use dd to create an image of the card, giving us a snapshot of the system that we can restore in the event of a breakage.

You can find my Github repo for this project here:
https://github.com/AdamGalt/Auto-Pi-Backup

This contains the script we are going to need to make the magic happen.

First we need to download the pibackup.sh file:

wget https://github.com/AdamGalt/Auto-Pi-Backup/raw/master/pibackup.sh

Then make it executable:

sudo chmod +x pibackup.sh

---- Optional ----

Move the file to your scripts folder, an attached drive or network share you are using for your backups.

mv pibackup.sh /media/storage/location/pibackup.sh

I prefer to use an NFS share, as I have quite a few Pi's to backup, but you can use whatever you prefer here.


Now edit the file and replace the values of:

"backuppath=" and "daystokeep="

with your own location and days you wish to retain the backup images for.

nano pibackup.sh

Then save and exit your editor.

You can now run the script using:

sudo ./pibackup.sh

Or, if you moved it:

sudo ./media/storage/location/pibackup.sh

Just replace the location path with your own.

The backup will take a long time to run and use as much room as the maximum capacity of the card. I tend to use 32GB class 10 cards, which take about 30 minutes to complete over the network.

Now we can automate it


Once you have confirmed that it is working as expected you can automate this process using cron!

Edit the crontab for root:

sudo crontab -e

and add the entry:

0 4 * * 0 /media/storage/location/pibackup.sh

This will run the backup at 0400hrs every Sunday.

Now you can set the backups to start when you are asleep and not using the devices.

If you are not familiar with cron then https://crontab.guru is a great site to help you work out how to set the values you want to use.