Experiences with using `dd` to copy multi-partition, multi-FS device

MadHatter asked:

I have a disc with several partitions on, each of which has a different kind of file system (ext3, ext4, and, let’s say, NTFS). I want to know if I can copy the whole disc to a new one (which is of larger capacity) using dd, preferably in a single operation.

To forestall unnecessary effort, let me clarify a few things. I’m not interested in other ways to do it (I know the right way, and third-party software is not desirable). I’m not all that interested in theoretical answers (“it ought to work”). I’m extremely interested in anyone else’s experiences trying to do such a thing.

I know the real right answer is “try it and see”, but I’m 250 miles from the disc in question.

My answer:

If you have to do this, and you have to be in a really bad place to even think about doing this…

It’s safest to copy the partitions individually. For instance:

# Install  a temporary ssh key so we don't have to deal with lots of
# password prompts, remember to remove it from authorized_keys later
ssh-copy-id -i ~/.ssh/id_rsa user@host

# Copy everything, you can go have breakfast - and probably lunch -
# while you wait
for partition in /dev/sdb?*
    dd if=$partition | ssh user@host "dd of=$(basename $partition).img"

Wait a few minutes or hours for the copy to complete, and then begin your recovery from the disk images. For instance, on a new disk you may create a new partition table with partitions of the correct size to hold the disk images, and then dd the images back out to the new partitions.

Copying the entire disk (e.g. dd if=/dev/sdb) makes things more complicated, since you then have a full disk image you will probably have to split out into partitions somehow anyway. And if you’re thinking of dding the entire drive directly to another physical disk, think again: sometimes you’ll just lose when the system refuses to acknowledge that the partition table and the new hard drive have anything to do with each other.

If you must copy the entire disk in one shot (you likely don’t have to, so don’t think about this) then copy it to an image file and use kpartx to work with the partitions. For instance:

dd if=/dev/sdb | ssh user@host "dd of=sdb.img"
# Later, on the recovery host...
kpartx -l sdb.img
kpartx -a sdb.img
# Now you can...
mount -o ro /dev/mapper/loop0p1 /mnt/rescuep1
mount -o ro /dev/mapper/loop0p2 /mnt/rescuep2
# or copy to a new hard drive...
dd if=/dev/mapper/loop0p1 of=/dev/sdc1
dd if=/dev/mapper/loop0p2 of=/dev/sdc2

If the physical disk is having physical trouble, consider using dd_rescue instead (though you didn’t really ask about that).

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.