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