create initial script
This commit is contained in:
parent
bce0c3abc5
commit
d319d52c0d
1 changed files with 127 additions and 89 deletions
168
backup.sh
Executable file → Normal file
168
backup.sh
Executable file → Normal file
|
@ -1,106 +1,144 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# Simple backup with rsync
|
|
||||||
# local-mode, tossh-mode, fromssh-mode
|
|
||||||
|
|
||||||
SOURCES=(/root /etc /home /boot )
|
## General Settings
|
||||||
TARGET="/home/sterzy/backup"
|
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
|
||||||
|
|
||||||
# edit or comment with "#"
|
## SSH Settings
|
||||||
#LISTPACKAGES=listdebianpackages # local-mode and tossh-mode
|
TOSSH="" # What ssh server stores the backups?
|
||||||
MONTHROTATE=monthrotate # use DD instead of YYMMDD
|
FROMSSH="" # From what ssh server should the files be
|
||||||
|
# backuped?
|
||||||
|
# For several reasons TOSSH and FROMSSH cannot
|
||||||
|
# be used at the same time!
|
||||||
|
TOSSHUSER="" # The ssh user
|
||||||
|
TOSSHPORT=22 # The ssh port
|
||||||
|
FROMSSHUSER="" # The ssh user
|
||||||
|
FROMSSHPORT=22 # The ssh port
|
||||||
|
|
||||||
RSYNCCONF=(--delete)
|
|
||||||
#MOUNTPOINT="/media/daten" # check local mountpoint
|
|
||||||
MAILREC="hi@sterzy.com"
|
|
||||||
|
|
||||||
SSHUSER="sterzy"
|
|
||||||
#FROMSSH="fromssh-server"
|
|
||||||
TOSSH="backup-pi"
|
|
||||||
SSHPORT=22
|
|
||||||
|
|
||||||
### do not edit ###
|
|
||||||
|
|
||||||
|
## Command variables,
|
||||||
|
# These should be fine, but if something doesn't work these can be adjusted
|
||||||
MOUNT="/bin/mount"; FGREP="/bin/fgrep"; SSH="/usr/bin/ssh"
|
MOUNT="/bin/mount"; FGREP="/bin/fgrep"; SSH="/usr/bin/ssh"
|
||||||
LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date"; RM="/bin/rm"
|
LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date"; MKDIR="/bin/mkdir"
|
||||||
DPKG="/usr/bin/dpkg"; AWK="/usr/bin/awk"; MAIL="/usr/bin/mail"
|
DPKG="/usr/bin/dpkg"; AWK="/usr/bin/awk"; MAIL="/usr/bin/mail"
|
||||||
CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync"
|
CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync"
|
||||||
|
|
||||||
|
## Helpers
|
||||||
LAST="last"; INC="--link-dest=$TARGET/$LAST"
|
LAST="last"; INC="--link-dest=$TARGET/$LAST"
|
||||||
|
|
||||||
LOG=$0.log
|
function backup ()
|
||||||
|
{
|
||||||
$DATE > $LOG
|
$DATE > $LOG
|
||||||
|
|
||||||
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
|
|
||||||
TARGET=$TARGET/
|
|
||||||
fi
|
|
||||||
|
|
||||||
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' ' '" >> $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' ' ' >> $LOG 2>&1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$MOUNTPOINT" ]; then
|
# Create folders that don't exist
|
||||||
MOUNTED=$($MOUNT | $FGREP "$MOUNTPOINT");
|
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
||||||
fi
|
$ECHO "$S $TOSSH $MKDIR -p $TARGET$1" >> $LOG
|
||||||
|
$S $TOSSH "$MKDIR -p \"$TARGET\"$1" >> $LOG 2>&1
|
||||||
if [ -z "$MOUNTPOINT" ] || [ "$MOUNTED" ]; then
|
if [ $? -ne 0 ]; then
|
||||||
if [ -z "$MONTHROTATE" ]; then
|
ERROR=1
|
||||||
TODAY=$($DATE +%y%m%d)
|
fi
|
||||||
else
|
elif ( [ -n "$S" ] && [ -n "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
|
||||||
TODAY=$($DATE +%d)
|
$ECHO "$MKDIR -p $TARGET$1" >> $LOG
|
||||||
fi
|
$MKDIR -p "$TARGET"$1 >> $LOG 2>&1
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
if [ "$SSHUSER" ] && [ "$SSHPORT" ]; then
|
ERROR=1
|
||||||
S="$SSH -p $SSHPORT -l $SSHUSER";
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# Backup files
|
||||||
for SOURCE in "${SOURCES[@]}"
|
for SOURCE in "${SOURCES[@]}"
|
||||||
do
|
do
|
||||||
if [ "$S" ] && [ "$FROMSSH" ] && [ -z "$TOSSH" ]; then
|
if [ -n $"S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
||||||
$ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC" >> $LOG
|
$ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$1\" $INC " >> $LOG
|
||||||
$RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
|
$RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$1" $INC >> $LOG 2>&1
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
ERROR=1
|
ERROR=1
|
||||||
fi
|
fi
|
||||||
fi
|
elif [ -n $"S" ] && [ -z "$TOSSH" ] && [ -n "$FROMSSH" ]; then
|
||||||
if [ "$S" ] && [ "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
$ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" >> $LOG
|
||||||
$ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$TODAY\" $INC " >> $LOG
|
$RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$1 $INC >> $LOG 2>&1
|
||||||
$RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$TODAY" $INC >> $LOG 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
ERROR=1
|
ERROR=1
|
||||||
fi
|
fi
|
||||||
fi
|
elif [-z "$S"] && [ -z "$TOSSH" ] && [ -z "$FROMSSh" ]; then
|
||||||
if [ -z "$S" ]; then
|
$ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$1 $INC" >> $LOG
|
||||||
$ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC" >> $LOG
|
$RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$1 $INC >> $LOG 2>&1
|
||||||
$RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
ERROR=1
|
ERROR=1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
if [ "$S" ] && [ "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
# Create a link to the latest backup
|
||||||
$ECHO "$SSH -p $SSHPORT -l $SSHUSER $TOSSH $LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG
|
if [ -n "$S" ] && [ -n "$TOSSH" ] && [ -z "$FROMSSH" ]; then
|
||||||
$SSH -p $SSHPORT -l $SSHUSER $TOSSH "$LN -nsf \"$TARGET\"$TODAY \"$TARGET\"$LAST" >> $LOG 2>&1
|
$ECHO "$S $TOSSH $LN -nsf $TARGET$1 $TARGET$LAST" >> $LOG
|
||||||
|
$S $TOSSH "$LN -nsf \"$TARGET\"$1 \"$TARGET\"$LAST" >> $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
|
||||||
if [ $? -ne 0 ]; then
|
if [ $? -ne 0 ]; then
|
||||||
ERROR=1
|
ERROR=1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
if ( [ "$S" ] && [ "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] ); then
|
|
||||||
$ECHO "$LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG
|
|
||||||
$LN -nsf "$TARGET"$TODAY "$TARGET"$LAST >> $LOG 2>&1
|
|
||||||
if [ $? -ne 0 ]; then
|
|
||||||
ERROR=1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
$ECHO "$MOUNTPOINT not mounted" >> $LOG
|
|
||||||
ERROR=1
|
|
||||||
fi
|
|
||||||
$DATE >> $LOG
|
$DATE >> $LOG
|
||||||
if [ -n "$MAILREC" ]; then
|
|
||||||
if [ $ERROR ];then
|
if [ -n MAILADRESS ]; then
|
||||||
$MAIL -s "Error Backup $LOG" $MAILREC < $LOG
|
if [ -n $ERROR ]; then
|
||||||
|
$MAIL -s "[Backup] error occured during backup - $LOG" $MAILADRESS < $LOG
|
||||||
else
|
else
|
||||||
$MAIL -s "Backup $LOG" $MAILREC < $LOG
|
$MAIL -s "[Backup] backup created successfully - $LOG" $MAILADRESS < $LOG
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
|
||||||
|
TARGET=$TARGET/
|
||||||
|
fi
|
||||||
|
|
||||||
|
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!" >> $LOG
|
||||||
|
$ECHO "No backup performed!" >> $LOG
|
||||||
|
fi
|
||||||
|
|
||||||
|
# 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
|
||||||
|
if [[ $DAY = 1 ]]; then
|
||||||
|
backup "monthly/$MONTH"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create a special backup approx every week
|
||||||
|
if [[ $WEEKDAY = 7 ]]; then
|
||||||
|
let WEEK=($DAY+6)/7
|
||||||
|
backup "weekly/$WEEK"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create daily backups
|
||||||
|
backup "daily/$DAY-$HOUR"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue