I’ve re-installed my Pi’s SD card storage more times than I care to recall. New cards, programming glitches, messing with Linux’s settings files – all of these reasons have forced me to go through the process of re-flashing the Pi’s storage card. That’s bad enough – what’s worse is having to re-download the applications I’d added since the previous install, applying updates and choosing again all of my system preferences.
What I should have done was clone my card, allowing me to re-flash the SD card with a complete, working and configured to my personal satisfaction Pi operating system.
I don’t know why I didn’t do this. I suppose Mac OS X’s inability to cope with the Pi’s EXT4 file-system was the main reason: I can’t simply drag and drop the card’s contents, first to the Mac and then, later, back to a freshly formatted card. But a little Googling set me on track to use the next best thing: the
dd command, available in OS X – and Linux for that matter.
So clone the Pi’s SD card, slip it out the Pi and into a Mac. Open the Mac’s Terminal app and first enter the command:
This will give you a list of the disks and volumes inside or connected to your computer. The Pi SD card will contain a Linux partition under TYPE NAME:
Now you’re ready to duplicate the SD card, saving it as a disk image file on your hard drive:
sudo dd if=/dev/rdisk1 of=~/Desktop/pi.img bs=1m
if stands for ‘input file’, in this case the SD card.
diskutil will list it as
/dev/disk1 – prefix ‘disk’ with ‘r’ to point to the card’s raw storage space to speed up the process:
In the next section,
of stands for ‘output file’, here a new file,
pi.img which is to be place by
dd on my desktop. Remember, ~ is the Unix symbol that represents your home directory.
bs for ‘block size’ – I’ve chosen 1MB, or
dd prefers it. You’ll need to prefix the
dd command with the
sudo command, and this will first ask your for your Mac admin password. If you’re a Pi user, you’ll likely be used to
sudo. When you’ve entered your password, the duplication process will begin. Depending on the capacity of your SD card, it could take some time. While the back-up is being made, all you’ll see in the Terminal is a flashing cursor, but you can get some feedback by looking at the size of the
pi.img file on your Desktop.
WARNING Always make sure you check these values carefully – get them wrong and you can wreck the data on your hard drive. Don’t, for instance, key in the wrong
/dev/disk entry from the
diskutil list. If you accidentally tell
dd to copy the wrong disk or copy the SD to your hard drive, it will merrily go ahead and do so. It won’t check to see if you’re happy for it to proceed. Don’t copy my examples exactly – the input and output file locations may be different on your system.
Re-flashing the SD card from the image is just a matter of swapping around the input file and output file values to read from your
.img file and write to the card.
pi.img in the example above is large. You can compress it afterwards by right-clicking and selecting
Compress “pi.img”, but it’s more efficient to compress the file as you go. To do this, you use the Unix ‘pipe’ symbol, | , to route the output of
dd not to a file but to the
sudo dd if=/dev/rdisk1 bs=1m | gzip > ~/Desktop/pi.gz
Re-flashing the SD card then becomes:
gzip -dc ~/Desktop/pi.gz | sudo dd of=/dev/rdisk1 bs=1m
Update Note that you’ll need to unmount – but not eject – your SD card before you can write to it. So enter
diskutil unmountDisk /dev/disk1 to do so. Now you’re ready to run the restoration using the command sequence in the previous paragraph.
I got my 16GB SD card down to a 1.48GB
.gz file, small enough to keep safe on a USB stick I had kicking around. Depending on how big your SD card is and how much data you’ve put on it, your mileage may vary.
PS. If you’re reading this but you’re not a Mac user, Lifehacker has instructions for Windows. Backing up the Pi SD card on a Linux machine is an almost identical process to the Mac one. The difference: the SD card will be listed as
PPS. If you’re reading this but you’re not a Raspberry Pi users, what are you waiting for? Go and buy one! Here’s a nice set comprising the Pi and a stack of handy accessories.