e4fba472fb79803735f4032fc5b131c631e02c01
[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>]
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     while ! $success; do
57       eject $device
58       [ $? != 0 ] && err "Cannot eject the disk. Drive is busy. Run lsof $mountdir to see why."
59       echo "Please insert backup disk:  $date"
60       echo "Press Enter to continue"
61       read bogus
62       eject -t $device
63
64       # Mount the media
65       mount -t ext2 $device ${mountdir%/}
66
67       if grep -q $date ${mountdir%/}/backup-status.txt; then
68         success=true
69       else
70         echo "This doesn't seem to be the right disk."
71       fi
72     done
73
74     rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
75     cat $filelist | rsync $rsyncopts ${mountdir%/}/ ${2%/}/
76   else
77     echo "No files are needed from disk: $date.  Skipping it."
78   fi
79   rm -f $filelist
80 done