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

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.

ADVERTISEMENTS



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: gddrescue 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 http://mirrors.service.networklayer.com/ubuntu/ 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 ) ... Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: gddrescue 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 http://mirrors.service.networklayer.com/ubuntu/ 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 = =============================================================================== Installing: 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 Installed: ddrescue.x86_64 0 : 1.16 - 1 .el6 Complete! 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 ================================================================================ Installing: 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 Installed: ddrescue.x86_64 0:1.16-1.el6 Complete!

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

You need to use a logfile to resume a rescue. Never ever run ddrescue on a read/write mounted partition. Do not try to repair a file system on a drive with I/O errors. 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 ## 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.

: Overwrite output device or partition. -n : Do not try to split or retry failed blocks.

: Do not try to split or retry failed blocks. -d : Use direct disc access for input file.

: Use direct disc access for input file. -r3 : Exit after given three (3) retries (use -1 as infinity retries).

: Exit after given three (3) retries (use 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 . vivek @ server1.cyberciti.biz: / data / resuced / wks01 ## 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 . vivek@server1.cyberciti.biz:/data/resuced/wks01

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 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 ## reboot ## 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 ## /dev/sda freezes here ## ddrescue -f /dev/hda /dev/hdb logfile ## So restart /dev/sda or reboot the server ## reboot ## 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