Monday, May 23, 2016

Rescue files from dying hard drive

For rescuing files with "least effort" strategy (copy what can be read and don't try endlessly to recover what can't be read) ddrescue and find commands come to rescue. The command below tries to copy all the files from the current location to the destination; if copying fails due to any error it erases the destination file, so everything copied is actually good data (not damaged).

# Makes the directory structure copy
find . -type d -exec mkdir /dst/dir/{} \;

# Tries to copy the files with 1 retry  and exit on error (and delete dst file if exit on error)
find . -type f -exec sh -c "dd_rescue -X1 -e1 {} /dst/dir/{} || rm /dst/dir/{}" \;

Note: this solution only works if your file system is still readable.

Based on the answer from: http://superuser.com/questions/159354/linux-tool-to-copy-files-directories-from-failing-hard-disk