How to use ddrescue in Ubuntu?

What is ddrescue?

Ddrescue is an open source data recovery tool used to clone failing hard drives and storage devices. It was created by Antonio Diaz and is part of the GNU project [1]. Ddrescue copies data from a failing drive to a healthy drive while skipping over bad sectors. This allows maximum data recovery from partially failing drives.

The key benefit of ddrescue over other cloning tools is its ability to retry reading errors. This means it can repeatedly attempt to read from bad sectors in order to maximize the amount of data recovered. Ddrescue also has the ability to resume cloning from where it left off. This is useful if the cloning needs to be paused and restarted later.

Overall, ddrescue is an essential tool for recovering data from failing, damaged, or corrupted storage devices in Linux environments. Its clever error handling allows more data to be recovered than simply copying the entire drive.

Installing ddrescue

Ddrescue is available in the default Ubuntu repositories, so it can be easily installed using the apt package manager. To install ddrescue, open the terminal and run the following command:

“sudo apt install ddrescue”[1]

This will install the latest version of ddrescue and all its dependencies. Once the installation is complete, ddrescue will be ready to use.

Creating a recovery image file

To create a recovery image file with ddrescue, you need to use the following command syntax:

ddrescue [options] /dev/sdXN /path/to/imagefile logfile

Where:

  • /dev/sdXN – Specifies the source drive to clone. This is typically the failing drive.
  • /path/to/imagefile – Specifies the path and filename for the recovery image file that will be created.
  • logfile – Specifies the log file where ddrescue will save information about any read errors.

For example:

ddrescue /dev/sda /mnt/backups/sda.img sda.log

This will clone the /dev/sda drive to a recovery image file called sda.img in the /mnt/backups directory, and save read error information to sda.log.

It’s recommended to store the recovery image file on a healthy drive with ample free space. The logfile is important for retrying any failed reads later.

Specifying source and destination drives

To use ddrescue, you need to specify the source drive that will be cloned and the destination drive or image file where the clone will be written. Here’s how to specify each:

Source drive: Use the if= argument followed by the path to the source drive, such as /dev/sda. You can also specify a partition rather than the whole drive, like /dev/sda1.

Destination drive: Use the of= argument followed by the path to the destination drive, such as /dev/sdb. As with the source, you can specify a partition if needed.

Destination image file: To clone to an image file instead of a drive, specify the path and filename after of=, like of=/path/to/image.img. The image file should be at least the size of the source drive or partition.

For example:
ddrescue /dev/sda /dev/sdb clones the whole sda drive to sdb.
ddrescue /dev/sda1 /dev/sdb2 clones partition sda1 to partition sdb2.

See https://www.data-medics.com/forum/threads/how-to-clone-a-hard-drive-with-bad-sectors-using-ddrescue.133/ for more details on drive/partition specification.

Cloning a failing drive

ddrescue can be used to clone a failing drive to a healthy drive before the original drive completely fails. This allows you to recover data from a failing drive.

To clone a failing drive with ddrescue:

  1. Connect the source failing drive and destination healthy drive to your computer.
  2. Determine the device paths for the drives, such as /dev/sda for the source and /dev/sdb for the destination.
  3. Create a recovery image file to store retry data:

    ddrescue -f -n /dev/sda /dev/sdb sda.img


    The -f flag forces ddrescue to overwrite the destination, and -n tells it to not split the output into multiple files.
  4. Ddrescue will now clone the drive, skipping bad sectors and retrying them later.
  5. To pause the recovery, press Ctrl+C. To resume, rerun the same ddrescue command.
  6. When finished, the destination drive will contain a clone of the readable data from the failing source drive.

This allows you to access the data from the failing drive by instead using the healthy clone drive. Ddrescue logs all retry errors to the recovery file so you can reattempt them later if needed. For more on ddrescue, see the ddrescue manual.

Checking and retrying read errors

One of the key features of ddrescue is the ability to log read errors and retry them later. During the initial cloning process, ddrescue records any read errors in a logfile (using the -l option).

The logfile maps out all the blocks on the source drive, marking any unreadable blocks as errors. After the initial pass, you can examine this logfile to identify areas with read errors.

To retry the read errors, run ddrescue again with the -r option, specifying the same logfile. This tells ddrescue to consult the logfile and only focus on retrying the previously failed blocks:

