The procedure is as follows:
- Open the Terminal app
- Get disk list with the diskutil list
- To create the disk image: dd if=/dev/DISK of=image.dd bs=512
- To write the disk image: dd if=image.dd of=/dev/DISK
Let us see all commands in details.
1. Create disk image with dd command
Open the Terminal application and type the following command to list disks:
$ diskutil list
In this example my SD card size is 4GB and located at /dev/disk2.
2. Unmount the disk
Unmount the disk called /dev/disk2:
$ diskutil unmountDisk /dev/disk2
Here is what we see:
Unmount of all volumes on disk2 was successful
3. Create the disk image with dd
Finally create the disk image of the entire disk /dev/disk2:
$ sudo dd if=/dev/disk2 of=backup.my.sdcard-18-oct-2015.img.dd bs=512
OR
$ sudo dd if=/dev/disk2 of=backup.my.sdcard-18-oct-2015.img.dd bs=1m
OR
$ sudo dd if=/dev/disk2 of=foo.bar.img.dd bs=64k
It will take some time, and you won’t see any updates on the screen. You can press the ctrl+t (hold control key and press t) to see dd command progress on macOS:
Password: 60504+0 records in 60504+0 records out 3965190144 bytes transferred in 839.664927 secs (4722348 bytes/sec)
You can create compressed disk image as follows:
$ sudo dd if=/dev/disk2 bs=64K | gzip -c > backup.disk.img.dd.gz
Where,
- dd : Command name
- if=/dev/disk2 : Input disk name
- of=backup.my.sdcard-18-oct-2015.img.dd : Output image name
- bs=64k or bs=1m or bs=512 : Set both input and output block size to n bytes.
- gzip -c > backup.disk.img.dd.gz : Create compressed disk image using gzip
You can verify your disk with file command:
$ file disk-name-here.img.dd
disk-name-here.img.dd: x86 boot sector; partition 1: ID=0xc, starthead 130, startsector 8192, 114688 sectors; partition 2: ID=0x83, starthead 165, startsector 122880, 6277120 sectors, code offset 0xb8
NOTE: If you use the “dd” command with rdisk instead of disk, the cloning will be 20x faster.
How do I write dd images to disk again?
The syntax is as follows:
$ diskutil list
$ diskutil unmountDisk /dev/disk2
$ sudo dd if=backup.my.sdcard-18-oct-2015.img.dd of=/dev/disk2
### Restores compressed image and write /dev/disk2 ###
$ sudo sh -c ‘gunzip -c backup.disk.img.dd.gz | dd of=/dev/disk2’
e.g.:
dd if=/dev/disk2 -> dd if=/dev/rdisk2
dd of=/dev/disk2 -> dd of=/dev/rdisk2