fa16130323f4a2997a6eddfa3949a4997e585dd4
[backups/.git] / scripts / restore-script.sh
1 #!/bin/sh
2
3 if [ -z "$mountdir" ]; then
4   echo >&2 '$mountdir must be set'
5   exit 1
6 fi
7
8 if [ -z "$rundir" ]; then
9   echo >&2 '$rundir must be set'
10   exit 1
11 fi
12
13 export PATH=${rundir%%/}:/bin:/usr/bin:/sbin:/usr/sbin:/sw/bin
14
15 if [ $# != 2 -o ! -d "$1" -o ! -d "$2" ]; then
16   echo >&2 "Usage: $0 <source-directory> <target-directory>"
17   echo >&2 "Example: $0 / /"
18   exit 1
19 fi
20
21 device=$(mount | grep "on ${mountdir%%/} " | awk '{print$1}')
22
23 if [ -z "$device" ]; then
24   echo >&2 "Cannot determine cdrom device"
25   exit 1
26 fi
27
28 cp ${mountdir%%/}/backups.db ${mountdir%%/}/backup-status.txt $rundir
29 dbfile=${rundir%%/}/backups.db
30
31 dates=$(cat $dbfile | list-dates.sh)
32
33 for date in $dates; do
34   success=false
35   while ! $success; do
36     eject $device
37     echo >&2 "Please insert backup disk:  $date"
38     read bogus
39     eject -t $device
40
41     # Mount the media
42     mount -t ext2 $device ${mountdir%%/}
43
44     if grep -q $date ${mountdir%%/}/backup-status.txt; then
45       success=true
46     else
47       echo >&2 "This doesn't seem to be the right disk."
48     fi
49   done
50
51   rsyncopts="-W -H -l -p -t -g -o -0 --files-from=- --stats --progress"
52   cat $dbfile | files-from-date.sh $date | rsync $rsyncopts ${mountdir%%/}/ ${2%%/}/
53 done