Hard Drive Cloning in Linux
Here’s a really simple method I use for cloning hard drives. The source and target system must have a CD-ROM drive, a USB port and a hard drive (duh!).
Creating the Clone Image
1. Boot knoppix from CD. When you boot knoppix disable the swap space. You can speed things up by booting into a terminal. Here’s the command I use:
boot: knoppix 2 noswap
2. This step is optional. I like to make sure that all the unused space is zero’d out, because the zeros compress much better than randomized bytes. So I mount the partition, create a file of zeros which expands until it fills all unused space, then I delete the zero file. If the target drive has multiple partitions, then I do it for each partition. Be careful about FAT drives: files on FAT32 have a maximum size of 4GB, so to zero out more than 4 GB on a FAT32 partition I must create multiple zero files:
knoppix# dd if=/dev/zero of=/mnt/hda1/zero1
knoppix# dd if=/dev/zero of=/mnt/hda1/zero2
knoppix# rm -f /mnt/hda1/zero1 /mnt/hda1/zero2
3. Plug in an external USB drive. I use an external USB DVD-RW. You could adapt these command to copy to a file on an external hard drive. My DVD-ROM drive appears as /dev/sr0.
3. Here’s the command I use to copy the hard drive to the DVD-R
knoppix# dd if=/dev/hda | gzip -f | growisofs -Z /dev/sr0=/dev/fd/0
4. The burn takes some time. I go away and come back about an hour later.
5. Eject and label the CD.
Copying the Image to a Target
1. Again, boot from knoppix into a terminal
boot: knoppix 2 noswap
2. Use fdisk to partition the hard drive. (I’m not sure if this is necessary, since we are copying the entire hard drive.)
3. Install the USB external drive.
4. Use this command to copy the image from DVD-R to the hard drive:
knoppix# dd if=/dev/sr0 | zcat | dd of=/dev/hda
Copying the partition to an image on another computer
I often copy partitions to an image file on my server. Here is the procedure:
$ dd if=/dev/hda1 -bs 1k | gzip -c | ssh radagast ‘cat > backup.img.gz’
The command to rewrite the image file is:
# ssh radagast ‘cat backup.img.gz’ | gunzip -c | dd of=/dev/hda1 bs=1k
Copying the partition to another computer using netcat
The same thing can be done more quickly unencrypted using netcat, however some extra commands are needed on the destination computer. Suppose I want to copy /dev/hda1 from Origin to a file named hda1.ntfs.img on Destination. First, on Destination I must execute this command as root:
root@Destination# netcat -l -p 2222 | dd of=hda1.ntfs.img
Now on the origin computer stream the partition to Destination:2222 using this command:
root@Origin# dd if=/dev/hda1 bs=512 | netcat Destination 2222
Using the tar command with netcat
The same idea as above, but using a tar file. On the Destination computer
root@Destination# netcat -l -p 2222 | dd of=backup_directory.tar.gz bs=1k
and on the origin computer
root@Origin# tar -czvf – backup_directory | netcat Destination 2222
Mac OS X default install doesn’t include netcat, so sometimes I have to use ssh like this to backup the directory
root@Origin# tar -czvf – backup_directory | ssh user@destination ‘cat > of=backup_directory.tar.gz’
and this command later to restore the directory
root@Origin# ssh user@destination ‘dd if=backup_directory.tar.gz’ | tar xzvf –
290 total views, no views today