There's been a number of questions regarding disk cloning tools and dd
has been suggested at least once. I've already considered using dd
myself, mainly because ease of use, and that it's readily available on pretty much all bootable Linux distributions.
What is the best way to use dd
for cloning a disk? I did a quick Google search, and the first result was an apparent failed attempt. Is there anything I need to do after using dd
, i.e. is there anything that CAN'T be read using dd
?
dd is most certainly the best cloning tool, it will create a 100% replica simply by using the following command. I've never once had any problems with it.
Be aware that while cloning every byte, you should not use this on a drive or partition that is being used. Especially applications like databases can't cope with this very well and you might end up with corrupted data.
To save space, you can compress data produced by dd with gzip, e.g.:
You can restore your disk with:
To save even more space, defragment the drive/partition you wish to clone beforehand (if appropriate), then zero-out all the remaining unused space, making it easier for gzip to compress:
Wait a bit, dd will eventually fail with a "disk full" message, then:
Also, you can get a dd process running in the background to report status by sending it a signal with the kill command, e.g.:
Check your system - the above command is for Linux, OSX and BSD dd commands differ in the signals they accept (OSX uses
SIGINFO
- you can press Ctrl+T to report the status).CAUTION: dd'ing a live filesystem can corrupt files. The reason is simple, it has no understanding of the filesystem activity that may be going on, and makes no attempt to mitigate it. If a write is partially underway, you will get a partial write. This is usually not good for things, and generally fatal for databases. Moreover, if you screw up the typo-prone if and of parameters, woe unto you. In most cases, rsync is an equally effective tool written after the advent of multitasking, and will provide consistent views of individual files.
However, DD should accurately capture the bit state of an unmounted drive. Bootloaders, llvm volumes, partition UUIDs and labels, etc. Just make sure that you have a drive capable of mirroring the target drive bit for bit.
When using
dd
to clone a disk which may contain bad sectors, useconv=noerror,sync
to ensure that it doesn't stop when it encounters an error, and fills in the missing sector(s) with null bytes. This is usually the first step I take if trying to recover from a failed or failing disk - get a copy before doing any recovery attempts, and then do recovery on the good (cloned) disk. I leave it to the recovery tool to cope with any blank sectors that couldn't be copied.Also, you may find
dd
's speed can be affected by the bs (block size) setting. I usually trybs=32768
, but you might like to test it on your own systems to see what works the fastest for you. (This assumes that you don't need to use a specific block size for another reason, e.g. if you're writing to a tape.)To clone a disk, all you really need to do is specify the input and output to dd:
Of course, make sure that you have proper permissions to read directly from /dev/hdb (I'd recommend running as root), and that /dev/hdb isn't mounted (you don't want to copy while the disk is being changed - mounting as read-only is also acceptable). Once complete, image.img will be a byte-for-byte clone of the entire disk.
There are a few drawbacks to using dd to clone disks. First, dd will copy your entire disk, even empty space, and if done on a large disk can result in an extremely large image file. Second, dd provides absolutely no progress indications, which can be frustrating because the copy takes a long time. Third, if you copy this image to other drives (again, using dd), they must be as large or larger than the original disk, yet you won't be able to use any additional space you may have on the target disk until you resize your partitions.
You can also do a direct disk-to-disk copy:
but you're still subject to the above limitations regarding free space.
As far as issues or gotchas go, dd, for the most part, does an excellent job. However, a while ago I had a hard drive that was about to die, so I used dd to try and copy what information I could off it before it died completely. It was then learned that dd doesn't handle read errors very well - there were several sectors on the disk that dd couldn't read, causing dd to give up and stop the copy. At the time I couldn't find a way to tell dd to continue despite encountering a read error (though it appears as though it does have that setting), so I spent quite a bit of time manually specifying skip and seek to hop over the unreadable sections.
I spent some time researching solutions to this problem (after I had completed the task) and I found a program called ddrescue, which, according to the site, operates like dd but continues reading even if it encounters an error. I've never actually used the program, but it's worth considering, especially if the disk you're copying from is old, which can have bad sectors even if the system appears fine.
If the source drive is damaged at all, you'll have more luck using
dd_rhelp
withdd_rescue
(my personal preference) or GNUddrescue
.The reason behind this is that, on read errors,
dd
keeps trying and trying and trying - potentially waiting for a long time for timeouts to occur.dd_rescue
does smart things like reading up to an error, then picking a spot further on on the disk and reading backwards to the last error, anddd_rhelp
is basically add_rescue
session manager - cleverly starting and resumingdd_rescue
runs to make it quicker again.The end result of
dd_rhelp
is maximum data recovered in minimum time. If you leavedd_rhelp
running, in the end it does the exact same job asdd
in the same time. However, ifdd
encountered read errors at byte 100 of your 100Gb disk, you'd have to wait a long time to recover the other 9,999,900 bytes*, whereasdd_rhelp
+dd_rescue
would recover the bulk of the data much faster.The source disk must not have any mounted filesystems. As a user able to read the block device (root works), run 'dd if=/dev/sda ....'
Now, one of the neat things here is that you're creating a stream of bytes... and you can do a lot with that: compress it, send it over the network, chunk it into smaller blobs, etc.
For instance:
But more powerfully:
The above copies a compressed image of the source harddrive to a remote system, where it stores it in numbered 2G chunks using the source host's name while keeping you updated on progress.
Note that depending on the size of disk, speed of cpu on source, speed of cpu on destination, speed of network, etc. You may want to skip compression, or do the compression on the remote side, or enable ssh's compression.
This will copy the disk, and skip blocks with errors, which is very important.
These are the basic and essential options for using dd to clone or rescue a disk.
I did not want to post another answer, but there were no good answers with the essential "conv=sync,noerror" options among the 25 already posted.
To clone a disk, all you really need to do is specify the input and output to
dd
:Of course, make sure that you have proper permissions to read directly from
/dev/hdb
(I'd recommend running as root), and that/dev/hdb
isn't mounted (you don't want to copy while the disk is being changed). Once complete,hdb.img
will be a byte-for-byte clone of the entire disk.There are a few drawbacks to using
dd
to clone disks. First,dd
will copy your entire disk, even empty space, and if done on a large disk can result in an extremely large image file. Second,dd
provides absolutely no progress indications, which can be frustrating because the copy takes a long time. Third, if you copy this image to other drives (again, using dd), they must be as large or larger than the original disk, yet you won't be able to use any additional space you may have on the target disk until you resize your partitions.You can also do a direct disk-to-disk copy:
but you're still subject to the above limitations regarding free space.
The first drawback can be resolved by gzipping the data as you make the copy. For example:
The second drawback can be resolved by using the pipeview (
pv
) tool. For example:I know of no way to overcome the third drawback.
Additionally, you can speed up the copy time by telling
dd
to work with larger chunks of data. For example:Another nice thing you can do with dd and rescue disks is copy data over the network:
You can stick gzip in both these pipelines if the network is not local. For progress, use
pv
. To make local_machine's netcat quit after it's done copying, you might add-w 5
or something.