9d9baabc9389a496e5a0db8771844331befe7f6b
[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 [ ! -d "$mountdir$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   success=false
41   while ! $success; do
42     eject $device
43     [ $? != 0 ] && err "Cannot eject the disk. Drive is busy. Run lsof $mountdir to see why."
44     echo "Please insert backup disk:  $date"
45     echo "Press Enter to continue"
46     read bogus
47     eject -t $device
48
49     # Mount the media
50     mount -t ext2 $device ${mountdir%%/}
51
52     if grep -q $date ${mountdir%%/}/backup-status.txt; then
53       success=true
54     else
55       echo "This doesn't seem to be the right disk."
56     fi
57   done
58
59   rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
60   cat $dbfile | files-from-date.sh $date | grep -z -Z -e "^${1}" | rsync $rsyncopts ${mountdir%%/}/ ${2%%/}/
61 done