Categories
armbian Raspberry Pi Software

The fastest way to clone an SD card on macOS

If you have a raspberry pi or other single board computer and would like to make a backup of it, or even clone it to another SD card, then it can take a long time. Your first thought is to probably use the built in “Disk Utility”. Unfortunately this has issues reading linux partitions (well in my experience) and is often slow. This simple command line trick will have you copying or cloning a full disk image of your SD card in record time!

WARNING: Be very careful when running any command with sudo dd in it. If you type any of the parameters incorrectly you may accidently erase or overwrite important data.

Requirements:

  • macOS running a recent version (this guide was tested on macOS Catalina).
  • basic knowledge of command line operations.
  • Make sure you’ve got homebrew installed. You can visit this link to find out how to download and install homebrew if you haven’t already got it.
  • After you’ve installed homebrew, you’ll need to install a package called core-utils. Do so by running brew install coreutils in your terminal. It should take a few minutes to run.

Identify your sd card:

You’ll need to find out which disk your SD card represents. You can run diskutil list and should see an output like below:

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +500.0 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD โ€” Data     396.0 GB   disk1s1
   2:                APFS Volume Preboot                 81.9 MB    disk1s2
   3:                APFS Volume Recovery                528.5 MB   disk1s3
   4:                APFS Volume VM                      4.3 GB     disk1s4
   5:                APFS Volume Macintosh HD            11.0 GB    disk1s5

/dev/disk4 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *31.9 GB    disk4
   1:             Windows_FAT_32 boot                    268.4 MB   disk4s1
   2:                      Linux                         31.6 GB    disk4s2

From that output we can see that our SD card must be /dev/disk4 as our card is 32GB in size and has a fat32 and linux partition (standard for most raspberry pi images). You should add an r in front of disk4 so it looks like this /dev/rdisk4. The r means when we’re copying, it will use the “raw” disk. For an operation like this, it is much more efficient.

Copy the SD card as a disk image (dmg)

Now you should run the following command, replacing 4 with whatever number you identified as your sd card:

sudo gdd if=/dev/rdisk4 of=sd_backup.dmg status=progress bs=16M

Tip: you can experiment with different numbers for the block size by replacing bs=16M with larger or smaller numbers to see if it makes a difference to the speed. I’ve found 16M the best for my hardware.

You should see some progress feedback telling you the transfer speed. If you’d like to experiment with different block sizes, just type ctrl + c to cancel the command, then you can run it again.

Once the command has finished running, you’ll end up with a file in your home directory called sd_backup.dmg. If you’d like to backup multiple SD cards (or keep multiple backups!) simply replace sd_backup.dmg with a different file name. This will contain a complete disk image of your SD card. If you’d like to restore it, or clone it to another SD card, read on.

Copy the disk image (dmg) to your SD card

You’ll first need to unmount your SD card. Do not click the eject button in finder, but run this command, replacing 4 with whatever number you identified as your sd card sudo diskutil unmountDisk /dev/disk4.

Then to copy the image, run the following command:

sudo gdd of=/dev/rdisk4 if=sd_backup.dmg status=progress bs=16M

Tip: you can experiment with different numbers for the block size by replacing bs=16M with larger or smaller numbers to see if it makes a difference to the speed. I’ve found 16M the best for my hardware.

You should see some progress feedback telling you the transfer speed. If you’d like to experiment with different block sizes, just type ctrl + c to cancel the command, then you can run it again.

Once the command has finished running, your SD card should be an exact copy of the disk image you specified.

29 replies on “The fastest way to clone an SD card on macOS”

Mac OS X gdd -> dd : remove progress.
unmount the new new volume when writing otherwise resource busy.

Boot your Raspberry Pi using the freshly made (bigger) SD card. Install gparted, if necessary. (sudo apt install gparted) Select desired partition with free space following it. Tell gparted to resize partition. Slide the popup dialog’s slider all the way to the right. Click the check icon to get started. Get a coffee, and when you’re back, check status. Bonus points: from terminal type df -h and revel in the new free space available.

This process rescued a Raspbian image with 8 Docker containers on it that I hadn’t backed up, but suffered a failing microSD card. Thank you so much! It is backed up regularly now ๐Ÿ™‚

Greetings ๐Ÿ™‚ Trust you are keeping safe from Covid, hope so!
Thank you for straightforward instructions re cloning SD card on Mac – appreciated.

I have limited experience re using Mac CLI and wish to clone a SD card (from a Raspberry Pi) to a drive connected via USB to my Mac.

Using the Mac Disk Util i can identify which partition on the external drive I wish to use to save the back-up image but unsure of the actual command(s) to use… Tried using syntax as per Raspberry Pi CLI when copying or moving files but seems to be a difference (my ignorance actually) when using the Mac terminal, hence this note to ask for guidance.

Many thanks in advance and sorry for asking what is probably a dumb question…

Best regards

Hi Jaimyn, thanks so much for your post. I couldn’t complete the last step though, as I get the following message…

gdd: failed to open ‘/dev/rdisk1’: Resource busy

(I had just introduced a FAT-formatted SD Card in my iMac’s card slot)

Regards, REG.

You need to unmount the FAT partition via the Disk Utility tool first. Then you should be able to use the instructions to write the image.

Answering this for anyone else that comes to this post/me when I come back – you need to make sure the unmount succeeded for the SD card that you are copying to (not the one you’ve copied from), you can force it but just make sure to follow up with verifyDisk and repeat mounting/unmounting to check all peachy now before attempting to copy again.

I had to run with “bs=16m” rather than “bs=16M” otherwise I got the error:

`dd: bs: illegal numeric value`

Thanks for the guide!

This was a great tutorial and helped me copy the raspi boot image from an SD card –> home directory on my Mac. Rather than use the last instruction, though, I chose to use BalenaEtcher to copy it from the backup img to a second SD card. Was very simple, and fast, too.

Thanks! This has been super helpful. Any tips if the destination SD card is smaller than the source? There’s plenty of space to fit the files but not the clone??

After spending all night using the Mac’s own system via SSH to copy and write a 64gb card with Octopi and ending up with an SD card that didn’t work, I followed your simple and accurate tutorial which was so much faster got it all done in less than 45 minutes with a great clone.
Thank you ๐Ÿ™‚

This worked great, however I ended up with 32.1G image from a 32G SD card, so when I want to restore, it will need to live on a 64G SD card. Any tips for how to deal with this?

Is there a way to do that in one go without actually creating the disc image file? I have limited space on my mac…

Last night I was up until 4am, first trying to restore the backup I (thought) I had, then trying to use ‘ddrescue’ to restore what I could from my damaged SD card, And eventually going up and setting up a fresh OS install, and installing everything I had on it.

Clearly my previous backup solution did not work, so after I finished setting up everything I had (And had some sleep) I tested this solution to make sure what I just setup will have a proper backup. Worked as simple as it is written, thank you!

I would also suggest, for those who plan on making periodic backups to compress the dmg file: https://support.apple.com/guide/terminal/compress-and-uncompress-file-archives-apdc52250ee-4659-4751-9a3a-8b7988150530/mac

After last night I am going to keep at least 3 historical backups, done manually (thanks failed cronjob…)

Same question as Daniel. I have a dongle for my MacBook Air M2 that lets me mount both ยตSD cards at once. Is there any way to clone the working 128GB card to the new 256GB one without creating the backup image file? I only have about 100GB free on my boot drive so no room to create it.

Leave a Reply to SonnyH Cancel reply

Your email address will not be published.