From e848189f02cfc8036f779349456def45cb9eb263 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Sat, 22 Jul 2017 12:28:42 +0200 Subject: [PATCH] more documentation --- backup.sh | 50 ++++++++++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/backup.sh b/backup.sh index cabc9a7..a083605 100755 --- a/backup.sh +++ b/backup.sh @@ -32,32 +32,36 @@ CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync" ## Helpers LAST="last"; INC="--link-dest=$TARGET/$LAST" -function addDate () +# Format log information +function formatLog () { while IFS= read -r line; do printf "$($DATE):\t$line\n" done } +# Backup function +# Creates a backup of the folders provides by $SOURCES in $TARGET in the sub +# directory provided by the first parameter ($1) function backup () { - $ECHO "Creating backup in \"$TOSSH:$TARGET$1\"..." | addDate >> $LOG + $ECHO "Creating backup in \"$TOSSH:$TARGET$1\"..." | formatLog >> $LOG if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then - $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 + $ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR '\n' ' '" | formatLog >> $LOG + $DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' ' | formatLog >> $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" | addDate >> $LOG - $S $TOSSH "$MKDIR -p \"$TARGET\"$1" | addDate >> $LOG 2>&1 + $ECHO "$S $TOSSH $MKDIR -p $TARGET$1" | formatLog >> $LOG + $S $TOSSH "$MKDIR -p \"$TARGET\"$1" | formatLog >> $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" | addDate >> $LOG - $MKDIR -p "$TARGET"$1 | addDate >> $LOG 2>&1 + $ECHO "$MKDIR -p $TARGET$1" | formatLog >> $LOG + $MKDIR -p "$TARGET"$1 | formatLog >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi @@ -67,20 +71,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 " | addDate >> $LOG - $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 + $ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$1\" $INC " | formatLog >> $LOG + $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from=$EXCLUDEFROM | formatLog >> $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" | addDate >> $LOG - $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 + $ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" | formatLog >> $LOG + $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | formatLog >> $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" | addDate >> $LOG - $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 + $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" | formatLog >> $LOG + $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | formatLog >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi @@ -89,37 +93,39 @@ 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" | addDate >> $LOG - $S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" | addDate >> $LOG 2>&1 + $ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" | formatLog >> $LOG + $S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" | formatLog >> $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" | addDate >> $LOG - $LN -nsf "$TARGET"$1 "$TARGET"$LAST | addDate >> $LOG 2>&1 + $ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" | formatLog >> $LOG + $LN -nsf "$TARGET"$1 "$TARGET"$LAST | formatLog >> $LOG 2>&1 if [ $? -ne 0 ]; then ERROR=1 fi fi - $ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" | addDate >> $LOG + $ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" | formatLog >> $LOG } # Clear log $ECHO -n "" > $LOG +# Make sure the provided path is absolute if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then TARGET=$TARGET/ fi +# Handle SSH info if [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then S="$SSH -p $TOSSHPORT -l $TOSSHUSER" 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!" | addDate >> $LOG - $ECHO "No backup performed!" | addDate >> $LOG + $ECHO "Plese don't provide to and from ssh information!" | formatLog >> $LOG + $ECHO "No backup performed!" | formatLog >> $LOG fi # Get date etc. @@ -142,6 +148,7 @@ fi # Create daily backups backup "daily/$($DATE +"%A")/$HOUR" +# Send email if an adress was provided if [ -n MAILADRESS ]; then if [ $ERROR ]; then $MAIL -s "[Backup] error occured during backup - $LOG" $MAILADRESS < $LOG @@ -149,4 +156,3 @@ if [ -n MAILADRESS ]; then $MAIL -s "[Backup] backup created successfully - $LOG" $MAILADRESS < $LOG fi fi -