Digital Camera Recovery Howto

Contents:

1. Why did I write this?
2. Brief introduction to digital cameras in linux
3. Tools used
4. Getting started
5. A little more advanced
6. Recovering MPEG files
7. Closing
8. Credits



1. Why did I write this?

	Well, I had managed to format my data partition by accident,
and since I didn't have enough money to afford another hard drive, I
didn't have a backup of most of the data (I know, I know). I did have
backups of most of my pictures, except for the last two "vacations" I'd
been on. Eventually it occured to me that, despite deletion of the
photos on the memory card, the camera had not been used since the
previous trip. The files (photos) should be intact on the memory card --
the camera uses the same technology as thumb drives, and the data
on thumb drives isn't cleared when the power goes off. I started
searching the internet for information on file recovery from a FAT32
partition on linux and how to recover deleted files on a digital
camera. There was very little information available, and in my
opinion, what was available wasn't well documented. So here we
are.


2. Brief introduction to digital cameras in linux

	I assume that most people who will be reading this already know
how to set up their digital camera in linux, so I'm not going to create
a comprehensive howto on that. I do want to do a quick run-through of
the theory behind linux connecting to a digital camera, though. Most
modern digital cameras (that I've used) connect to the computer through
the USB port, and are accessed just like a generic USB hard drive.
Linux accesses memory devices such as cameras and thumb drives through
the SCSI driver because it supports writing to a device. On my
computer, that means that once I've used modprobe to load the USB
driver and the usb-storage module, my camera can be accessed through
the device /dev/sda1. Some kernel versions use uba/ubb/etc. You need
to know which device your camera is connected as, just as you would if
you were checking the file system for stability or anything else.


3. Tools used

	* Linux (with root access)
	* Gnome desktop
		For Nautilus (the file manager), and
		Ghex2 (a hex editor)
	* cat
		Installed by default on most linux distro's
	* Photorec 1.2
		See: http://www.cgsecurity.org/index.html?photorec.html
	* Camera or card reader (surprise!)
		I used a camera, the Sony DSC P-52

	As with anything on linux, there are probably a lot of
	different packages and/or ways to do this. This is just a list
	to get you started.


4. Getting started

	OK, once you've downloaded Photorec, this is the basic syntax
to compile it for linux:

	# cd ~
	# tar -xzf /path/to/photorec-1.2.tar.gz
	# cd photorec
	# make

	The compile took a whole two seconds on my system. Once you run
make, you should have an executable for your system called "photorec."
Next, create a directory to save the files to:
	
	# mkdir recovered
	
	Now run photorec like this:

	# su
	# photorec /e /r /dev/sda1 /d ./recovered/

	Substitute /dev/sda1 for whichever device your camera is when
connected to on your system. Now comes the fun part. Open up Nautilus.
Go to the "recovered" directory (in your home directory under
"photorec"), and inspect the images. For me, most of the jpeg images
came out great. If you see that some of your pictures seem to have
been mixed together, or if you had .mpg files recovered, read on.
	

5. A little more advanced

	If your pictures start off with one image, and then about
half-way down it seems to switch to another picture, it is probably
because your filesystem was a little fragmented on your camera. If you
want to fix it, you'll have to get a hex editor (like ghex2 on my
system) and play around with copying parts of the files together to
see how it comes out (kind of like a jigsaw puzzle). The hardest part
is finding where the end of the first image is in one file, and where
it resumes in another. This is something you'll just have to experiment
on. There isn't a quick and easy way to know exactly which parts of the
files to copy/paste. You'll want to paste the image data into a new
file, and not modify the recovered files -- which could cause loss of
data for other pictures.


6. Recovering MPEG files

	This is subject to the same scrambled data problem as the
pictures, but for the most part these should be easier to work with. I
had several movie clips that I had taken, and when they had been
recovered, there was nearly 740 "recovered" files from my 128 MB
memory stick. If you have the same thing happen to you, this is how to
fix it. Open up the directory with the recovered files in it in
Nautilus, and take note of the files that Nautilus shows part of the
video clip as the icon for. My list looked like this:

	2.mpg
	129.mpg
	172.mpg
	263.mpg
	287.mpg
	295.mpg
	560.mpg
	669.mpg

	Assuming that most of my memory card was unfragmented, the
first video clip could be all of the .mpg files between 2 and 128...
then 129 through 171 could be the second one, and so on. This is where
"cat" comes in handy. This command can read multiple files and print
the output as one long string. Here's an example of my command:

	# cat 2.mpg 3.mpg 4.mpg 5.mpg 6.mpg 7.mpg 8.mpg 9.mpg 10.mpg>test.mpg

	Now fragments 2 through 10 have been combined together to
make "test.mpg." It can get rather bothersome to type out all of those
numbers, so here's a script to do it for you. Copy and paste this into
a file, and set its executable attribute (chmod ugo+x ):

	#!/bin/bash
	NUM=$1
	while [ $NUM != $2 ]; do
	OUT=`echo $OUT $NUM.mpg`
	((NUM++)); done
	cat $OUT

	# End of script

	I called this script "combine.sh," so to combine movie segments
2.mpg through 128.mpg, I would type this:

	# ./combine.sh 2 129 >video1.mpg


7. Closing

	I hope that my experiences help somebody. This is not a
foolproof method, merely a starting point. The best thing on your side
is patience -- lots of it! Good luck recovering your digital
images/videos :)


8. Credits

	* God
		He's awesome, and I love getting to hang out around
		Him :)
	* Christophe Grenier
		For writing a great program like Photorec
	* You
		For taking the time to read something I wrote