Making backups of hard drives using the bash utility “dd”

July 18, 2018 4 minutes

So, I have an old corrupted hard drive that I need to back up so that it can be reused. In order to accomplish this, I learned about the dd bash command and how it can be used to create disk image backups. Overall, I have been successful with this project. Hopefully this article helps others who need to complete similar tasks.

Research

I did a bit of research online to start out. It turns out that every article covering dd warns that you risk overwriting data if you goof up. Some people have accidentally mixed up the parameters and overwrote the file they intended to duplicate. This may happen to you as well, so be really careful. You have been warned.

Syntax

$ dd if=/your/file/here of=/destination/ conv=noerror,sync status=progress

if = the initial file to be copied

of = the location the output file should be copied to (if this location is already occupied, the previous contents will be overwritten without warning)

Both of these options appear to work with files or directories. You can duplicate a file to another file, a directory into another directory, or a disk into a disk image. This last option is the one that interests me.

conv is an optional parameter that allows you to specify additional details like these:

  • noerror = do not stop the transfer if there is an error in reading the data source (if you need to use this option, consider using ddrescue instead)
  • sync = put data in exactly the same place as it was on the initial drive. If a block is incomplete, this option pads the end with zeroes so that the next block starts where it should. It also copies all the empty space that was on the initial drive, thus greatly increasing the time that the command takes to run.

Note: The dd command does not normally provide any output while it runs other than errors. This can be disconcerting when you are not sure whether or not it is working. By adding the parameter status=progress to the end, you will be able to see how much data has been transfered at any given time.

Process

I start with two drives.

  • Drive A is a 250 GB internal sata drive from an old computer.
  • Drive B, the destination for the backup, is a 2 TB USB external hard drive.

Step 1: Figure out the locations of each drive

Note: even though dd is a standard command on linux systems, diskutil is specific to MacOSX. So Ubuntu users will need to use a different utility for this step.

Michaels-MacBook-Pro:~ Michael$ diskutil list
...
/dev/disk2 (external, physical):
#:      TYPE NAME                   SIZE        IDENTIFIER
0:      FDisk_partition_scheme	   *2.0 TB      disk2
1:      Windows_NTFS TOSHIBA EXT    2.0 TB      disk2s1

/dev/disk3 (external, physical):
#:      TYPE NAME                   SIZE        IDENTIFIER
0:                                 *250.0 GB    disk3

This output tells me that Drive A is connected as /dev/disk3 and Drive B is mounted at /dev/disk2s1. Later I found I needed to use the volume name to identify Drive B. If you do not mount either volume, you should not have this problem.

Step 2: dd

I want to make this backup in the form of a .img disk image. This way I can store multiple drive images on the single external drive, as well as preserve the other backups that are already on that drive.

In order to connect my Mac to the NTFS formatted drive, I tried installing the free trial of Paragon NTFS. Sadly, it could not mount the corrupted drive. However, I am not sure if the driver it gave was needed.

This is the command that worked for me.

sudo dd if=/dev/disk3 of=/Volumes/TOSHIBA\ EXT/diskone.img

Step 3: Run the command

dd provides no output to confirm that it is working. However, I could see in Finder that the iso on the external drive was created and gradually increased in size. Content, I left the computer to complete the transfer independently. It took about four hours to transfer 250 GB.

Later I learned that if you include status=progress at the end of your command, dd will output statistics on how it is working.

As an alternative that keeps you much more informed, ddrescue worked fine for me on later projects. I am unsure of the exact differences between the utilities.

I hope this will help somebody else with a similar project. Thanks for reading!