Linux: Save and Recover Data From Crashed Disks With ddrescue Command Like a Pro

Posted on in Categories CentOS, Data recovery, Debian Linux, Howto, Linux, RedHat/Fedora Linux, Sys admin last updated May 3, 2006

GNU ddrescue is a program that copies data from one file or block device (hard disk, cd/dvd-rom, etc) to another, it is a tool to help you to save data from crashed partition i.e. it is a data recovery tool. It tries to read and if it fails it will go on with the next sectors, where tools like dd will fail. If the copying process is interrupted by the user it is possible to continue at any position later. It can copy backwards.

This program is useful to rescue data in case of I/O errors, because it does not necessarily abort or truncate the output. This is why you need to use this program and not the dd command. I have recovered much data from many disks (CD/hard disk/software raid) over the years using GNU ddrescue on Linux. I highly recommend this tool to Linux sysadmins.

Install ddrescue on a Debian/Ubuntu Linux

Type the following apt-get command to install ddrescue:
# apt-get install gddrescue
Sample outputs:

Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
0 upgraded, 1 newly installed, 0 to remove and 3 not upgraded.
Need to get 49.6 kB of archives.
After this operation, 152 kB of additional disk space will be used.
Get:1 precise/universe gddrescue amd64 1.14-1 [49.6 kB]
Fetched 49.6 kB in 0s (1,952 kB/s)
Selecting previously unselected package gddrescue.
(Reading database ... 114969 files and directories currently installed.)
Unpacking gddrescue (from .../gddrescue_1.14-1_amd64.deb) ...
Processing triggers for install-info ...
Processing triggers for man-db ...
Setting up gddrescue (1.14-1) ...

Install ddrescue on a RHEL/Fedora/CentOS Linux

First turn on EPEL repo on a RHEL/CentOS/Fedora Linux. Type the following yum command:
# yum install ddrescue
Sample outputs:

Loaded plugins: product-id, rhnplugin, security, subscription-manager,
              : versionlock
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
This system is receiving updates from RHN Classic or RHN Satellite.
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package ddrescue.x86_64 0:1.16-1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
 Package            Arch             Version               Repository      Size
 ddrescue           x86_64           1.16-1.el6            epel            81 k
Transaction Summary
Install       1 Package(s)
Total download size: 81 k
Installed size: 189 k
Is this ok [y/N]: y
Downloading Packages:
ddrescue-1.16-1.el6.x86_64.rpm                           |  81 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : ddrescue-1.16-1.el6.x86_64                                   1/1
  Verifying  : ddrescue-1.16-1.el6.x86_64                                   1/1
  ddrescue.x86_64 0:1.16-1.el6

You can directly download ddrescue from the official GNU project web site and compile it on Linux or Unix-like systems.

A note about using ddrescue safely

  1. You need to use a logfile to resume a rescue.
  2. Never ever run ddrescue on a read/write mounted partition.
  3. Do not try to repair a file system on a drive with I/O errors.
  4. Be careful about destination partition/device, any data stored there will be overwritten.

How do I use ddrescue command?

In this example rescue a /dev/sda to /dev/sdb

     ## No need to partition /dev/sdb beforehand, but if the partition table on /dev/sda ##
     ## is damaged, you will need to recreate it somehow on /dev/sdb. ##
     ddrescue -f -n /dev/sda /dev/sdb logfile
     ddrescue -d -f -r3 /dev/sda /dev/sdb logfile
     ## get list of partitions on a /dev/sdb ##
     fdisk /dev/sdb
     ## check for errors ##
     fsck -v -f /dev/sdb1
     fsck -v -f /dev/sdb2

Understanding ddrescue command options

  • -f : Overwrite output device or partition.
  • -n : Do not try to split or retry failed blocks.
  • -d : Use direct disc access for input file.
  • -r3 : Exit after given three (3) retries (use -1 as infinity retries).
  • -b2048 : Sector size of input device [default is set to 512].

Example: Rescue a partition in /dev/sda3 to /dev/sdb3 in Linux

 ## You need to create the sdb2 partition with fdisk first. sdb2 should be of appropriate type and size ##
     ddrescue -f -n /dev/sda2 /dev/sdb2 logfile
     ddrescue -d -f -r3 /dev/sda2 /dev/sdb2 logfile
     e2fsck -v -f /dev/sdb2
     mount -o ro /dev/sdb2 /mnt
## Rread rescued files from /mnt ##
     cd /mnt
     ls -l
## Copy files using rsync ##
     rsync -avr .

Example: Rescue/recover a DVD-ROM in /dev/dvdom on a Linux

The syntax is:

     ddrescue -n -b2048 /dev/dvdrom dvd-image logfile
     ddrescue -d -b2048 /dev/dvdrom dvd-image logfile

Please note that if there are no errors (errsize is zero), dvd-image now contains a complete image of the DVD-ROM and you can write it to a blank DVD-ROM on a Linux based system:
# growisofs -Z /dev/dvdrom=/path/to/dvd-image

Example: Resume failed rescue

