Bump the revision number
[backups/.git] / scripts / restore-script.sh
1 #!/bin/sh
2
3 function err() {
4   echo >&2 "-E- $1"
5   exit 1
6 }
7
8 [ -n "$3" ] && mountdir=$3
9
10 [ -z "$mountdir" ] && err '$mountdir must be set'
11
12 rundir=${rundir:-$(dirname $0)}
13
14 export PATH=${rundir%/}:/bin:/usr/bin:/sbin:/usr/sbin:/sw/bin
15
16 if [ -z "$1" -o ! -d "$2" ]; then
17   err "
18
19 Usage: $(basename $0) <source-dir> <relocate-dir> [<mnt-dir> [remote_user@remote_host]]
20   source-dir   - Directory on backup disks to restore (relative to
21                  $mountdir)
22   relocate-dir - Base directory to restore files into (usually /)
23   mnt-dir      - Directory where backup disk is mounted (optional)
24
25 Examples:
26   # Restore everything from disks (explicitly specifying dvd mount point.)
27   % $(basename $0) / / /mnt/cdrom
28
29   # Restore everything from disks
30   % $(basename $0) / /
31
32   # Restore only files under /home on disks to /home on the system
33   % $(basename $0) /home /
34
35   # Restore files under /home on disks to /home/home on the system
36   # (this is usually not what you want.)
37   % $(basename $0) /home /home
38 "
39 fi
40
41 device=$(mount | grep "on ${mountdir%/} " | awk '{print$1}')
42
43 [ -z "$device" ] && err "Cannot determine cdrom device"
44
45 cp ${mountdir%/}/backups.db ${mountdir%/}/backup-status.txt $rundir
46 dbfile=${rundir%/}/backups.db
47
48 dates=$(cat $dbfile | list-dates.sh)
49
50 for date in $dates; do
51   filelist=$(tempfile)
52   # Check to see if files are actually needed from the current disk before requesting it.
53   cat $dbfile | files-from-date.sh $date | grep -z -Z -e "^${1%/}/" > $filelist
54   if [ -s $filelist ]; then
55     success=false
56     loop=0
57     while ! $success; do
58       eject $device
59       [ $? != 0 ] && err "Cannot eject the disk. Drive is busy. Run lsof $mountdir to see why."
60       echo "Please insert backup disk:  $date"
61       echo "Press Enter to continue"
62       read bogus
63       eject -t $device
64
65       # Mount the media
66       mount -t ext2 $device ${mountdir%/}
67
68       if grep -q $date ${mountdir%/}/backup-status.txt; then
69         success=true
70       elif e2label $device | grep -q $date; then
71         success=true
72       elif [ $loop -gt 2 ]; then
73         echo "I still can't verify the identity of the disk."
74         echo "If you are sure it is correct, press Enter to continue."
75         echo "Otherwise, press CTRL-C to abort."
76         read bogus
77         success=true
78       else 
79         echo "This doesn't seem to be the right disk."
80         loop=$((loop+1))
81       fi
82     done
83
84     rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
85     [ -z "$4" ] && cat $filelist | rsync $rsyncopts ${mountdir%/}/ ${2%/}/
86     [ -n "$4" ] && cat $filelist | rsync $rsyncopts -e ssh ${mountdir%/}/ ${4}:${2%/}/
87   else
88     echo "No files are needed from disk: $date.  Skipping it."
89   fi
90   rm -f $filelist
91 done