11

A large file on our ext4 file system may have picked up a bad block in the middle. Copying the file to another drive results in an input/output error part way through. Data beyond the bad sectors still seems ok though.

We don't have a backup of this file since it was recently created, so I'm looking for a way of copying the entire readable portion of the file while skipping bad sectors.

I've read other sites which suggest ways of recovering entire hard drives with bad blocks but they don't say much about recovering any files which span the bad blocks.

Thanks.

1
  • What type of file? Are you sure the file is of a type that will still be usable if a single block is corrupt? You could try performing a rescue on the entire drive, then mount the rescued filesystem, and recover the file.
    – Zoredache
    Mar 20, 2013 at 16:37

5 Answers 5

11

You should be able to dd it:

dd if=fileWithBadBlocks of=recoveredFile bs=4k conv=noerror,sync
2
  • 1
    This worked very well to copy the file away. It skipped over several sections which were corrupt but retrieved most of the file. Thank you very much for your help.
    – Chris C.
    Mar 20, 2013 at 18:15
  • 1
    This can result in a larger output file, can fix that using: truncate --reference corrupt-file recovered-file Jun 14, 2014 at 1:57
12

It should be noted that the file system will only be reading "blocks" off the disk (typically 4k) but corruption on hard disks would typically concern "sectors" (512 Bytes on non-ADF-disks).

If a single sector in a block is unreadable, the entire block is considered invalid by the filesystem and the contents are not returned upon request, so actually trying to copy the file might not give you the best results regarding the amount of recoverable data.

Instead, you could look into either using dd with the conv=noerror option and the default block size of 512 bytes or even ddrescue to copy the entire partition containing the file system and copy the file off afterwards.

2

A little update at 2021, recently I encountered the same issue, especially with innoDB (.ibd) files became corrupted. Needed to fix thing asap, and cloning the entire disk could take days, so I developed a software which intended to solve exactly the same issue.

I searched a lot without a success, thats why I made it to public. https://github.com/DaVieS007/Partial-File-Recovery

1
  • 1
    Can you tell us how this is different than ddrescue? Dec 7, 2022 at 23:10
2

While dd can be useful, for such a task I suggest using ddrescue. @the-wabbit suggested something similar, by reading the entire partition. However, ddrescue can be used even for single-file reads so you don't read the entire partition for recovering a specific file.

For example: ddrescue /to/be/rescued /the/rescued/file

1
  • What is the difference between this and dd conv=noerror ? Mar 22 at 17:01
-1

Just my 2c from this year.

I have arrived here because my raid 0 drive started having failed blocks and using my raid controller to 'replace disk' didn't do anything except copy the bad juju onto another disk?

Seeking to save what data I could, I am now trying ddrescue. Just adding how to install;

and the best one; a really useful guide; https://www.technibble.com/guide-using-ddrescue-recover-data/

I would also suggest using TMUX if you're running remotely? maybe there's smarter ways to do it but the ddrescue command needs to stay running and I don't want to just stay logged in remotely forever.

not sure why it's going to take 5 days to run either (500GB) but tmux:

 > run command
 > ctrl+b, d

now running in the tmux session you can reconnect too next time you log in.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .