What is the best way to clone a disk between two Macs?

What is the best way to clone a disk between two Macs? I ask this every couple years or so and every time I get the same answer. "Use Carbon Copy Cloner", they say. But the unfortunate fact is that CCC is a file-level copy between disks. When I migrate to my new Mac, sure all the files are there but there are quirks here and there (including file dates being different etc).

So, really now, once again: how do you clone the disk over byte-for-byte?


Solution 1:

You can use the dd command to make a bit-perfect clone of a drive. It's a command line tool that ships with OS X. In order to make the clone perfect you'll need to ensure the source and the destination aren't actively in use.

To prepare for the clone I recommend creating a secondary boot disk that you can boot from. Your source for the clone should be an offline volume, not in use, when you're making the copy. Otherwise you risk copying things that are in incomplete states on disk.

With your machine booted to your secondary boot disk, log in and fire up a Terminal or iTerm window.

Run diskutil to get a list of your available drives. One of them will be your target drive you're trying to clone. The other will be your source drive. For example:

> diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *320.1 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS Macintosh HD            319.2 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3       
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk1
   1:                        EFI                         209.7 MB   disk1s1
   2:                  Apple_HFS Backup                  499.8 GB   disk1s2
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.1 GB   disk2
   1:                        EFI                         209.7 MB   disk2s1
   2:                  Apple_HFS Clone                   499.8 GB   disk2s2

Let's say that Macintosh HD (disk0) is the source and Clone (disk2) is the target for our dd operation. Start the clone with:

> sudo dd if=/dev/rdisk0 of=/dev/rdisk2 bs=1m conv=noerror,sync

When dd finishes you may see an error like this:

dd: /dev/rdisk2: short write on character device
dd: /dev/rdisk2: Input/output error
3726+1 records in
3726+1 records out
500107862016 bytes transferred in 14584.393113 secs (34290619 bytes/sec)

That last error message is actually okay. The last block written was a short block because there wasn't a full 1MB block to copy. No worries.

Now you've got a bit-wise perfect clone of your Macintosh HD drive. Reboot your system using the Macintosh HD drive and enjoy your clone! And when we say bit-wise perfect we mean it. The disk structure is copied block-by-block so this dd approach works to copy data from a disk that uses a partitioning scheme that macOS doesn't natively support.

Solution 2:

Apple's bespoke command line utility to do disk cloning is asr.

It is tailored to the specifics of OS X needs to perform file by file as well as block based imaging and deals with differences in partition sizes, allows network streaming (and even multicast streaming) as well as copying between disks that are locally connected. Unlike dd, it knows about Apple's latest Core Storage volume management and is the program that the graphical Disk Utility calls to move data from one partition or volume to another.

You can read more at the manual page for asr.

Solution 3:

Disk Utility can do volume-to-volume cloning with the Restore tab. Between two Mac OS Extended volumes, this'll do a block copy, i.e. it just copies the volume structures, so all the files come out identical (down to the file ID numbers). This is essentially the same thing dd does, except that Disk Utility can expand/contract the volume if the destination isn't exactly the same size as the source, and it's a lot faster (for some reason, dd is quite slow on OS X).

EDIT: After seeing @Ian's note about speed using /dev/rdiskN vs. /dev/diskN, I ran some quick&dirty benchmarks copying between two 4GB flash drives:

dd using /dev/diskN: 2737 seconds
dd using /dev/rdiskN: 907 seconds
Disk Util, full volume: 840 seconds to copy + 213 seconds to verify
Disk Util, empty volume: 4 seconds to copy + 1 second to verify

So it looks like the rdisk suggestion makes dd run about the same speed as Disk Utility; the real differences are that Disk Utility verifies its data (slower, but maybe safer) and skips blank space (faster if the disk isn't nearly full). That, and as I said above DU can resize as it copies.

Solution 4:

Verbose output from dd via pv

enter image description here

Copying even a small disk can take a long time and the silence can be frustrating. If you install pv (pipe viewer) you can use it to monitor the progress of any stream.

If you are using homebrew (and you should be) installing pv is as easy as:

brew install pv

Then decide which disks you want to copy.

diskutil list
/dev/disk0 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *251.0 GB   disk0
   1:                        EFI EFI                     209.7 MB   disk0s1
   2:          Apple_CoreStorage SSD                     250.1 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3

/dev/disk1 (internal, virtual):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:                            SSD                    +249.8 GB   disk1
                                 Logical Volume on disk0s2
                                 2CFBB247-D59D-474F-8467-2B1BDB275524
                                 Unencrypted

/dev/disk2 (internal, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *15.6 GB    disk2
   1:             Windows_FAT_32 boot                    43.0 MB    disk2s1
   2:                      Linux                         15.5 GB    disk2s2

/dev/disk3 (external, physical):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *15.6 GB    disk3
   1:             Windows_FAT_32 NO NAME                 15.6 GB    disk3s1

In this case /dev/disk2 is an Micro SD card from my Raspberry Pi in the internal reader and /dev/disk3 is a new MicroSD in a USB dongle.

The command to copy it is going to be very similar to common dd commands except we are going to take advantage of the fact that of (output file) defaults to STDOUT and if (input file) defaults to STDIN. This allows you to use unix pipes in between. The dd function listens for control-t key and will update you on progress when it is asked to report while running.

# I'm going to define variables to make your copy-paste easier
SRC=/dev/rdisk2
DST=/dev/rdisk3

# This is the command you want
sudo dd bs=1m if=$SRC | pv | sudo dd bs=1m of=$DST

# Notice how similar this is to doing a simple
sudo dd bs=1m if=$SRC |      sudo dd bs=1m of=$DST

# ...which is functionally identical to
sudo dd bs=1m if=$SRC                      of=$DST

# (except 2 processes vs. 1)

The actual output looks like:

dd bs=1m if=/dev/zero count=16000 | pv | dd bs=1m of=/dev/null
16GiB 0:00:06 [2.46GiB/s] [       <=>                                          ]