Small (Mac OS) Script to Backup Raspberry Pi

Posted by & filed under , .

Raspi_Colour_RI’m a big fan of Raspberry PI — to the point where I have actually 2 of these little rascals. (I actually use one of them occasionally to run bits of development stuff on it — test things like nodejs and groovy scripts etc; and the other one I sort of keep for experimental PI projects, trying to figure out the intensity of the light outside, whether it’s going to rain and all sorts of useless things 🙂 ) Anyway, I find myself for simplicity I try to keep the 2 of them in sync so to speak — in other words whatever packages Raspberry PI 1 has, I try to keep on Raspberry PI 2, such that I can interchange them easily. (That in itself makes using them so much easier!)

To keep them in sync I normally take an image of the SD Card from one of them on my Mac, then install the image on the other SD Card — which then goes into the other Raspberry PI. This way they both run with exactly the same config and also I get to make a backup of the SD card, which occasionally has been known to be temperamental. (Nothing worse than the card getting corrupted without a backup so you have to re-set it all up 🙁 )

Trouble is that every time I take an image backup (which is not that often, having reached a certain “maturity” level with the packages on my PI’s, I don’t change configuration that often or install other packages) I have to scratch my memory a bit on how to run a dd command in the Mac terminal to take the image dump.

(To those of you dd guru’s who will throw it in my face that I’m such an idiot because dd is such an easy command, good for you that you’re an expert and you memorized all the options for this tool, I prefer to save my brain space for more useful things, when these options can be found googling around very easily!)

As such, I’ve put together a bash script to do this easily: on my Mac, the flash drive is always disk2 so I’ve hardcoded that in (feel free to change it, that’s why I made the script print out that and wait for input so you don’t find yourself taking a dump of a network drive). Perhaps there is a more elegant way of detecting which drive is the flash drive and using that, but I don’t want to complicate this too much, so I’ve decided to leave it like that for now. The script simply generates a date/time stamp in the format YYYY-MM-DD-hhmm (MM=month and mm=minute by the way) and then invokes dd. Note that to call dd you need root privileges so the script sudo‘s it automatically — in MacOS this will prompt for your root password.

Also, I’ve added an extra command line option: gzip — if you call gzip , the script will create the image file as per usual and then at the end will gzip this image file. This is because typically out of the 16Gb worth of image file, only about 2Gb actually contain data, the rest of the partition is empty — as such, zipping this up can drastically reduce the file size.

Finally, the script source below — not much to it as you can see, but might come in handy:

echo "Assuming using disk2"
diskutil list
echo -n "Press ENTER to continue ... "
read -e OK
TODAY=`date +'%Y-%m-%d-%H%M'`
echo "Writing image to $FILE_NAME"
sudo dd if=/dev/rdisk2 of=$FILE_NAME bs=1m
if [ "$GZIP" == "gzip" ]
   echo "Gzipping $FILE_NAME"
   gzip $FILE_NAME

Good luck with the Pi!

2 Responses to “Small (Mac OS) Script to Backup Raspberry Pi”

  1. Sean

    you should host this on github

  2. Liv

    Oh it’s too small of a script for this 🙂