Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '13 at 16:37

2 Answers 2

up vote 5 down vote accepted

You should be able to dd it:

dd if=fileWithBadBlocks of=recoveredFile bs=4k conv=noerror,sync
share|improve this answer
    
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 '13 at 18:15
    
This can result in a larger output file, can fix that using: truncate --reference corrupt-file recovered-file –  Sam Watkins Jun 14 at 1:57

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.