From 9f9329a04dccb87526a50481f4e73766b1c0ab30 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Sat, 22 Jul 2017 12:15:08 +0200 Subject: [PATCH] improve logging --- backup.sh | 66 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/backup.sh b/backup.sh index 2331d5d..cabc9a7 100755 --- a/backup.sh +++ b/backup.sh @@ -32,25 +32,32 @@ CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync" ## Helpers LAST="last"; INC="--link-dest=$TARGET/$LAST" +function addDate () +{ + while IFS= read -r line; do + printf "$($DATE):\t$line\n" + done +} + function backup () { - $DATE > $LOG + $ECHO "Creating backup in \"$TOSSH:$TARGET$1\"..." | addDate >> $LOG if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then - $ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR '\n' ' '" >> $LOG - $DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' ' >> $LOG 2>&1 + $ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR '\n' ' '" | addDate >> $LOG + $DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' ' | addDate >> $LOG 2>&1 fi # Create folders that don't exist if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then - $ECHO "$S $TOSSH $MKDIR -p $TARGET$1" >> $LOG - $S $TOSSH "$MKDIR -p \"$TARGET\"$1" >> $LOG 2>&1 + $ECHO "$S $TOSSH $MKDIR -p $TARGET$1" | addDate >> $LOG + $S $TOSSH "$MKDIR -p \"$TARGET\"$1" | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then - $ECHO "$MKDIR -p $TARGET$1" >> $LOG - $MKDIR -p "$TARGET"$1 >> $LOG 2>&1 + $ECHO "$MKDIR -p $TARGET$1" | addDate >> $LOG + $MKDIR -p "$TARGET"$1 | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi @@ -60,20 +67,20 @@ function backup () for SOURCE in "${SOURCES[@]}" do if [ -n $"S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then - $ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$1\" $INC " >> $LOG - $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from=$EXCLUDEFROM >> $LOG 2>&1 + $ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$1\" $INC " | addDate >> $LOG + $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi elif [ -n $"S" ] && [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then - $ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" >> $LOG - $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM >> $LOG 2>&1 + $ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" | addDate >> $LOG + $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi elif [-z "$S"] && [ -z "$TOSSH" ] && [ -z "$FROMSSh" ]; then - $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" >> $LOG - $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM >> $LOG 2>&1 + $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" | addDate >> $LOG + $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi @@ -82,30 +89,25 @@ function backup () # Create a link to the latest backup if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then - $ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" >> $LOG - $S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" >> $LOG 2>&1 + $ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" | addDate >> $LOG + $S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then - $ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" >> $LOG - $LN -nsf "$TARGET"$1 "$TARGET"$LAST >> $LOG 2>&1 + $ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" | addDate >> $LOG + $LN -nsf "$TARGET"$1 "$TARGET"$LAST | addDate >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi fi - $DATE >> $LOG - - if [ -n MAILADRESS ]; then - if [ $ERROR ]; then - $MAIL -s "[Backup] error occured during backup - $LOG" $MAILADRESS < $LOG - else - $MAIL -s "[Backup] backup created successfully - $LOG" $MAILADRESS < $LOG - fi - fi + $ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" | addDate >> $LOG } +# Clear log +$ECHO -n "" > $LOG + if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then TARGET=$TARGET/ fi @@ -116,8 +118,8 @@ elif [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then S="$SSH -p $FROMSSHPORT -l $FROMSSHUSER" elif [ -n "$TOSSH" ] && [ -n "$FROMSSH" ]; then ERROR=1 - $ECHO "Plese don't provide to and from ssh information!" >> $LOG - $ECHO "No backup performed!" >> $LOG + $ECHO "Plese don't provide to and from ssh information!" | addDate >> $LOG + $ECHO "No backup performed!" | addDate >> $LOG fi # Get date etc. @@ -140,5 +142,11 @@ fi # Create daily backups backup "daily/$($DATE +"%A")/$HOUR" - +if [ -n MAILADRESS ]; then + if [ $ERROR ]; then + $MAIL -s "[Backup] error occured during backup - $LOG" $MAILADRESS < $LOG + else + $MAIL -s "[Backup] backup created successfully - $LOG" $MAILADRESS < $LOG + fi +fi