Recover Data and (deleted) Partition with Linux from Hard Drives, CD-ROMs or DVDs

One important thing to know before doing anything else:

Every byte written to partitions in question will decrease chances to get data back since deletion of files and even partitions does not automatically mean actually deleting that data on disk. In most cases it only means deleting administrative entries in file or partition tables. But writing new files or partitions on the very same disk might actually overwrite data blocks containing pieces associated with files still intact! The second thing you might want to consider: If you mistakenly deleted single files or even a bunch of them look somewhere else but better not reboot until you really know why you have to do so as rebooting in those cases will sometimes also mess up chances to get back your data. Here I will only handle partition or hard drive errors.

So the very first step will be to get hold of extra storage (like USB drives), boot up some Live CD, e.g. your Ubuntu installation cd. If you have liability to paranoia you might even want to physically detach your hard drive and use some USB case to access it instead. First of all do something like

ddrescue -vr-1 /dev/sda recovered_data_blocks.img gddrescue.log

using gddrescue. Adjust parameters to your needs, especially /dev/sda (complete drive) or /dev/sda1 (partition in question). For Ubuntu you need to install gddrescue (= GNU ddrescue) package first. gddrescue will create an image trying hard to copy every data block still intact so you can, at any point, restore the drive stage where you first discovered you lost data. This, or course, can be used with any block-oriented medium such as CD-ROMs or DVDs. You might want to specify “-b 2048″ for CDs. Also use -n to get the most data on first run. For subsequent attempts you might want -rX where X is the number of retries for erroneous areas. I recommend to use -vr-1 for verbose infinite retries. gddrescue first reads as many as possible intact blocks and then splits error areas with each subsequent run. Note though for partitions or even complete hard drives it will take hours so be patient and make sure to use gddrescue’s log file option! With other tools I tried it took even more time which makes sense since gddrescue reads only blocks that really need to be read.

Second you should — just in case — save your partition geometries with fdisk (output similar):

$ sudo fdisk -ls
Password:

Disk /dev/hda: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1        9145    73457181    b  W95 FAT32
/dev/hda2            9146       19106    80011732+  83  Linux
/dev/hda3           19107       19457     2819407+   5  Extended
/dev/hda5           19107       19457     2819376   82  Linux swap / Solaris

Disk /dev/hdb: 160.0 GB, 160041885696 bytes
255 heads, 63 sectors/track, 19457 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System

Disk /dev/hdc: 20.8 GB, 20847697920 bytes
255 heads, 63 sectors/track, 2534 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdc1   *           1        2427    19494846   83  Linux
/dev/hdc2            2428        2534      859477+   5  Extended
/dev/hdc5            2428        2534      859446   82  Linux swap / Solaris

After ddrescue has finished try TestDisk for lost partitions which, among others, looks for back up partition tables, inode tables, and the like. To restore plain data there are heaps of tools. Most notably foremost and PhotoRec. Note though that for lost partitions there is no way to really know what file type found blocks belong to or even the name of associated files’ paths, at least as far as I can imagine. File names really are — in all file systems I know — only attributes of a data segment called file (or inode) such as access date, ownership and such. Sometimes file names are even stored in special files (directories or folders) which means there data blocks need to be intact and it has to be known where they are! The scenario I ran into was I mistakenly installed Ubuntu on top of Windows XP (NTFS partition) instead of resizing the partition as intended. TestDisk even though listing a NTFS partition (from it’s back-up MFT located at the end of NTFS partitions) couldn’t however restore the partition as such. Running Microsoft’s chkdsk stored a bunch of files with their filenames several folders into one called found.000 (I ran it inMicrosoft’s recovery command line from CD after manually through fdisk changed partition type to NTFS/HPFS, e.g. x07). Using the above mentioned tools I could restore some more but without there original path or file names. All together I recovered ~40 GB out of probably 60 GB data (which included system files).

Resources

Further reading:

6 Comments

  1. Thursday, 31st May 2012 at 18:10

    [...] I would suggest you to write a partition table similar to the original and ask testdisk to repair that one. here are some additional tips and hints: http://tldp.org/HOWTO/Partition/recovering.html https://sysblogd.wordpress.com/2008/0…-roms-or-dvds/ [...]

  2. Gabriel Villalobos said,

    Wednesday, 1st Feb 2012 at 23:52

    http://tldp.org/HOWTO/Partition/recovering.html

  3. Saturday, 12th Jun 2010 at 17:07

    [...] you can try to run testdisk or magic rescue on sdd. https://sysblogd.wordpress.com/2008/0…-roms-or-dvds/ [...]

  4. Saturday, 12th Jun 2010 at 08:36

    [...] You have no partition in that space – have you seen: https://sysblogd.wordpress.com/2008/0…-roms-or-dvds/ [...]

  5. skierpage said,

    Thursday, 4th Jun 2009 at 01:46

    I wrote a bootable 2GB flash drive image, *including* its partition table on top of my 120GB Windows/Linux hard drive… argggh.

    Fortunately I had a Kubuntu LiveCD lying around, so I booted it and got online to look for help. (Everyone should burn LiveCDs, they’re fun and essential in an emergency.) Your post is the first thing that Google found that wasn’t either yet another guide to creating a recovery CD (that contains no useful information about actual recovery), or a site that pretends to help with disk recovery but is actually trying to sell something. Thanks!

    TestDisk’s basic Analyze found my Linux partitions, and its Deep analyze found evidence of my original NTFS partition. Fantastic software.

    • Thursday, 15th Mar 2012 at 05:23

      Thanks skierpage,

      My usb harddisk accidentaly lost partition with sfdisk.
      I have succesfully restore all my partition from usb harddisk with testdisk on debian.
      Just re-plugin usb after Testdisk says “reboot your pc”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: