Purpose: This is always going to be a handy trick no matter if you are a beginner or an experienced user of Linux. If hard drive storage space is not a concern for you, and it should not be because the cost of storage keeps decreasing day-by-day, then you should clone your data or make duplicate copies of image so that you don’t lose your data accidentally. Making a clone of your data avoids the hassle of recovering your data.
Clone a CD/DVD
Suppose your friend gave you this CD/DVD to see some interesting stuff. You might accidentally damage the CD and could make it unreadable permanently. So before you start accessing the CD/DVD through your DVD ROM always first create a copy of it (of course after seeking proper permission). Insert your CD/DVD into the drive and give the following command:
# cp /dev/cdrom /opt/mycdromimage
Note: Your CD-ROM device could either be /dev/scd0 (if it is a USB-CDROM) or could simply be /dev/hda. Just see your /etc/fstab file to see what device name is associated with your CD/DVV-ROM drive.
Now you can access the contents of the CD even after removing it from the drive just as if it was a part of your hard drive. To access the clone image of your CD just give the following command:
# mount -o loop /opt/mycdromimage /mnt/target
Now do an “ls” and you can see the contents:
# ls /mnt/target
Clone a USB/IDE drive
Similarly we will clone an existing USB or an IDE drive so that you can restore it later in case if you lose the data on them or they get corrupted or stolen:
# dd if=/dev/hdb conv=sync,noerror bs=64K | gzip -c > /root/backup/hdb.img.gz
/dev/hdb = Drive that you would like to back up (clone)
/root/backup/hdb.img.gz = Destination where you would like to store the clone copy
Restoring the drive
Now let’s say at some point of time in future you would like to restore the hard drive data on a new drive because your previous hard drive crashed. To restore the data give the following command:
# gunzip -c /root/backup/hdb.img.gz | dd of=/dev/hdb conv=sync,noerror bs=64K
/root/backup/hdb.img.gz= Image that you would like to restore
/dev/hdb = Drive on on which you would like to restore the above image
Note that you can do all the above steps from a Live-CD also assuming that you have another external storage device attached so that you can store your clone image on it in order to restore from it later.
That’s it. Now you can relax without worrying about your data loss as long as you backup frequently.