Recovering a file with bad blocks in the middle

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.


Solution 1:

You should be able to dd it:

dd if=fileWithBadBlocks of=recoveredFile bs=4k conv=noerror,sync

Solution 2:

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.

Solution 3:

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