Sunday, April 6, 2008

DVD/CD/Hard Drive Mirroring and Data Recovery using Linux (Kubuntu/Ubuntu)

So one of the Y-Cables in my computer failed. This caused (I think) random voltages to pass into my hard disk, which too started failing. Soon I found myself minus a hard disk and a lot of data. I had another hard disk of the exact same model, so I decided to use a Kubuntu/Ubuntu live disk to restore my failing hard disk to the other one.

My first attempt was to use dd.

dd if =/dev/hdX of=/dev/hdY

Now this would work but only because both hard disks were the exact same model. If they weren't I would have had to create a hard disk image and then copy data from that manually... not very useful. Unfortunately, the above doesn't work - dd stops at the first error. So a little research on the net showed the way:

dd bs=4k if=/dev/hdX of=/dev/hdY conv=noerror,sync

to mirror, or for an image

dd bs=4k if=/dev/hdX of=/path/to/image conv=noerror,sync

What do each of these options do? bs=4k makes dd copy in blocks of 4KB to ensure that it doesnt miss any good sectors, noerror means dont stop for errors, and sync means in case of an error output empty bytes so that the output image is not corrupted.

This unfortunately has a problem: bs=4k is miniscule. Telling dd to work like this will take days to recover data. Ideally we need something that moves extremely fast over a hard disk, scanning and copying good sectors with a large block size and then going back to reattempt the erroneous sectors with a smaller block size. There used to be a convoluted way to do this, using a program called dd_rescue and a batch file called dd_rhelp. However, this is not very efficient - the use of a batch file by definition slows things down. So the guys at GNU have come up with a very efficient program which does this automatically, called (guess?) ddrescue.

First you need to install ddrescue to your Live CD.

sudo apt-get install gddrescue

Note you need the gddrescue package not ddrescue. If you are unable to access the internet from your Live CD, you can download gddrescue*.deb (for your version of (K)Ubuntu) from here, copy it to a flash drive and then install it inside of the Live CD using

sudo dpkg -i gddrescue_1.2-1_i386.deb

There's a lot of advanced stuff you can do with ddrescue (see the man page, or the information here, here and here) However, in most normal use cases, you can jest get the data out using this command:

ddrescue /dev/hdX /dev/hdY /path/to/save/recovery.log

or, for an image

ddrescue /dev/hdX /path/to/image /path/to/save/recovery.log

The third option is non-compulsory. What it does is read and save a recovery log, so that if you have to shut down your machine or it crashes in the middle or recovery (recovery can take a LONG time for hard disks), ddrescue can resume seamlessly from where it left off (aint it cool)

Once you have the data out, if you mirrored your hard disk, you can just start using the new hard disk as normal (first making it a master etc). If you made an image, you can get the data from it out using

mkdir imagemout
sudo mount -o loop /path/to/image imagemount

For CD/DVDs, the output of dd/ddrescue is an ISO, which can be read or burned using any standard tool. Note that for DVD/CDs all the above applies, just for dd you have to use bs=2k not bs=4k

Of course, none of the above was useful for me... just putting in a new Y-Cable stopped any errors from showing up... I actually tossed a hard disk into the trash can, realized it wasnt the hard disk which was faulty and pulled it out. (And now it is running my Windows XP)

No comments: