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

Solution 1:

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-keygen
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?*
do
    dd if=$partition | ssh user@host "dd of=$(basename $partition).img"
done

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).