]> git.sesse.net Git - backup.sh/commitdiff
Unify logging a bit.
authorSteinar H. Gunderson <sesse@samfundet.no>
Sat, 4 Oct 2014 22:45:39 +0000 (00:45 +0200)
committerSteinar H. Gunderson <sesse@samfundet.no>
Sat, 4 Oct 2014 22:45:39 +0000 (00:45 +0200)
backup.sh

index 6f861df68a3df6d9ae527ceea61ed09bea976abe..0e230399b4ed6be81fe2767139520ce439642e49 100755 (executable)
--- a/backup.sh
+++ b/backup.sh
@@ -48,6 +48,10 @@ die() {
        rm $LOCKFILE
        exit 1
 }
+diemsg() {
+       echo `date`": $computer: $@"
+       die
+}
 
 # Trap C-c and kill
 trap die SIGINT SIGTERM
@@ -79,8 +83,7 @@ backup()
                # If it does not exist, whine a bit and then run date-based instead.
                if [ -s "$SNARFILE" ]; then
                        if ! scp $SNARFILE root@$computer:$incrementalsnar; then
-                               echo `date`" $computer: Could not copy .incremental.snar to $computer"
-                               die
+                               diemsg "Could not copy .incremental.snar to $computer"
                        fi
                        lastcmd="--listed-incremental=$incrementalsnar --no-check-device"
                else
@@ -113,8 +116,7 @@ backup()
                # File is >0 in size and neither cat or tar tzvf failed; we assume it worked.
 
                if ! scp root@$computer:$incrementalsnar $SNARFILE.tmp; then
-                       echo `date`" $computer: Could not copy .incremental.snar from $computer"
-                       die
+                       diemsg "Could not copy .incremental.snar from $computer"
                fi
 
                # Move tar file in place
@@ -136,30 +138,26 @@ backup()
        else
                # Something wrong happened.
                rm $TARFILE
-               echo `date`" $computer: $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
-               die
+               diemsg "tar failed or $TARFILE empty. $backuplevel backup of $computer:$filesystem failed and deleted"
        fi
 }
 
 # Check that the target filesystem is mounted (actually check that it's not
 # the root filesystem)
 if [ ! -d "$storagedir/$computer" ]; then
-       echo `date`" $computer: Target filesystem ($storagedir/$computer) does not exist. Aborting"
-       die
+       diemsg "Target filesystem ($storagedir/$computer) does not exist. Aborting"
 fi
 rootfilesystem=`df -P /`
 targetfilesystem=`df -P "$storagedir/$computer"`
 if [ "$rootfilesystem" == "$targetfilesystem" ]; then
-       echo `date`" $computer: Target filesystem ($storagedir/$computer) was mounted on /. Aborting"
-       die
+       diemsg "Target filesystem ($storagedir/$computer) was mounted on /. Aborting"
 fi
 
 echo `date`" $computer: Backing up $computer" >&2
 
 # Try to SSH to the computer without entering a password.
 if ! `ssh -n -o NumberOfPasswordPrompts=0 root@$computer /bin/true`; then
-       echo `date`" $computer: Could not use passwordless SSH."
-       die
+       diemsg "Could not use passwordless SSH."
 fi
 
 # Check if pigz is available
@@ -179,34 +177,29 @@ filesystems=`ssh -n root@$computer "cat /etc/fstab" \
 
 # Clean up our dir at this client
 if ! ssh root@$computer "rm -r /root/.backup ; mkdir -m 700 /root/.backup"; then
-       echo `date`" $computer: Could not create backup staging area at $computer:/root/.backup"
-       die
+       diemsg "Could not create backup staging area at $computer:/root/.backup"
 fi
 
 # Try to copy $exclude to $computer
 if ! scp $exclude root@$computer:/root/.backup/exclude > /dev/null; then
-       echo `date`" $computer: Could not copy exclude.txt to $computer"
-       die
+       diemsg "Could not copy exclude.txt to $computer"
 fi
 
 # Try to copy preeexec and postexec if they exist
 if [ -f $confdir/preexec.$computer ]; then
        if ! scp $confdir/preexec.$computer  root@$computer:/root/.backup/preexec >&2; then
-               echo `date`" $computer: Could not copy preexec.$computer to $computer:/root/.backup/preexec"
-               die
+               diemsg "Could not copy preexec.$computer to $computer:/root/.backup/preexec"
        fi
 fi
 if [ -f $confdir/postexec.$computer ]; then
        if ! scp $confdir/postexec.$computer root@$computer:/root/.backup/postexec >&2; then
-               echo `date`" $computer: Could not copy postexec.$computer to $computer:/root/.backup/postexec"
-               die
+               diemsg "Could not copy postexec.$computer to $computer:/root/.backup/postexec"
        fi
 fi
 
 # Try to run preexec if it exists
 if ! ssh root@$computer "[ ! -f /root/.backup/preexec ] || /bin/bash -x /root/.backup/preexec" >&2; then
-       echo `date`" $computer: Could not run $computer:/root/.backup/preexec"
-       die
+       diemsg "Could not run $computer:/root/.backup/preexec"
 fi
 
 for filesystem in $filesystems
@@ -248,7 +241,7 @@ do
        fi
 
        # We want to be here
-       cd $storagedir/$computer/$sfilesystem/$backuplevel || die
+       cd $storagedir/$computer/$sfilesystem/$backuplevelmsg || die "$storagedir/$computer/$sfilesystem/$backuplevel does not exist"
 
        # Perform the actual backup
        backup
@@ -280,8 +273,7 @@ done
 
 # Try to run postexec if it exist
 if ! ssh root@$computer "[ ! -f /root/.backup/postexec ] || /bin/bash -x /root/.backup/postexec" >&2; then
-       echo `date`" $computer: Could not run $computer:/root/.backup/postexec"
-       die
+       diemsg "Could not run $computer:/root/.backup/postexec"
 fi
 
 # Remove lockfile