Oops, a silly little bug.
[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 [ -z "$rundir"   ] && err '$rundir must be set'
12
13 export PATH=${rundir%/}:/bin:/usr/bin:/sbin:/usr/sbin:/sw/bin
14
15 if [ -z "$1" -o ! -d "$2" ]; then
16   err "
17
18 Usage: $(basename $0) <source-dir> <relocate-dir> [<mnt-dir>]
19   source-dir   - Directory on backup disks to restore (relative to
20                  $mountdir)
21   relocate-dir - Base directory to restore files into (usually /)
22   mnt-dir      - Directory where backup disk is mounted (optional)
23
24 Examples:
25   # Restore everything from disks (explicitly specifying dvd mount point.)
26   % $(basename $0) / / /mnt/cdrom
27
28   # Restore everything from disks
29   % $(basename $0) / /
30
31   # Restore only files under /home on disks to /home on the system
32   % $(basename $0) /home /
33
34   # Restore files under /home on disks to /home/home on the system
35   # (this is usually not what you want.)
36   % $(basename $0) /home /home
37 "
38 fi
39
40 device=$(mount | grep "on ${mountdir%/} " | awk '{print$1}')
41
42 [ -z "$device" ] && err "Cannot determine cdrom device"
43
44 cp ${mountdir%/}/backups.db ${mountdir%/}/backup-status.txt $rundir
45 dbfile=${rundir%/}/backups.db
46
47 dates=$(cat $dbfile | list-dates.sh)
48
49 for date in $dates; do
50   filelist=$(tempfile)
51   # Check to see if files are actually needed from the current disk before requesting it.
52   cat $dbfile | files-from-date.sh $date | grep -z -Z -e "^${1%/}/" > $filelist
53   if [ -s $filelist ]; then
54     success=false
55     while ! $success; do
56       eject $device
57       [ $? != 0 ] && err "Cannot eject the disk. Drive is busy. Run lsof $mountdir to see why."
58       echo "Please insert backup disk:  $date"
59       echo "Press Enter to continue"
60       read bogus
61       eject -t $device
62
63       # Mount the media
64       mount -t ext2 $device ${mountdir%/}
65
66       if grep -q $date ${mountdir%/}/backup-status.txt; then
67         success=true
68       else
69         echo "This doesn't seem to be the right disk."
70       fi
71     done
72
73     rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
74     cat $filelist | rsync $rsyncopts ${mountdir%/}/ ${2%/}/
75   else
76     echo "No files are needed from disk: $date.  Skipping it."
77   fi
78   rm -f $filelist
79 done