March 2, 2012

How-to: clone an hard disk or grab an image with dd

It happens that you need to clone the content of an hard disk or you want to store the image of a pendrive; if you are a “pure Microsoft user” you must rely on closed source programs but if you are a Linux user (even occasionally) you can do it with a simple live distribution like Ubuntu or similar.
In the case the disks have the same geometry the solution is crazy simple; assuming the original drive is /dev/sdb and the new disk is /dev/sdc the command to parse is:

# dd if=/dev/sdb of=/dev/sdc

If the disks don't share the same geometry the task is a bit longer but equallysimple: you must first replicate the partition table of the origin on the destination disk (you can use fdisk, parted or whatever you like!) and than copy every partition.
Assuming you partitioned the destination disk now for every partition you must parse the command:

# dd if=/dev/sdbX of=/dev/sdcX
where X is the number of the partition you are copying (ex. sdb1 -> sdc1, sdb2 -> sdc2, ...)

If you simply want to grab an image of a disk or a pendrive, assuming /dev/sdb is the original drive and /media/external_hd is the mount-point of the destination disk, you have to parse this command:

# dd if=/dev/sdb of=/media/external_hd/immagine_disco.img

Now to restore the image you have only to swap input with output:

# dd if=/media/external_hd/immagine_disco.img of=/dev/sdb

That's all folks!