Backing Up a Raspberry Pi Image to a Compressed Format on a Mac

Quick steps for backing up and zipping your Raspberry Pi image onto a Mac OS. There is a TL;DR at the bottom if you’re just looking for the commands.

Having spent considerable time setting up and installing new software on my raspberry pi, I thought it would be nice to save the image before I did something stupid. Three hours later that short time investment paid dividends when I essentially bricked it by writing over the network interface file. The following steps will hopefully save you some pain.

Listing All Devices #

Before putting your SD card in, list all current devices with:

diskutil list

Now put the SD card and run diskutil list again. Look for the new device.

In my case:

Screen Shot 2015-07-24 at 1.07.13 PM.png

Take note of the device name (in my case /dev/disk3). Be extremely careful to note the right device. One of those other devices in the list is your hard drive, you do not want to be messing with that. There is no going back if you start screwing that up and your computer will not hesitate to do that if you tell it to.

Next we want to find the raw disk name. Doing that is simple: add an ‘r’ in front of ‘disk’ in the device name. (in my case /dev/disk3 becomes /dev/rdisk3)

Backing up to a .gz #

Back up the image to a zip file on your computer by piping the output of dd to gzip:

sudo dd if=your-raw-device-name bs=1m | gzip > ~/pi_backup_image.gz

In my case (DO NOT COPY):
sudo dd if=/dev/rdisk3 bs=1m | gzip > ~/pi_backup_image.gz

This might take some time and unfortunately you will not get any console output until the command has completed. If you want to check your progress though, you can use Ctrl+T to log it to the console.

In my case, the disk was 64 GB, the compressed output was 2.2GB and it took about 70 minutes to complete.

Flashing the .gz back to your disk #

Inevitably you will screw something up on your raspberry pi (in my case I overwrote the network interface file and rebooted while my only ability to control it was over ssh on the LAN. Oops…) Now you’re glad you made the backup.

Same as above, insert the SD card and list all devices:

diskutil list

After locating the raspberry pi device, again with extreme care (see above) that you are looking at the correct one, unmount the disk with:

diskutil umountDisk your-disk-name

In my case (DO NOT COPY):
diskutil umountDisk /dev/disk3

As when we were backing up the disk, to find the raw device name, add ‘r’ in front of disk (in my case, /dev/disk3 becomes /dev/rdisk3). To unzip and flash the image back onto your SD card, run:

gzip -dc ~/pi_backup_image.gz | sudo dd of=your-raw-device-name bs=1m

In my case (DO NOT COPY):
gzip -dc ~/pi_backup_image.gz | sudo dd of=/dev/rdisk3 bs=1m

Again, there will be no console output until everything has completed. Use Ctrl+T to log the progress to the console.


TL;DR #

If you know what you’re doing, this is all you need to know.

Backup

sudo dd if=your-raw-device-name bs=1m | gzip > ~/pi_backup_image.gz

Rewriting

diskutil unmountDisk your-device-name

gzip -dc ~/pi_backup_image.gz | sudo dd of=your-raw-device-name bs=1m
 
3
Kudos
 
3
Kudos

Now read this

American losers

Money in politics translates to a lower standard of living for the vast majority of Americans. Our Internet Is Slow In the 2014 election alone, the Telecom Five bribed the Senate Subcommittee on Communications, Technology, and Internet... Continue →