ddrescue -r 3 /dev/sda /dev/sdb rescue.log

The -r flag is the number of retry passes to attempt. Setting it higher allows ddrescue to make multiple attempts at reading the problematic blocks before giving up. The default is 0, which means no retries.

With each pass, ddrescue progressively slows down read rates on areas with errors, making more effort to try and read damaged sectors. This helps maximize the rescue of data from even severely damaged drives.

Consult the logfile frequently to monitor any newly failed blocks. You can run as many retry passes as needed until you are satisfied with the results.

For more fine-grained control over retrying read errors, see the ddrescue manual page: https://www.gnu.org/software/ddrescue/manual/ddrescue_manual.html#Retrying

Pausing and resuming recovery

One useful feature of ddrescue is the ability to pause and resume the cloning process. This allows you to stop the recovery at any point and continue from that same position later.

To pause ddrescue, simply press Ctrl + C while it is running. This will interrupt the process and prompt you to continue or abort. Type “C” to continue the recovery later. Ddrescue will save its current position to the log file specified with the -l option.

To resume the process, run the ddrescue command again using the same arguments as before. Make sure to specify the same log file with -l. Ddrescue will pick up from where it left off previously based on the data in the log file.

The ability to pause and resume is useful if you need to temporarily stop the cloning process to use the source drive for other purposes. It also allows you to restart ddrescue after a system reboot or crash without losing progress. Just be sure to use the same log file when resuming.

For more details, see the ddrescue manual page: https://www.gnu.org/software/ddrescue/manual/ddrescue_manual.html#Interrupting

Recovery completion

Identifying when the ddrescue recovery process is fully completed is important. The recovery is finished when ddrescue has attempted to read all data from the source drive and reached the end of the drive.

There are a few signs that indicate the recovery is complete:

  • The terminal output will say “Finished” when ddrescue has completed the initial pass over the drive.
  • The mapfile showing bad sectors will no longer grow in size. All identified bad areas have been logged.
  • Rerunning ddrescue does not find any new errors. The number of bad areas stays the same.
  • The copied image file is stable and no longer growing in size.
  • The destination drive can be mounted and explored without any obvious gaps in files/data.

According to the Super User post What do you do after ddrescue finishes?, once the initial pass is done, the next step is verifying the rescue worked and checking for any missed data. Try mounting the clone drive and browsing files. If everything looks complete with no critical data missing, the ddrescue recovery is likely successful.

Advanced ddrescue options

Ddrescue has a number of advanced command line options that give you more control over the recovery process:1

Trim Option

The --trim option tells ddrescue to trim unaligned blocks at the end of the input file. This can help recover more data in cases where the input file size is not a multiple of the sector size.2

Skip Size

The --min-read-size option sets the minimum size of a chunk that ddrescue will try to read from the input file before retry. Increasing this skip size can help speed up recovery on some drives.3

Minimum Read Size

The --max-read-size option sets the maximum size that ddrescue will read at once from the input file. Decreasing this size can help with problematic drives by reading in smaller chunks.2

Troubleshooting

Ddrescue can encounter several issues during the recovery process that may require troubleshooting to resolve. Here are some common problems and potential solutions:

Error messages – If you receive error messages during the cloning or recovery process, first check that you have specified the correct source and destination drives. Errors like “no such file or directory” often mean the file paths are incorrect. Refer to the ddrescue documentation for help interpreting error messages.

Read errors – If ddrescue is unable to read from bad sectors, try running it again with the ‘-r3’ option to retry each read up to 3 times. You can also pause the recovery and retry the read errors selectively with the ‘-R’ flag. Check the ddrescue manual for more details on retrying reads.

Slow or stalled progress – If the cloning or recovery seems to stall, pause ddrescue and check for disk errors or bad sectors. You may need to replace the source drive if it is failing. Adjusting block size with ‘-b’ may also help improve read performance.

Failed or aborted recovery – If ddrescue fails entirely, first verify the destination drive has sufficient free space. If space is available, try running ddrescue again with more read retries. Finally, a last resort is cloning the drive sector-by-sector with ddrescue’s ‘-n’ option enabled.

Seeking help – Check the ddrescue documentation and online troubleshooting guides first when problems arise. For unresolved issues, seek help on the ddrescue mailing list or Ubuntu forums to tap into the experience of other users.