Learning to Clone A Raspberry Pi’s SD Card (on command line of course)

I wanted to be able to clone a Raspberry Pi’s SD card to having some state that I could go back to if I had messed up to much in this Raspberry Pi’s OS.

As I’m into learning as much command line stuff as possible this is an command line only approach using a Mac (MacOS 10.14.4).

Clone An SD Card On Command Line

Connect the SD card to the Mac, open Terminal and enter the following command to locate it:

This will give you a list something like this:

So here the SD card would be /dev/disk2. I can tell that not only by the size of it.

Create a disk image by the command on your Desktop (or elsewhere If you feel like):

Enter your Admin Password to continue.

Go and have coffee, several cups… or pots…

This may take quite some time depending on your SD card’s size and the data already existing on it (up to more than an hour).

No progress is shown in Terminal. Yet once completed you’ll be told so.

Restore A Disk Image To An SD Card On Command Line

Connect an SD Card (can be blank) to the Mac.

Locate it by the diskutil command as before:

Again this might look something like this:

The SD card is /dev/disk2.

Unmount the SD card by entering:

Format the SD card to a FAT16 or FAT32 file system like this:

Find more detailed infos on formatting from command line here: Learning to Format a SD Card as FAT32 Using Mac Command Line

If, like in my case the disk image is stored on an external hdd, find out the mounting point of an external HDD‘s partition use diskutil list command again. Check the IDENTIFIER of that partition (disk2s3 e.g.) and use another diskutil command as:

This will give you a lot of info about that device like the Mount Point’s path we are looking for:

Restore from a disk image that you made earlier and that (in my case is located on an external HDD) by:

Using rdisk2 instead of disk2 speeds things up tremendously (in my case something like 15 times faster).
Note: Do not use the “r” for cards where you want to keep several partitions. All data on that physical drive will be erased! It’s not working for partitions!

Enter your Admin Password to continue.

If cloning took long – this will take even longer. Time to go to bed and come back the next day…

Ressources I’ve been using to learn this:

https://computers.tutsplus.com/articles/how-to-clone-raspberry-pi-sd-cards-using-the-command-line-in-os-x–mac-59911

https://superuser.com/questions/429058/how-can-i-get-the-mount-path-of-a-usb-device-on-osx

https://www.raspberrypi.org/documentation/installation/installing-images/mac.md

https://serenity.ee/post/82120938429/mac-os-dd-with-devdisk-vs-devrdisk

https://apple.stackexchange.com/questions/234167/how-can-i-track-progress-of-dd

https://en.wikipedia.org/wiki/Dd_(Unix) – Note that not all functions of the program dd are available on MacOS!

Leave a Reply

Your email address will not be published. Required fields are marked *

nine + fifteen =