Saturday, December 31, 2022

Recovering entire folders using ddrescue

I had to recover the contents of a hard disk using ddrescue because the hardware was faulty. The normal course of action involves using ddrescue on the entire disk or partition to make an image but that was not an option here because the hard disk was simply too big - the resulting image wouldn't fit in any of my other disks, not even after compression.

According to the documentation it was also possible to use ddrescue on individual files, but not on entire folders. But that was precisely what I had: a huge hierarchy of folders and files that I wanted to keep.

Let's consider that we have two folders, /source with the damaged filesystem, and /target where we will store whatever we can salvage.

Let's move to the source drive first:

cd /source

First we create the directory structure that will hold the files in the target disk:

find -type d -print -exec mkdir -p '/target/{}' \;

Then, we start moving the files:

find -type f -print -exec mv '{}' '/target/{}' \;

Files that can be read will be copied to the target disk and then deleted from the source disk. Files that are damaged will throw I/O errors and will stay in the source disk. Then we can use the "find" command with no parameters to see which those are. Recovering files with ddrescue can be particularly slow, so this is a good moment for you to remove the files you don't care too much about.

And then we start rescuing the damaged files:

find -type f -print -exec ddrescue --log-events=/target/ddrescue.log '{}' '/target/{}' \;

Good luck recovering your stuff.