From 2963d003c1f7b4795ee38443f1b47ce0c8a4daf8 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sun, 5 Oct 2014 00:45:39 +0200 Subject: [PATCH] Unify logging a bit. --- backup.sh | 42 +++++++++++++++++------------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/backup.sh b/backup.sh index 6f861df..0e23039 100755 --- 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 -- 2.39.2