Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Last active February 26, 2019 19:45
Show Gist options
  • Save pepasflo/c0cd0858d1b76db6d057582e6ac430b8 to your computer and use it in GitHub Desktop.
Save pepasflo/c0cd0858d1b76db6d057582e6ac430b8 to your computer and use it in GitHub Desktop.
Notes on creating a disk backup of a Mac using dd

Note: this is just a draft -- the last time I followed this process was a few years ago, so don't use this guide until I actually give it a test run.

Create a "disaster recovery" backup of your Mac by making an exact copy of the disk

In addition to using something like Time Machine to backup your Mac, I also recommend creating a "disaster recovery" backup by making an exact byte-for-byte copy of the hard drive itself. This is a universal method which can be used with any kind of computer (not just Macs).

If the hard drive contents are changing while backup is in-progress, this can result in a corrupted backup. This means you can't be booted from the hard drive which you are trying to backup. In the old days, this was easy: simply remove the hard drive from the machine, plug it into another machine, and image the drive. That's a bit of a pain with modern Mac laptops.

An alternative to removing the hard drive is to boot the mac off of a USB flash drive.

Create a bootable flash drive

A universal way of doing this would be to install something like Knoppix to the flash drive. However, Knoppix may not be compatible with the latest Mac hardware.

You can instead use the Mac OS installer application to create an installer which boots and runs from a flash drive. Apple publishes a guide for doing just this.

The summary is:

  • Partition and format the USB flash drive as Mac OS Extended.
  • sudo /Applications/Install\ macOS\ Mojave.app/Contents/Resources/createinstallmedia --volume /Volumes/MyVolume

Boot from the flash drive

Hold the 'option' key while booting, which should present a boot menu.

Launch a terminal

When your Mac boots from the flash drive, it should automatically start the Mac OS installer. From the menu at the top of your screen, there should be an option to open a terminal (which will exit the Mac OS Installer).

Create the backup

Plug in an external USB hard drive onto which you will write the backup file. Run df -h to see where your Mac mounted the external drive (e.g. /Volumes/UNTITLED).

Run diskutil list and find the device name which describes the hard drive internal to your Mac (e.g. /dev/disk0).

Run dd if=/dev/disk0 bs=1m | gzip --fast > /Volumes/UNTITLED/mybackup.dd.gz

This will use dd to read /dev/disk0 in 1 megabyte chunks (which speeds things up a bit), uses gzip to compress the backup image, and writes the backup to a file named /Volumes/UNTITLED/mybackup.dd.gz

Restoring from the backup

If you later need to restore from this backup, boot your Mac from the same USB flash drive, open a terminal, plug in your external USB drive, and run dd if=/Volumes/UNTITLED/mybackup.dd.gz | gunzip > /dev/disk0. Your Mac should now be restored to the exact state at which you performed the backup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment