TLDR: The next few paragraphs can be mostly ignored by the general reader, feel free to skip to the solution below, if you simply have an SD card which you can read a bit of, but which dies before you’re able to access the full contents.
I’ve recently had the chance to learn about some fun tools for data recovery, in this instance through trying to recover some photos from a (presumably physically) damaged SD card for a relative over the holiday period.
My experience here is all based on this incident, related only to recovering photo data from a damaged SD card, although the tools and practices could possibly work for other forms of file recovery, on different kinds of media (notably spinning platter hard drives).
Additional Note: Since I made this work and posted about it on Twitter, I was pointed in the direction of this article, which covers the actual file recovery part, and covers a tool which I made use of in recovering the data from this SD card (photorec).
The Problem
The card I was handed had a problem which I’d not seen before. When presented to iPhoto in a card reader, the software would show that there were 309 photos and would attempt to begin an import, around half of the way through, OSX would throw up the “Removable media unplugged without being Safely Removed” dialogue, and the card would disappear from the mounted device list, and it’s device entry under /dev would disappear.
It seemed as if a small part of the physical SD card was damaged, which would cause the operating system to see the device as disconnected when that part of the card was read.
By iterating over the SD card byte-by-byte, I was able to discover that the faulty problem data appeared to occur at a consistent location on the card. By skipping to $errorByte+(some number of kilobytes) before starting the byte-by-byte read, I was able to continue getting valid data off the card (up until another small area of error bytes later on the card).
TLDR: read the disk byte-by-byte, found two small areas of the disk which would cause issues, was able to read the rest of the disk without a problem
This essentially left me with the knowledge that most of the data was probably intact, the errors appeared to be confined to two small (<1MB) areas of the physical SD card, around about the 53% and 85% marks. Unfortunately, this made recovering the data tricky. I wrote a bit more code to seek() past the bad bits and write the rest of the data to files, although this didn’t seem to provide me with anything I could restore from.
Solution: Ddrescue to the um… rescue!
Through some googling, I was found this page, which highlighted the *excellent* tool Ddrescue.
Unlike the standard GNU dd, which basically just copies “some” blocks from somewhere to somewhere else, ddrescue is designed to recover as much data as possible from a known-faulty device.
The way this is done is described here. In my case, ddrescue was able to scan over the card until it hit the bad section, then was able to continue past that section, even reading backwards from the end of the file until the end of the erroring sections to narrow down the bad areas as much as possible, all t