James had a hard drive problem. He pulled the disk out of his laptop and brought it to me. Firstly I created a full image of the broken partition like so:
sudo dd if=/dev/sdb2 bs=1k conv=sync,noerror of=/path/to/image
Then I tried TestDisk. It worked like a charm and fixed the apparently broken NTFS boot sector. I thought that when James put the drive back in the laptop, it might “just work”, but apparently it didn’t. I had saved some of the most important files, but not all. James then wiped the drive to get a working machine again.
So now I had to restore files from an image of a broken partition. Turns out to be dead easy. The key ingredients were loopback and TestDisk.
sudo losetup /dev/loop0 /path/to/image
sudo testdisk /dev/loop0
It took me a while to figure out that I needed to choose partition table type none. I was dealing with an image of a single partition, so there was no partition table. After that, TestDisk behaved just like normal. I rebuilt the NFTS boot sector and then mounted the image like so:
sudo mkdir /mnt
sudo mount /dev/loop0 /mnt
This warned about the disk not having been shut down properly, ran something or other to clean it up, and then bingo, all the files were mounted and visible. I copied all the data from /mnt to an external drive, and will give that to James to restore from. Too easy!