Clone or Backup Raspberry Pi SD Card with Mac OS X

If you need to backup or make a copy of Raspberry Pi SD Card then there are some tools available. However, this post explain how to use command line on Mac OS X in creating an image file (backup file) of an SD Card and later this image file can be used to put into another SD Card.

First of all run the terminal on your Mac and type command df -h

AnandMac-2:~ anand$ df -h
Filesystem                          Size   Used  Avail Capacity  iused   ifree %iused  Mounted on
/dev/disk0s2                       247Gi  231Gi   16Gi    94% 60530779 4093113   94%   /
devfs                              187Ki  187Ki    0Bi   100%      648       0  100%   /dev
map -hosts                           0Bi    0Bi    0Bi   100%        0       0  100%   /net
map auto_home                        0Bi    0Bi    0Bi   100%        0       0  100%   /home
localhost:/_orgYFQCOsa4qp3lbDpDV7  247Gi  247Gi    0Bi   100%        0       0  100%   /Volumes/MobileBackups
/dev/disk0s4                        51Gi   46Gi  5.0Gi    91%   254868 5354408    5%   /Volumes/BOOTCAMP

Then connect SD Card that contains data to backup to the SD Card reader. Run the command df -h again.

AnandMac-2:~ anand$ df -h
Filesystem                          Size   Used  Avail Capacity  iused   ifree %iused  Mounted on
/dev/disk0s2                       247Gi  231Gi   16Gi    94% 60530901 4092991   94%   /
devfs                              190Ki  190Ki    0Bi   100%      656       0  100%   /dev
map -hosts                           0Bi    0Bi    0Bi   100%        0       0  100%   /net
map auto_home                        0Bi    0Bi    0Bi   100%        0       0  100%   /home
localhost:/_orgYFQCOsa4qp3lbDpDV7  247Gi  247Gi    0Bi   100%        0       0  100%   /Volumes/MobileBackups
/dev/disk0s4                        51Gi   46Gi  5.0Gi    91%   254868 5354408    5%   /Volumes/BOOTCAMP
/dev/disk1s1                       7.4Gi  1.9Mi  7.4Gi     1%        0       0  100%   /Volumes/Kingston

Now compare the results of running command df -h before and after inserting the SD card and look for the new device. In my case it is /dev/disk1s1. This is the device name of the partition.

After finding the device name of the partition now we are going to determine the raw device name of the entire disk by omitting the final “s1” and replacing “disk” with “rdisk” (this is very important: you will lose all data on the hard drive on your computer if you get the wrong device name). Make sure the device name is the name of the whole SD card as described above, not just a partition of it, for example, rdisk1, not rdisk1s1. In my case it is /dev/rdisk1

Run the following command to create an image file from the SD Card

sudo dd if=/dev/rdisk1 of=/path/to/filename.img bs=1m

The above command is simply taking the whole disk (SD card) and create an image file to the location “/path/to/filename.img”. You may use any filename. Note that dd command will not feedback anything to the terminal unless there is an error or it is finished. While waiting for the dd command to finish you may see the progress by pressing Control+t

In order to create an SD card from the image file (any image file or the one you just created), determine the device name of the partition of the SD card using df -h as described above. In my case it is /dev/disk1s1. Then unmount the partition so that you will be allowed to overwrite the disk:

sudo diskutil unmount /dev/disk1s1

Then run the following command

sudo dd bs=1m if=/path/to/filename.img of=/dev/rdisk1

After the dd command finishes, eject the card:

sudo diskutil eject /dev/rdisk1