How to clone drive using dd safely?

I’m not telling you how to use the following command to clone a drive to another drive:

dd if=/dev/sda of=/dev/sdb bs=32M status=progress

I’m warning you about the possible risk of doing this. The risk is not that it did not show you a warning message bout possible data loss before doing its job. The risk is the whole process until you type this command and press enter. Without caution, it will be a disaster for you.

First, let’s clear about the syntax of this command. The if argument is the input file, i.e., dd will read data from this drive. The of argument is the output file, i.e., dd will write the data that it reads from the input file to this file. The bs argument is how many bytes dd reads once, i.e., dd reads bs bytes from the input file and writes the read data to the output file, then reads next bs bytes and writes them to the output file, …, till the end. The status argument lets dd display progress information at the last line of the screen which is like:

xxxxxx bytes read(yyy GB,zzz GiB), time elapsed, speed(MB/s)

How to interpret the numbers xxxxx,yyy,and zzz? Well, they all convey the same piece of information: the bytes copied so far.  yyy is in the unit of GB(1000^3), zzz is in the unit of GiB(1024^3), so yyy may not be the same as zzz.

Don’t type the command as follows:

dd -if=/dev/sda -of=/dev/sdb -bs=32M -status=progress

 

Now, we talk about the risk and possible damage of dd. Typically, you will use the dd command in a Linux shell. The problem is that it is hard for a newbie to get the information of operated drives of the dd command. You don’t know which physical drives /dev/sda and /dev/sdb correspond to. If you guess the wrong correspondences between the device names and the physical drives. Your drive that is to be backed up may actually be overwritten. Because you reply on dd to backup the data, it is very likely there was no existing backup of /dev/sda. Once that drive is overwritten, you will totally lose the data to be backed up,and there is no chance for you to recover or back it up later. Don’t think it is unlike to happen. The device names in /dev/ are very confusing and their correspondences to the real physical devices are varying. For example, if you boot the Linux system from a bootable USB driver which is the only storage device on your computer, the /dev/sda will correspond to that USB driver. If, later, you plugin a HDD and restart the computer, the /dev/sda will correspond to the HDD while /dev/sdb now correspond to the USB drive. If you insert the second HDD, and restart the PC, /dev/sda may correspond to the first HDD, /dev/sdb may correspond to the second HDD, and /dev/sdc now points to the USB drive. Remember, there is no fixed correspondence between the device name and the physical drive, and you should check carefully before executing the dd command. How? You can use the gdisk command to check every device name such as /dev/sda, /dev/sdb, /dev/sdc. gdisk will show the model names for the HDDs for you to differentiate them.  You can also press the “p” in gdisk to show the partition information of the disks to confirm they are the disks to be processed.

 

Posted in Webmaster Tools