more documentation

This commit is contained in:
Shannon Sterz 2017-07-22 12:28:42 +02:00
parent 9f9329a04d
commit e848189f02

View file

@ -32,32 +32,36 @@ 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 () # Format log information
function formatLog ()
{ {
while IFS= read -r line; do while IFS= read -r line; do
printf "$($DATE):\t$line\n" printf "$($DATE):\t$line\n"
done 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 () 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 if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then
$ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR '\n' ' '" | addDate >> $LOG $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' ' ' | addDate >> $LOG 2>&1 $DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' ' | formatLog >> $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" | addDate >> $LOG $ECHO "$S $TOSSH $MKDIR -p $TARGET$1" | formatLog >> $LOG
$S $TOSSH "$MKDIR -p \"$TARGET\"$1" | addDate >> $LOG 2>&1 $S $TOSSH "$MKDIR -p \"$TARGET\"$1" | formatLog >> $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" | addDate >> $LOG $ECHO "$MKDIR -p $TARGET$1" | formatLog >> $LOG
$MKDIR -p "$TARGET"$1 | addDate >> $LOG 2>&1 $MKDIR -p "$TARGET"$1 | formatLog >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
@ -67,20 +71,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 " | addDate >> $LOG $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 | addDate >> $LOG 2>&1 $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from=$EXCLUDEFROM | formatLog >> $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" | addDate >> $LOG $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 | addDate >> $LOG 2>&1 $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | formatLog >> $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" | addDate >> $LOG $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" | formatLog >> $LOG
$RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | addDate >> $LOG 2>&1 $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from=$EXCLUDEFROM | formatLog >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
@ -89,37 +93,39 @@ 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" | addDate >> $LOG $ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" | formatLog >> $LOG
$S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" | addDate >> $LOG 2>&1 $S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" | formatLog >> $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" | addDate >> $LOG $ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" | formatLog >> $LOG
$LN -nsf "$TARGET"$1 "$TARGET"$LAST | addDate >> $LOG 2>&1 $LN -nsf "$TARGET"$1 "$TARGET"$LAST | formatLog >> $LOG 2>&1
if [ $? -ne 0 ]; then if [ $? -ne 0 ]; then
ERROR=1 ERROR=1
fi fi
fi fi
$ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" | addDate >> $LOG $ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" | formatLog >> $LOG
} }
# Clear log # Clear log
$ECHO -n "" > $LOG $ECHO -n "" > $LOG
# Make sure the provided path is absolute
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
TARGET=$TARGET/ TARGET=$TARGET/
fi fi
# Handle SSH info
if [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then if [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
S="$SSH -p $TOSSHPORT -l $TOSSHUSER" S="$SSH -p $TOSSHPORT -l $TOSSHUSER"
elif [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then 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!" | addDate >> $LOG $ECHO "Plese don't provide to and from ssh information!" | formatLog >> $LOG
$ECHO "No backup performed!" | addDate >> $LOG $ECHO "No backup performed!" | formatLog >> $LOG
fi fi
# Get date etc. # Get date etc.
@ -142,6 +148,7 @@ fi
# Create daily backups # Create daily backups
backup "daily/$($DATE +"%A")/$HOUR" backup "daily/$($DATE +"%A")/$HOUR"
# Send email if an adress was provided
if [ -n MAILADRESS ]; then if [ -n MAILADRESS ]; then
if [ $ERROR ]; then if [ $ERROR ]; then
$MAIL -s "[Backup] error occured during backup - $LOG" $MAILADRESS < $LOG $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 $MAIL -s "[Backup] backup created successfully - $LOG" $MAILADRESS < $LOG
fi fi
fi fi