Scheduled backups for Raspberry Pi to a NAS

Because I have a few Raspberry Pi’s at my place, and I may get more in the future, and because I have a Network-Attached Storage (NAS) device, I figured I really should get around to backing up my Pi’s to prevent them from dying (as SD cards are apparently prone to do) and taking my data / configuration with them. To this end, I developed a really, really simple process that automates this and gives me a collection of gzipped SD card images, ready to recreate the state of my Pi. I saw on Twitter that there was some interest in this, so here is what I did.

If there are any questions feel free to post a comment – I’ll update this blog post to ensure it is as detailed as necessary.

Finally, I should note that this approach is based on a blog post by Martin O’Hanlon. The only real difference is that I have the Pi’s gzip the image file prior to sending across the network, and this substantially reduces the amount of space taken up by the images (especially if your Pi is running with a big SD card (e.g. 16GB)). This does of course increase the amount of time it takes to do the backup, and does require more CPU effort, so it is up to you whether you value speed or hard disk space more.

Steps to back up your Raspberry Pi to a home NAS device

1) Mount your NAS
I created a directory /home/pi/nas/backups. This directory will map to a ‘backups’ directory on my NAS. With this directory created I run sudo nano /etc/fstab to edit the Raspberry Pi file system list. I added the following line so that my NAS was automatically mounted at startup:

[code]//diskstation/backups /home/pi/nas/backups cifs username=pi,password=raspberry,workgroup=workgroup,users,auto,user_xattr 0 0[/code]

Note that this works because my home NAS has the name ‘diskstation’, and I’ve created a limited user account on there with username/password combination pi/raspberry (note that this isn’t the real username/password combination – please don’t try to hack my house) 🙂

2) Create backup script
I created a really simple backup script. This script is located on the NAS in the backups directory. This means that it is accessible on the Pi via /home/pi/nas/backups, but my recommendation is to copy the file onto the Pi itself (on my Pi’s I copy it into /home/pi). I call this file ‘runSDCardBackup.sh’, and you’ll note it expects one argument, which is the name of the file it will write to. Here’s the entirety of the script:
[code]
#!/bin/sh
# Script to backup the SD card of a Pi to the nas.
# This should be copied to the /home/pi directory on each Pi and then
# set in the crontab to run automatically on a set schedule.
# Be sure to pass in a single argument to name the backup file
# (e.g. homePiBackup or mediaPiBackup)
#
# Refer here for the original information:
# http://www.stuffaboutcode.com/2012/08/raspberry-pi-auto-backups.html
sudo bash -c "dd if=/dev/mmcblk0 | sudo gzip > /home/pi/nas/backups/$1.gz"
[/code]

You may need to set this shell script to be executable. To do this, simply type the following: sudo chmod +x runSDCardBackup.sh

3) Set up the script to run on a schedule
Run sudo crontab -e to edit the crontab file. I set the script to run at 2:30am every Monday morning. Because I have multiple Pis at my house, I set the time to be different for each Pi so that the network doesn’t get flooded with SD card images flying over it. Here’s the line I added to the crontab file:

[code]30 2  *   *   1     /home/pi/runSDCardBackup.sh homePiBackup > /home/pi/nas/backups/homePiBackup.log 2>&1[/code]

You’ll note that I pass in ‘homePiBackup’ to the shell script – this is the name that the file will be given on the NAS. On each Pi I obviously use a different name – in my house they have names like homePi (for home automation), mediaPi (for home theatre), etc. You’ll also note that I’m sending the output to a log file on the NAS – again note that you’ll want a different file name for each Pi.

4 thoughts on “Scheduled backups for Raspberry Pi to a NAS”

    1. Very simple:

      1) Because the file being created is a gzipped file, you firstly need to unzip it. You can do this with gzip -d filename.gz

      2) You’ll end up with a .img file, and then you can follow the usual instructions for writing to an SD card. The Raspberry Pi website has useful instructions here (look under ‘Raw Images’).

      3) Put the SD card into the Pi and start it up.

      For future reference, here is the text from the Raspberry Pi website:

      To use an image file, you will need to unzip it and write it to a suitable (2GB or larger) SD card using the UNIX tool dd. Windows users should use Win32DiskImager. Do not try to drag and drop or otherwise copy over the image without using dd or Win32DiskImager – it won’t work. If you’re still not clear on what to do, the community on the Raspberry Pi Wiki has written a guide for beginners on how to set up your SD card.

  1. Don’t you run the risk of getting a corrupted image if a process on the Pi writes to the card while dd is reading it? You’re reading whatever bytes happen to be in the block whenever dd gets to them. Maybe not an issue with sd cards.

Leave a Reply