]> git.sesse.net Git - backup.sh/blobdiff - backup.sh
Do not try to get .incremental.snar if we did a date-based backup.
[backup.sh] / backup.sh
index 075f89ad02502223901be9530af16f26403727b2..48307bc92ad3b81c994b3aa3b6d3920019b216be 100755 (executable)
--- a/backup.sh
+++ b/backup.sh
@@ -11,9 +11,6 @@
 # It's a bit peculiar, but quite effective. The following is worth keeping in
 # mind when hacking:
 #
-# KISS. And by that I mean really simple. Only regular Unix commands, please
-# (but GNU extensions are used and moreutils has to be installed).
-#
 # Because of the way we run this script from cron, informational logging goes
 # to stderr and warnings and errors to stdout. This way, we get mail whenever
 # stuff fails, and other output is redirected to the log file.
@@ -51,6 +48,10 @@ die() {
        rm $LOCKFILE
        exit 1
 }
+diemsg() {
+       echo `date`": $computer: $@"
+       die
+}
 
 # Trap C-c and kill
 trap die SIGINT SIGTERM
@@ -82,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
@@ -114,10 +114,11 @@ backup()
 
        if [ $? -eq 0 ] && [ -s $TARFILE ]; then
                # 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
+               if [ "$incrementalsnar" ]; then
+                       if ! scp root@$computer:$incrementalsnar $SNARFILE.tmp; then
+                               diemsg "Could not copy .incremental.snar from $computer"
+                       fi
                fi
 
                # Move tar file in place
@@ -139,30 +140,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
@@ -182,38 +179,32 @@ 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
-do
+for filesystem in $filesystems; do
        # Remove / and $ (for Windows) for path names
        sfilesystem=`echo $filesystem | tr '\/\$' '__'`
 
@@ -251,21 +242,22 @@ 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
 
        # Check if this box has a custom number of full backups
-       if [ -f $confdir/maksfulle.$computer ] ; then
+       if [ -f $confdir/maksfulle.$computer.$sfilesystem ]; then
+               mf=$((`cat $confdir/maksfulle.$computer`+1))
+       elif [ -f $confdir/maksfulle.$computer ] ; then
                mf=$((`cat $confdir/maksfulle.$computer`+1))
        else
                mf=$(($maxnumfull+1))
        fi
 
        # Delete old full backups
-       for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$mf`
-       do
+       for full in `ls -1t $storagedir/$computer/$sfilesystem/full/*tgz | tail -n +$mf`; do
                prefix=`echo $full | sed "s/\.[^.]*$//"`
                echo `date`": $computer:$filesystem Deleting full backup $prefix" >&2
                rm $prefix*
@@ -283,8 +275,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