10 October 2012

Server Tip: Clone Hard disk with basic Linux Utlities

To clone identical HDD in a Linux system you can use dd command, dd command makes it really easy.
dd if=/dev/hda of=/dev/hdb
above command will clone hda to hdb (partitions, boot record etc), but what if you have to clone a drive that is not attached to same system?
I found a really interesting way to transfer files over the network simple by using netcat and dd.
  • Boot the machine (where second drive is attached) with a live cd distro like Ubuntu or Knoppix. Setup networking or let the DHCP server assign an ip automatically. My DHCP assigned this machine ip 192.168.0.2
  • run following command on this machine
       
         nc -l -p 12222 | dd of=/dev/hdb 
    (where /dev/hdb is the target drive)
  • Now come to the machine where you have attached the drive to be cloned and issue following command.

         dd if=/dev/hda | nc 192.168.0.2 12222 
    (/dev/hda is the drive to be cloned)
It will take time (usually several hours) depending on size of the drive. If you are worried about bandwidth you can pipe through gzip to compress and uncompress the streamed data on source and destination machine respectively, to do this you would run following commands.
On target machine:
        nc -l -p 12222 | gzip –dfc | dd of=/dev/hdb
And on source machine.
        dd if=/dev/hda | gzip –cf | nc 192.168.0.2 12222
Try dd and nc (Netcat) for simple backups too!

No comments:

Post a Comment