2017-07-21 17:08:44 +02:00
|
|
|
#!/bin/bash
|
2017-07-22 17:14:35 +02:00
|
|
|
## Create by Stefan Sterz <hi@sterzy.com> in 2017
|
2017-07-21 17:08:44 +02:00
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
## General Settings
|
|
|
|
SOURCES=(/root /etc /home /boot) # What folder should be saved?
|
|
|
|
TARGET="/home/sterzy/backup" # Where should they be saved? (absolute path!)
|
|
|
|
EXCLUDEFROM="" # What files are supposed to be excluded?
|
|
|
|
MAILADRESS="" # To which email adress should the logs be sent?
|
|
|
|
LISTPACKAGES="" # Change to non empty string to save a list of
|
|
|
|
# installed packages!
|
|
|
|
LOG=$0.log # Log file name & location
|
|
|
|
RSYNCCONF=(--delete) # Additional rsync flags
|
2017-07-21 17:08:44 +02:00
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
## SSH Settings
|
|
|
|
TOSSH="" # What ssh server stores the backups?
|
|
|
|
FROMSSH="" # From what ssh server should the files be
|
|
|
|
# backuped?
|
|
|
|
# For several reasons TOSSH and FROMSSH cannot
|
|
|
|
# be used at the same time!
|
2017-07-22 17:40:50 +02:00
|
|
|
TOSSHUSER="" # The ssh user of the system on which the
|
|
|
|
# backup will be created
|
|
|
|
TOSSHPORT=22 # The ssh port of the system on which the
|
|
|
|
# backup will be created
|
|
|
|
FROMSSHUSER="" # The ssh user of the system from which the
|
|
|
|
# files will be backed up
|
|
|
|
FROMSSHPORT=22 # The ssh port of the system from which the
|
|
|
|
# files will be backed up
|
2017-07-21 20:53:13 +02:00
|
|
|
|
2017-07-21 17:08:44 +02:00
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
## Command variables,
|
|
|
|
# These should be fine, but if something doesn't work these can be adjusted
|
2017-07-21 17:08:44 +02:00
|
|
|
MOUNT="/bin/mount"; FGREP="/bin/fgrep"; SSH="/usr/bin/ssh"
|
2017-07-21 20:53:13 +02:00
|
|
|
LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date"; MKDIR="/bin/mkdir"
|
2017-07-21 17:08:44 +02:00
|
|
|
DPKG="/usr/bin/dpkg"; AWK="/usr/bin/awk"; MAIL="/usr/bin/mail"
|
|
|
|
CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync"
|
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
## Helpers
|
|
|
|
LAST="last"; INC="--link-dest=$TARGET/$LAST"
|
2017-07-21 17:08:44 +02:00
|
|
|
|
2017-07-22 12:28:42 +02:00
|
|
|
# Format log information
|
|
|
|
function formatLog ()
|
2017-07-22 12:15:08 +02:00
|
|
|
{
|
|
|
|
while IFS= read -r line; do
|
|
|
|
printf "$($DATE):\t$line\n"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-07-22 12:28:42 +02:00
|
|
|
# Backup function
|
|
|
|
# Creates a backup of the folders provides by $SOURCES in $TARGET in the sub
|
|
|
|
# directory provided by the first parameter ($1)
|
2017-07-21 20:53:13 +02:00
|
|
|
function backup ()
|
|
|
|
{
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "Creating backup in \"$TOSSH:$TARGET$1\"..." 2>&1 | formatLog >> $LOG
|
2017-07-21 17:08:44 +02:00
|
|
|
|
2017-08-03 21:55:08 +02:00
|
|
|
if [ -n "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then
|
2017-08-07 10:14:09 +02:00
|
|
|
$ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR 'n' ' '" 2>&1 | formatLog >> $LOG
|
|
|
|
$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' ' >> $LOG 2>&1
|
2017-07-21 17:08:44 +02:00
|
|
|
fi
|
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
# Create folders that don't exist
|
|
|
|
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$S $TOSSH $MKDIR -p $TARGET$1" 2>&1 | formatLog >> $LOG
|
|
|
|
$S $TOSSH "$MKDIR -p \"$TARGET\"$1" 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 20:53:13 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
|
|
|
fi
|
|
|
|
elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$MKDIR -p $TARGET$1" 2>&1 | formatLog >> $LOG
|
|
|
|
$MKDIR -p "$TARGET"$1 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 20:53:13 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
|
|
|
fi
|
2017-07-21 17:08:44 +02:00
|
|
|
fi
|
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
# Backup files
|
|
|
|
for SOURCE in "${SOURCES[@]}"
|
2017-07-21 17:08:44 +02:00
|
|
|
do
|
2017-08-03 21:55:08 +02:00
|
|
|
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$1\" $INC --exclude-from=\"$EXCLUDEFROM\"" 2>&1 | formatLog >> $LOG
|
|
|
|
$RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC --exclude-from="$EXCLUDEFROM" 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 17:08:44 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
2017-07-21 20:53:13 +02:00
|
|
|
fi
|
2017-08-03 21:55:08 +02:00
|
|
|
elif [ -n "$S" ] && [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC --exclude-from=\"$EXCLUDEFROM\"" 2>&1 | formatLog >> $LOG
|
|
|
|
$RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from="$EXCLUDEFROM" 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 17:08:44 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
2017-07-21 20:53:13 +02:00
|
|
|
fi
|
|
|
|
elif [-z "$S"] && [ -z "$TOSSH" ] && [ -z "$FROMSSh" ]; then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC --exclude-from=\"$EXCLUDEFROM\"" 2>&1 | formatLog >> $LOG
|
|
|
|
$RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC --exclude-from="$EXCLUDEFROM" 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 17:08:44 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
2017-07-21 20:53:13 +02:00
|
|
|
fi
|
2017-07-21 17:08:44 +02:00
|
|
|
fi
|
2017-07-21 20:53:13 +02:00
|
|
|
done
|
2017-07-21 17:08:44 +02:00
|
|
|
|
2017-07-21 20:53:13 +02:00
|
|
|
# Create a link to the latest backup
|
|
|
|
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" 2>&1 | formatLog >> $LOG
|
|
|
|
$S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 17:08:44 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
2017-07-21 20:53:13 +02:00
|
|
|
fi
|
|
|
|
elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "$LN -nsf $TARGET$1 $TARGET$LAST" 2>&1 | formatLog >> $LOG
|
|
|
|
$LN -nsf "$TARGET"$1 "$TARGET"$LAST 2>&1 | formatLog >> $LOG 2>&1
|
2017-07-21 17:08:44 +02:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
ERROR=1
|
|
|
|
fi
|
|
|
|
fi
|
2017-07-21 20:53:13 +02:00
|
|
|
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "Done creating backup in \"$TOSSH:$TARGET$1\"" 2>&1 | formatLog >> $LOG
|
2017-07-21 20:53:13 +02:00
|
|
|
}
|
|
|
|
|
2017-07-22 12:15:08 +02:00
|
|
|
# Clear log
|
|
|
|
$ECHO -n "" > $LOG
|
|
|
|
|
2017-07-22 12:28:42 +02:00
|
|
|
# Make sure the provided path is absolute
|
2017-07-21 20:53:13 +02:00
|
|
|
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
|
|
|
|
TARGET=$TARGET/
|
|
|
|
fi
|
|
|
|
|
2017-07-22 12:28:42 +02:00
|
|
|
# Handle SSH info
|
2017-07-21 20:53:13 +02:00
|
|
|
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
|
2017-07-21 17:08:44 +02:00
|
|
|
ERROR=1
|
2017-08-06 22:31:16 +02:00
|
|
|
$ECHO "Plese don't provide to and from ssh information!" 2>&1 | formatLog >> $LOG
|
|
|
|
$ECHO "No backup performed!" 2>&1 | formatLog >> $LOG
|
2017-08-03 21:50:59 +02:00
|
|
|
exit 1
|
2017-07-21 17:08:44 +02:00
|
|
|
fi
|
2017-07-21 20:53:13 +02:00
|
|
|
|
|
|
|
# Get date etc.
|
|
|
|
WEEKDAY=$($DATE +"%u") # %a = weekday as number
|
|
|
|
DAY=$($DATE +"%d") # %d = day in month as number
|
|
|
|
MONTH=$($DATE +%m) # %m = month as number
|
|
|
|
HOUR=$($DATE +"%H") # %H = hour as number
|
|
|
|
|
|
|
|
# Create a backup every first day in a month
|
2017-07-22 11:28:32 +02:00
|
|
|
if [[ $DAY = 01 ]]; then
|
2017-07-23 11:39:49 +02:00
|
|
|
backup "monthly/$MONTH-$($DATE +"%B")"
|
2017-07-21 20:53:13 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Create a special backup approx every week
|
|
|
|
if [[ $WEEKDAY = 7 ]]; then
|
2017-07-22 17:14:35 +02:00
|
|
|
let WEEK=$($DATE +"%W")%5 # save past 5 weeks
|
2017-07-21 20:53:13 +02:00
|
|
|
backup "weekly/$WEEK"
|
2017-07-21 17:08:44 +02:00
|
|
|
fi
|
2017-07-21 20:53:13 +02:00
|
|
|
|
|
|
|
# Create daily backups
|
2017-07-23 11:39:49 +02:00
|
|
|
backup "daily/$WEEKDAY-$($DATE +"%A")/$HOUR"
|
2017-07-21 20:53:13 +02:00
|
|
|
|
2017-07-22 12:28:42 +02:00
|
|
|
# Send email if an adress was provided
|
2017-08-03 21:50:59 +02:00
|
|
|
if [ -n "$MAILADRESS" ]; then
|
2017-07-22 12:15:08 +02:00
|
|
|
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
|