In this example, while rescuing the whole drive /dev/sda to /dev/sdb, /dev/sda freezes up at position XYZFOOBAR (troubled sector # 7575757542):

 ## /dev/sda freezes here ##
 ddrescue -f /dev/hda /dev/hdb logfile
 ## So restart /dev/sda or reboot the server ##
 ## Restart copy at a safe distance from the troubled sector # 7575757542 ##
 ddrescue -f -i 7575757542 /dev/sda /dev/sdb logfile
 ## Copy backwards down to the troubled sector # 7575757542 ##
 ddrescue -f -R /dev/sda /dev/sdb logfile

A note about dd_rescue command and syntax

On Debian / Ubuntu and a few other distro you end up installing other utility called dd_rescue. dd_rescue is a program that copies data from one file or block device to another, it is a tool to help you to save data from crashed partition.

Examples: dd_rescue

To make exact copy of /dev/sda (damaged) to /dev/sdb (make sure sdb is empty) you need to type following command:
# ddrescue /dev/sda /dev/sdb
Naturally, next step is to run fsck on /dev/sdb partition to recover/save data. Remember do not touch originally damaged /dev/sda. If this procedure fails you can send your disk to professional data recovery service. For example if /home (user data) is on /dev/sda2, you need to run a command on /dev/sdb2:
# fsck /dev/sdb2

Once fsck run, mount /dev/sdb2 somewhere and see if you can access the data:
# mount /dev/sdb2 /mnt/data
Finally, take backup using tar or any other command of your own choice. ddrescue command supports tons of options, read man page for more information:
# man dd_rescue
OR see gnu/ddrescue command man page:
# man ddrescue

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

31 comment

  1. I have on hd with NTFS file system which has important data to backed up and I have one more computer which has Readhat linux installed on it. How I can use dd_rescue to retrive data out from that hd having NTFS file system.

  2. Does anyone have a link to a ddrescue man page that is something more than that the program itself puts on screen when the user enters parameters incorrectly? It would be nice to know more about ddrescue than just what the options list is!

  3. Is there any other tool to recover files? I have a hard drive with bad sectors. It is an NTFS filesystem and has some importnat data on it. I have tried dd and now ddrescue, to no avail. I was trying to use autopsy/sleuthkit – but it requires an image. I am pulling my hair out trying to recover the files from this thing. I don’t care about the drive, just the data. All RAID from now on.

  4. On my Ubuntu 7.10, I can install the tool ddrescue
    sudo apt-get install ddrescue

    And it got installed:
    [sudo] password for vivek:
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    The following NEW packages will be installed:
    0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
    Need to get 17.6kB of archives.
    After unpacking 77.8kB of additional disk space will be used.
    Get:1 gutsy/universe ddrescue 1.13-3 [17.6kB]
    Fetched 17.6kB in 2s (8220B/s)
    Selecting previously deselected package ddrescue.
    (Reading database ... 123654 files and directories currently installed.)
    Unpacking ddrescue (from .../ddrescue_1.13-3_i386.deb) ...
    Setting up ddrescue (1.13-3) ...

  5. you write: “dd_rescue (also known as ddrescue)”. they are not at all the same software.

    ddrescue and dd_rescue are completely different programs which share no development between them. the two projects are not related in any way except that they both attempt to enhance the standard dd tool and coincidentally chose similar names for their new programs.

    ddrescue is the one to get.

  6. for clarity,
    * the PACKAGE “gddrescue” provides the COMMAND ddrescue
    * the PACKAGE “ddrescue” provides the COMMAND dd_rescue

    GNU ddrescue is the first one, and seems IMHO better/more flexible than dd_rescue.

  7. Damn, i can’t get it to work!!

    when I use the given command to start copying the img file, i get told that the destination is not a directory!

    I created a directory called backup.img but still get the stupid error

    so do you all are so very happy that it works

  8. I am trying to rescue an audio CD.
    $ ddrescue รขโ‚ฌโ€œn รขโ‚ฌโ€œb2048 /dev/cdrom cd.iso ddrescue.log
    ddrescue: Too many files.
    no output files produced.
    Is the file system corrupt or what?

  9. I’ve been given a dodgy old XP box to fix, apparently running our accounting system on some ghastly old COBOL system. The OS would show the initial post screens, then go black. I tried another video card, before suspecting the drive was going. I jammed in a new drive, and installed XP SP3 on it (yes I know, but the COBOL guy would have preferred W 98).

    I’m now trying to recover whatever is on the old drive. I plugged it in, and booted knoppix (6.4) off a cd. No ddrescue, but found dd & dd_rescue. So obviously I’m a noob, and don’t particularly want to copy an image, just whatever files I can grab. Again obviously, I’m ADD, impatient, and a bit stupid, and these tools are super cryptic. So now I’m booting into XP to see how XCopy manages. Yeesh.

    Not to troll or anything, but linux needs a tool for tools who don’t really much care if they recover the data or not.

    1. Having been reminded why no-one ever says ‘to recover corrupt files, just boot into XP’, I’ve had another go with dd_rescue. To be clear, because I’m running from a boot cd, I can’t (?) install either ddrescue or gddrescue. I eventually got it to run by mixing the lower level /dev/ for the source with the higher level /media/ for the destination:

      dd_rescue -l /media/sdb2/rescue.log /dev/sda1 /media/sdb2/old.img

      It’s still running, and note even though they’re only using 3% of this 80Gb drive, I still need room for an 80Gb image on sdb2.

  10. Sorry, me again, feel free to consolidate these three posts into one, or delete the lot.

    Anyway, all of my usage problems originated because my destination partition was only 40Gb. As soon as I swapped in a blank 80Gb drive, dd_rescue works great. I settled on dd_rescue /dev/sda1 /dev/sda2. Also it turned out the drive was physically OK, so putting it in my new XP installation and running scandisk sorted it out. I think I even recovered all files, the only thing missing was the ENTIRE Windows folder. So installing a different virus scanner.

Leave a Comment