Raspberry Pi Backup

As part of my development on the Raspberry Pi it dawned on me that it may be a good idea to automatically backup any scripts that I write. Most of these will initially be test scripts, but still worth keeping.

Having recently got the NAS back up and running this seemed to be the logical location to store any backups. Unfortunately, this only supports FTP, rather than SFTP. As all the backups are only going to be over the LAN, this shoudn’t be too much of an issue.

The script was created and added to a Cron job that runs at 2am every morning. To save storing a backup when nothing has changed I added a quick MD5 hash check to see if anything has changed in the directory we are backing up. As well as the scripts I have also taken a copy of the list of installed packages. This should help if I ever need to rebuild the Pi.

The final script is shown here:

#!/bin/bash
# Script to backup ~/rpi directory
#
# create package list
dpkg --get-selections | grep -v deinstall > /home/pi/rpi/package-list

# take a copy of this script
cp /home/pi/rpi-backup.sh /home/pi/rpi/rpi-backup.sh

# create tar file
_now=$(date +"%Y%m%d")
_file="$_now-rpi.tgz"
tar -cvzf /home/pi/$_file /home/pi/rpi/

# check if the same as the last upload
_md5last=$( last.md5
fi

# remove temp files
rm /home/pi/$_file

I’ve now decided that I may also need a full backup of the SD card that the Pi runs on. This will be taken once a month, but I’m currently looking at options for this.

Leave a Reply

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

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