Yesterday, Mohammed my friend asked me on how to clone disks since he remembers me cloning disks using Linux over Network. I thought of writing the way I do it so everyone could benefit from it.
Simply, dd is tool to convert or copy a file.. And what is lovely about *NIX based systems have disks and most hardware as hardware nodes accessible as files! Usually, on Linux, hard disks are accessible via /dev/hd[a] (For ATA Drives) or /dev/sd[a] (For SCSI or SATA Drives).
First Scenario is to image the hard drive to a USB disk, Assuming that we have booted a Linux Live CD, The USB Disk mounted to /media/disk and the hard disk is accessible via /dev/sda.
We run:
dd if=/dev/sda of=/media/disk/hard-disk-image-name.img
Note: if = input file, and of = output file
Now, lets say we want to restore the image to hard disk:
dd if=/media/disk/hard-disk-image-name.img of=/dev/sda
What if we want to save the image compressed and restore it from a compressed file, Assuming that you know gzip.
Disk to image compressed:
dd if=/dev/sda | gzip -9 | dd of=/media/disk/hard-disk-image-name.img.gz
Restore compressed Image to disk:
dd if=/media/disk/hard-disk-image-name.img.gz | gzip -d | dd of=/dev/sda
Ok, now all the above is about saving to an image on a USB disk, how about we want to write to an image or disk over the network, we have two options here, using SSH or Netcat!
I will be providing examples using Netcat, however, over SSH is similar, Its the same concept.
Disk to Disk or image over network (Its a matter of chaning the output file), assuming that we have booted a Live CD and we have a network configured on both linux machines on Network: 10.0.0.0/24, Master Machine IP: 10.0.0.1, and slave Machine is 10.0.0.2
Disk to disk over network, as I said above, it could be written to a USB disk image by changing the output file:
On Slave:
nc -l -p 9000 | dd of=/dev/sda
Note: Netcat options are as follows: -l is to listen to a port, -p to specify the port number to listen on.
On Master:
dd if=/dev/sda | nc 10.0.0.2 9000
Note: Netcat is going to connect to the slave machine IP and port as specified above.
Now, you can even using gzip over network if you are conservative about the bandwidth!