X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=backup.sh;h=a5cc91570a4455e7f90a258a481f761295070fdb;hb=ebd6ca30a863bc9d9d073ab2ea5760f248d2034e;hp=fce079c8aef076012608b5643c0a65d6e59ea42f;hpb=35ee17f5f1344cab950789aca51415dc4b8093c3;p=backup.sh diff --git a/backup.sh b/backup.sh index fce079c..a5cc915 100755 --- a/backup.sh +++ b/backup.sh @@ -20,6 +20,7 @@ trap die SIGINT SIGTERM # Don't start if we're already running if [ -e $LOCKFILE ]; then echo `date`": $LOCKFILE exists, exiting." + echo `date`": $LOCKFILE exists, exiting." >&2 exit 1 fi; touch $LOCKFILE @@ -112,8 +113,8 @@ backup() TARCMD="ssh root@$computer \"$tar --one-file-system -zcf - -C $filesystem . $lastcmd \ --exclude-from=$exf\" > $TARFILE" echo `date`" $computer: Running $TARCMD" >&2 - - if eval $TARCMD; then + eval $TARCMD; + if [ -s $TARFILE ]; then mv $TARFILE $DATE.tgz #make a filelist. #update the datefile if the filelist is ok. @@ -122,7 +123,7 @@ backup() touch -t $DATE ../.date #make a sortet filelist - grep -v ^d $DATE.idx | sort -n -r +2 > $DATE.sdx + grep -v ^d $DATE.idx | sort -n -r -k 2 > $DATE.sdx #fix perm chmod 600 *tgz #only for us @@ -151,7 +152,8 @@ do # We don't want to return 0 R=1 - break; + # Do next computer + continue; fi #tømmer variabelen for sikkerhets skyld @@ -166,39 +168,44 @@ do #clean up our dir at this client if ! ssh root@$computer "rm -r ~/.backup ; mkdir -m 700 ~/.backup"; then - echo `date`" $computer: Could not create backup staging area at $computer:~/.backup" + echo `date`" $computer: Could not create backup staging area at $computer:~/.backup - skipping backup of $computer" # We don't want to return 0 R=1 - break; + continue; fi #try to copy $exclude to $computer if ! scp $exclude root@$computer:~/.backup/exclude > /dev/null; then - echo `date`" $computer: Could not copy exclude.txt to $computer" + echo `date`" $computer: Could not copy exclude.txt to $computer - skipping backup of $computer" # We don't want to return 0 R=1 - break; + continue; fi #try to copy preeexec and postexec if they exist # TODO: Gah, clean this mess! [ -f $confdir/preexec.$computer ] && ( - scp $confdir/preexec.$computer root@$computer:~/.backup/preexec || - ( echo `date`" $computer: Could not copy preexec.$computer to $computer:~/.backup/preexec"; + scp $confdir/preexec.$computer root@$computer:~/.backup/preexec >&2 || + ( echo `date`" $computer: Could not copy preexec.$computer to $computer:~/.backup/preexec - skipping backup of $computer" R=1 - break + continue ) ) [ -f $confdir/postexec.$computer ] && ( - scp $confdir/postexec.$computer root@$computer:~/.backup/postexec || - ( echo `date`" $computer: Could not copy postexec.$computer to $computer:~/.backup/postexec" - break + scp $confdir/postexec.$computer root@$computer:~/.backup/postexec >&2 || + ( echo `date`" $computer: Could not copy postexec.$computer to $computer:~/.backup/postexec - skipping backup of $computer" R=1 + continue ) ) #try to run preexec if it exist - ssh root@$computer "[ -f ~/.backup/preexec ] && /bin/bash -x ~/.backup/preexec" + if ! ssh root@$computer "[ ! -f ~/.backup/preexec ] || /bin/bash -x ~/.backup/preexec" >&2; then + echo `date`" $computer: Could not run $computer:~/.backup/preexec - skipping backup of $computer" + R=1 + continue + fi + for filesystem in $filesystems do @@ -241,7 +248,7 @@ do fi #delete complete backups - for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail +$mf` + for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$mf` do prefix=`echo $full | sed "s/\.[^.]*$//"` echo `date`": $computer:$filesystem sletter full $prefix (for mange)" >&2 @@ -258,7 +265,10 @@ do done #try to run postexec if it exist - ssh root@$computer "[ -f ~/.backup/postexec ] && /bin/bash -x ~/.backup/postexec" + if ! ssh root@$computer "[ ! -f ~/.backup/postexec ] || /bin/bash -x ~/.backup/postexec" >&2; then + echo `date`" $computer: Could not run $computer:~/.backup/postexec" + R=1 + fi ) done & @@ -269,6 +279,8 @@ wait rm $LOCKFILE # Did anything go wrong? +# (IMPORTANT NOTE: The R-business does not work as expected, since this script +# forks itself. if [ $R != 0 ]; then echo `date`": Backup run ended with errors, check logs." exit 1