Archive for October, 2007

Recover deleted files with photorec

Written on October 31st, 2007 by shakir
Categories: Information Insemination

I was just watching a movie while suddenly all the files except the hidden ones in my home directory were gone, just as someone issued the command

sudo rm -rf /home/shakir/*

To avoid further data loss, I quickly power off and boot into my Ubuntu live CD to create an image of my /home partition. Here’s how to see in which partition is my /home directory:

shakir@herugrim ~ $ cat /proc/partitions
major minor  #blocks  name
8     0  156290904 sda
8     1   21494938 sda1
8     2    8795587 sda2
8     3    2939895 sda3
8     4          1 sda4
8     5    9775521 sda5
8     6  113282316 sda6

From the partition sizes I can tell my /home partition was in the /dev/sda6, and the next step is to really create an image of the partition to safely doing the recovery process without risking of losing more data. /media/usbdisk is where my external usb harddisk is mounted

sudo dd if=/dev/sda6 of=/media/usbdisk/herugrim-sda6-20071010.img

Photorec is part of testdisk package, and this is how to install it in ubuntu;

sudo apt-get update
sudo apt-get install testdisk

It’s time to actually do run the program

sudo photorec /home/shakir/temp/herugrim-sda6-20071010.img

After going through some options, photorec starts doing it’s job.

Photorec stores recovered files in recup_dir.<sequence>/<sequence>.<file extension> in the specified target directory, which is not very useful. Here’s a script I wrote to find all the recovered JPEG files and move/rename it accordingly. Almost the same technique can be used for other file formats.

#!/bin/bash
PHOTODIR=/home/shakir/temp/photorec
cd $PHOTODIR
mkdir JPEG
for i in `ls | grep recup`;do
	for j in `ls $i/*.jpg`; do
		if FILE=`exiv2 $j 2>/dev/null | grep timestamp | awk ‘{ print $4"-"$5 }’ | tr -d ‘:’ | grep 200`; then
			cp -v $j "JPEG/"$FILE".jpg"
		fi
	done
done

Automatic backup using rsync

Written on October 9th, 2007 by shakir
Categories: Information Insemination

I’ve suddenly lost all the files in my home folders, and I quickly do these to not let the impact to be as bad, if it ever recurs.

Say 192.168.1.10 is my backup server (anduril), herugrim is my laptop’s name, these are the steps I did to let my laptop automatically rsync to the backup server.

Configure Password-less Login

Generate RSA key for use with SSH. Make sure you dont use any passphrase.

shakir@herugrim ~ $ ssh-keygen -t rsa -C shakir@192.168.1.10
Generating public/private rsa key pair.
Enter file in which to save the key (/home/shakir/.ssh/id_rsa): /home/shakir/.ssh/id_rsa.192.168.1.10
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/shakir/.ssh/id_rsa.192.168.1.10.
Your public key has been saved in /home/shakir/.ssh/id_rsa.192.168.1.10.pub.
The key fingerprint is: 11:d9:23:2e:68:05:59:d9:ac:5a:00:69:17:3a:b4:24  

Copy the public key to the server

shakir@herugrim ~ $ ssh-copy-id -i .ssh/id_rsa.192.168.1.10.pub shakir@192.168.1.10

Test if passwordless login successful

shakir@herugrim ~ $ ssh shakir@192.168.1.10 -i .ssh/id_rsa.192.168.1.10
shakir@anduril:~$

Configure Target Directory

shakir@anduril:~$ mkdir backup/herugrim -p

Manual Rsync (test power)

shakir@herugrim ~ $ rsync --verbose --progress --compress --rsh=ssh --recursive --times --perms --links --delete /home/shakir anduril:/home/shakir/backup/herugrim -e "ssh -i /home/shakir/.ssh/id_rsa.192.168.1.10"

Configure cron

At your machine To edit your crontab entry, issue this command

crontab -e

Add this entry to your crontab

30      9       *       *       1-5     rsync --compress --rsh=ssh --recursive --times --perms --links --delete /home/shakir anduril:/home/shakir/backup/herugrim -e "ssh -i /home/shakir/.ssh/id_rsa.192.168.1.10"

Run cron

sudo /etc/init.d/cron start     #depending on your linux distro

And I guess that’s all..

backup guys, backup… :)