Back-up a Raspberry Pi SD card using a Mac

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:

diskutil list

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:

Diskutil on OS X

diskutil shows the Pi SD card as /dev/disk1

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

The 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: /dev/rdisk1.

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.

Finally, bs for ‘block size’ – I’ve chosen 1MB, or 1m as 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.

Now, 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 gzip utility:

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 /dev/sdx-something.

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.

About these ads

27 thoughts on “Back-up a Raspberry Pi SD card using a Mac

  1. Thanks for the well explained Mac/Pi instructions but your first line after the disk util list is incorrect and would image your 250GB internal hard drive :-(
    sudo dd if=/dev/rdisk0 of=~/Desktop/pi.img bs=1m

    should be:
    sudo dd if=/dev/rdisk1 of=~/Desktop/pi.img bs=1m

    • Well spotted. It should indeed, in this case, be rdisk1 not rdisk0, though other users may see different rdisk suffixes still. Moral: always check diskutil closely. Oh, and proof-read your blog posts.

  2. Thanks for this post, all my efforts with my naff winblows machines failed until I booted up the Macbook Pro and it succeeded first time. I can now relax a little having a good quality backup image and go back to exploring the wonders of Pi!

  3. saving the image worked great.
    sudo dd if=/dev/rdisk2 of=~/Desktop/b001.img bs=1m

    but it didnt work when i switch them
    sudo dd if=~/Desktop/b001.img of=/dev/rdisk2 bs=1m

    any ideas on why?

    • I’ll look into this, Jeremy. Did you take the card out of your computer between copy it and then trying to write the data back? If so, the second time, rdisk2 may not be correct. The number can change. Diskutil will tell you the correct rdisk value.

      • the issue was i didn’t unmount rdisk2s2. the code below fixed my issue.
        sudo diskutil unmount /dev/disk1s1

  4. One of my images is 4GB which I restored to an 8GB SDHC card. I never expanded the partitions, etc., so I’m surprised that when I clone that 8GB card, the new image is 8GB (despite the partition table showing clearly a 3-ish GB partition with empty space at the tail). Have you come across a way to clone just the actual partitions rather than everything including the blank space?

  5. Hi,

    Thanks for these instructions but I am having a problem and was hoping you may be able to help!

    I have successfully made the image using:
    sudo dd if=/dev/rdisk2 of=~/Desktop/pi.img bs=1m

    I am trying to write to a different SD card. After unmounting the original SD Card I inserted the new one which also is listed as disk2 using diskutil list in Terminal.

    I input the following:
    sudo dd if=~/Desktop/pi.img of=/dev/rdisk2 bs=1m

    I then keep getting this error message and I don’t know why: dd: /dev/rdisk2: Resource busy

    Any ideas?

    Thanks in advance!

  6. Thanks for the guide!
    So do I understand you right: I don’t need to write everything to an .img file, I can directly backup as .gz ?

  7. Just wanted to thank you for a simple, yet incredibly helpful tutorial. Saved me a ton of time getting my RasPi back up and running after an update.

  8. Thanks, helped me a lot!

    It’s worth noting, that if you want to monitor the progress of the process, you could use:
    pgrep -l ‘^dd$’ <– to find the pid of the dd process and than run:
    sudo kill -INFO PID_FROM_ABOVE <– to print the current progress
    Only tried this on my Mac until now, but should work for Linux as well.

  9. I created the .img successfully from an 8GB microSD card. I cannot write back to a new 16GB microSD card. I unmounted both /disk1 and /disk1s1 and I am getting “dd: /dev/rdisk1: Permission denied”

    Any ideas? Originally, /dev/disk1 & /dev/disk1s1 were root:operator. Even though I am running the command as root, I tried “sudo chmod root:wheel /dev/disk1″ and the same for disk1s1 and it shows as root:wheel now… but even after unmounting again, I am getting Permission denied. No, the write protect is not on for the microSD card.

    Any ideas?

  10. FIXED! WEIRD issue with MBP and its internal SD card reader. It doesn’t like some of the SanDisk cards! Found a few threads of people not being able to mount them or being mounted as read-only. Mine was being seen as read-only. The fix was to move the write protect slider on the SD / microSD adapter 1/2 way between locked and unlocked. Sounds bizarre, but it is now seen as read/write and I am currently copying the disk image back over to it.

    https://discussions.apple.com/thread/3034444?start=15&tstart=0

    • That’s a good tip. I’ve had problems with other card and card-reader combos were inserting the card catches and moves the write-protect tab, and I’ve seen it with both press-to-lock SD slots and more simple slots like the ones in the MBP and MBA.

      But your experience will be very useful for other people who encounter the same problem. Thanks for sharing your solution.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s