X-Git-Url: http://git.pippins.net/embedvideo/.git/?a=blobdiff_plain;f=scripts%2Frestore-script.sh;h=af5a80cd684deee617c8cc4cedd522f6d6e3cca6;hb=8973f8a61994c0e28fce64a3dc688d1e5f438d42;hp=66ac4fb5b4da27c64842c96444daf04313059a71;hpb=9c5abcc59e83fa16bfeb810cc4d51960f8586a4e;p=backups%2F.git diff --git a/scripts/restore-script.sh b/scripts/restore-script.sh index 66ac4fb..af5a80c 100755 --- a/scripts/restore-script.sh +++ b/scripts/restore-script.sh @@ -1,67 +1,91 @@ #!/bin/sh function err() { - echo 2>&1 "-E- $1" + echo >&2 "-E- $1" exit 1 } -if [ ! -z "$3" ]; then - mountdir=$3 -fi +[ -n "$3" ] && mountdir=$3 -if [ -z "$mountdir" ]; then - echo >&2 '$mountdir must be set' - exit 1 -fi +[ -z "$mountdir" ] && err '$mountdir must be set' -if [ -z "$rundir" ]; then - echo >&2 '$rundir must be set' - exit 1 -fi +rundir=${rundir:-$(dirname $0)} -export PATH=${rundir%%/}:/bin:/usr/bin:/sbin:/usr/sbin:/sw/bin +export PATH=${rundir%/}:/bin:/usr/bin:/sbin:/usr/sbin:/sw/bin -if [ ! -d "$1" -o ! -d "$2" ]; then - echo >&2 "Usage: $0 []" - echo >&2 " source-dir - directory on backup disks to restore from" - echo >&2 " target-dir - directory to restore files to" - echo >&2 " mnt-dir - directory where backup disk is mounted (optional)" - echo >&2 "Example: $0 / / /mnt/cdrom" - exit 1 -fi +if [ -z "$1" -o ! -d "$2" ]; then + err " -device=$(mount | grep "on ${mountdir%%/} " | awk '{print$1}') +Usage: $(basename $0) [ [remote_user@remote_host]] + source-dir - Directory on backup disks to restore (relative to + $mountdir) + relocate-dir - Base directory to restore files into (usually /) + mnt-dir - Directory where backup disk is mounted (optional) -if [ -z "$device" ]; then - echo >&2 "Cannot determine cdrom device" - exit 1 +Examples: + # Restore everything from disks (explicitly specifying dvd mount point.) + % $(basename $0) / / /mnt/cdrom + + # Restore everything from disks + % $(basename $0) / / + + # Restore only files under /home on disks to /home on the system + % $(basename $0) /home / + + # Restore files under /home on disks to /home/home on the system + # (this is usually not what you want.) + % $(basename $0) /home /home +" fi -cp ${mountdir%%/}/backups.db ${mountdir%%/}/backup-status.txt $rundir -dbfile=${rundir%%/}/backups.db +device=$(mount | grep "on ${mountdir%/} " | awk '{print$1}') + +[ -z "$device" ] && err "Cannot determine cdrom device" + +cp ${mountdir%/}/backups.db ${mountdir%/}/backup-status.txt $rundir +dbfile=${rundir%/}/backups.db dates=$(cat $dbfile | list-dates.sh) for date in $dates; do - success=false - while ! $success; do - eject $device - [ $? != 0 ] && err "Cannot eject the disk. Drive is busy. Run lsof $mountdir to see why." - echo >&2 "Please insert backup disk: $date" - echo >&2 "Press Enter to continue" - read bogus - eject -t $device - - # Mount the media - mount -t ext2 $device ${mountdir%%/} - - if grep -q $date ${mountdir%%/}/backup-status.txt; then - success=true - else - echo "This doesn't seem to be the right disk." - fi - done - - rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress" - cat $dbfile | files-from-date.sh $date | grep -z -e "${1}" | rsync $rsyncopts ${mountdir%%/}/ ${2%%/}/ + filelist=$(tempfile) + # Check to see if files are actually needed from the current disk before requesting it. + cat $dbfile | files-from-date.sh $date | grep -z -Z -e "^${1%/}/" > $filelist + if [ -s $filelist ]; then + success=false + loop=0 + while ! $success; do + eject $device + [ $? != 0 ] && err "Cannot eject the disk. Drive is busy. Run lsof $mountdir to see why." + echo "Please insert backup disk: $date" + echo "Press Enter to continue" + read bogus + eject -t $device + + # Mount the media + mount -t ext2 $device ${mountdir%/} + + if grep -q $date ${mountdir%/}/backup-status.txt; then + success=true + elif e2label $device | grep -q $date; then + success=true + elif [ $loop -gt 2 ]; then + echo "I still can't verify the identity of the disk." + echo "If you are sure it is correct, press Enter to continue." + echo "Otherwise, press CTRL-C to abort." + read bogus + success=true + else + echo "This doesn't seem to be the right disk." + loop=$((loop+1)) + fi + done + + rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress" + [ -z "$4" ] && cat $filelist | rsync $rsyncopts ${mountdir%/}/ ${2%/}/ + [ -n "$4" ] && cat $filelist | rsync $rsyncopts -e ssh ${mountdir%/}/ ${4}:${2%/}/ + else + echo "No files are needed from disk: $date. Skipping it." + fi + rm -f $filelist done