improve logging

This commit is contained in:
Shannon Sterz 2017-07-22 12:15:08 +02:00
parent f55408a258
commit 9f9329a04d

View file

@ -32,25 +32,32 @@ CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync"
## Helpers ## Helpers
LAST="last"; INC="--link-dest=$TARGET/$LAST" LAST="last"; INC="--link-dest=$TARGET/$LAST"
function addDate ()
{
while IFS= read -r line; do
printf "$($DATE):\t$line\n"
done
}
function backup () function backup ()
{ {
$DATE > $LOG $ECHO "Creating backup in \"$TOSSH:$TARGET$1\"..." | addDate >> $LOG
if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then
$ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR '\n' ' '" >> $LOG $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' ' ' >> $LOG 2>&1 $DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' ' | addDate >> $LOG 2>&1
fi fi
# Create folders that don't exist # Create folders that don't exist
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
$ECHO "$S $TOSSH $MKDIR -p $TARGET$1" >> $LOG $ECHO "$S $TOSSH $MKDIR -p $TARGET$1" | addDate >> $LOG
$S $TOSSH "$MKDIR -p \"$TARGET\"$1" >> $LOG 2>&1 $S $TOSSH "$MKDIR -p \"$TARGET\"$1" | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
$ECHO "$MKDIR -p $TARGET$1" >> $LOG $ECHO "$MKDIR -p $TARGET$1" | addDate >> $LOG
$MKDIR -p "$TARGET"$1 >> $LOG 2>&1 $MKDIR -p "$TARGET"$1 | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
@ -60,20 +67,20 @@ function backup ()
for SOURCE in "${SOURCES[@]}" for SOURCE in "${SOURCES[@]}"
do do
if [ -n $"S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then if [ -n $"S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
$ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$1\" $INC " >> $LOG $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 >> $LOG 2>&1 $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
elif [ -n $"S" ] && [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then elif [ -n $"S" ] && [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then
$ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" >> $LOG $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 >> $LOG 2>&1 $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
elif [-z "$S"] && [ -z "$TOSSH" ] && [ -z "$FROMSSh" ]; then elif [-z "$S"] && [ -z "$TOSSH" ] && [ -z "$FROMSSh" ]; then
$ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" >> $LOG $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" | addDate >> $LOG
$RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM >> $LOG 2>&1 $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
@ -82,30 +89,25 @@ function backup ()
# Create a link to the latest backup # Create a link to the latest backup
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
$ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" >> $LOG $ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" | addDate >> $LOG
$S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" >> $LOG 2>&1 $S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
$ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" >> $LOG $ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" | addDate >> $LOG
$LN -nsf "$TARGET"$1 "$TARGET"$LAST >> $LOG 2>&1 $LN -nsf "$TARGET"$1 "$TARGET"$LAST | addDate >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
fi fi
$DATE >> $LOG $ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" | addDate >> $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
} }
# Clear log
$ECHO -n "" > $LOG
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
TARGET=$TARGET/ TARGET=$TARGET/
fi fi
@ -116,8 +118,8 @@ elif [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then
S="$SSH -p $FROMSSHPORT -l $FROMSSHUSER" S="$SSH -p $FROMSSHPORT -l $FROMSSHUSER"
elif [ -n "$TOSSH" ] && [ -n "$FROMSSH" ]; then elif [ -n "$TOSSH" ] && [ -n "$FROMSSH" ]; then
ERROR=1 ERROR=1
$ECHO "Plese don't provide to and from ssh information!" >> $LOG $ECHO "Plese don't provide to and from ssh information!" | addDate >> $LOG
$ECHO "No backup performed!" >> $LOG $ECHO "No backup performed!" | addDate >> $LOG
fi fi
# Get date etc. # Get date etc.
@ -140,5 +142,11 @@ fi
# Create daily backups # Create daily backups
backup "daily/$($DATE +"%A")/$HOUR" 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