LinuxScrips/backup_scripte/ubuntu_backup.sh

124 lines
3.8 KiB
Bash
Raw Permalink Normal View History

2022-02-01 23:49:29 +01:00
#!/bin/bash
# Simple backup with rsync, taken from Ubuntuusers.com
# local-mode, tossh-mode, fromssh-mode
SOURCES=(/home/arsimael)
CIFSSERVER="WoGehtsHin"
CIFSUSER="User"
CIFSPASSWD="NIET-PASSWORD!"
CIFSRMMOUNT="backup$"
CIFSLOMOUNT="/storage/remote/backup"
TARGET=$(echo $CIFSLOMOUNT/rsyncbackup)
if [ $(dpkg-query -W -f='${Status}' cifs-utils 2>/dev/null | grep -c "ok installed") -eq 0 ]; then apt install -y cifs-utils; fi
if [ $(dpkg-query -W -f='${Status}' rsync 2>/dev/null | grep -c "ok installed") -eq 0 ]; then apt install -y rsync; fi
if [ -d "$TARGET" ]; then
echo "Backup folder exists, skipping creation..."
else
mkdir -p $TARGET
fi
mount -t cifs -o user=$CIFSUSER,password=$CIFSPASSWD //$CIFSSERVER/$CIFSRMMOUNT $TARGET
# edit or comment with "#"
#LISTPACKAGES=listdebianpackages # local-mode and tossh-mode
MONTHROTATE=monthrotate # use DD instead of YYMMDD
RSYNCCONF=(--delete)
#MOUNTPOINT="/media/daten" # check local mountpoint
#MAILREC="user@domain"
#SSHUSER="sshuser"
#FROMSSH="fromssh-server"
#TOSSH="tossh-server"
SSHPORT=22
### do not edit ###
MOUNT="/bin/mount"; FGREP="/bin/fgrep"; SSH="/usr/bin/ssh"
LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date"; RM="/bin/rm"
DPKG="/usr/bin/dpkg"; AWK="/usr/bin/awk"; MAIL="/usr/bin/mail"
CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync"
LAST="last"; INC="--link-dest=$TARGET/$LAST"
LOG=$0.log
$DATE > $LOG
if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
TARGET=$TARGET/
fi
if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then
$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
fi
if [ "$MOUNTPOINT" ]; then
MOUNTED=$($MOUNT | $FGREP "$MOUNTPOINT");
fi
if [ -z "$MOUNTPOINT" ] || [ "$MOUNTED" ]; then
if [ -z "$MONTHROTATE" ]; then
TODAY=$($DATE +%y%m%d)
else
TODAY=$($DATE +%d)
fi
if [ "$SSHUSER" ] && [ "$SSHPORT" ]; then
S="$SSH -p $SSHPORT -l $SSHUSER";
fi
for SOURCE in "${SOURCES[@]}"
do
if [ "$S" ] && [ "$FROMSSH" ] && [ -z "$TOSSH" ]; then
$ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$MOUNTPOINT$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC" >> $LOG
$RSYNC -e "$S" -avR "$FROMSSH:\"$MOUNTPOINT$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
fi
if [ "$S" ] && [ "$TOSSH" ] && [ -z "$FROMSSH" ]; then
$ECHO "$RSYNC -e \"$S\" -avR \"$MOUNTPOINT$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$TODAY\" $INC " >> $LOG
$RSYNC -e "$S" -avR "$MOUNTPOINT$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$TODAY" $INC >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
fi
if [ -z "$S" ]; then
$ECHO "$RSYNC -avR \"$MOUNTPOINT$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC" >> $LOG
$RSYNC -avR "$MOUNTPOINT$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
fi
fi
done
if [ "$S" ] && [ "$TOSSH" ] && [ -z "$FROMSSH" ]; then
$ECHO "$SSH -p $SSHPORT -l $SSHUSER $TOSSH $LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG
$SSH -p $SSHPORT -l $SSHUSER $TOSSH "$LN -nsf \"$TARGET\"$TODAY \"$TARGET\"$LAST" >> $LOG 2>&1
if [ $? -ne 0 ]; then
ERROR=1
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
if [ -n "$MAILREC" ]; then
if [ $ERROR ];then
$MAIL -s "Error Backup $LOG" $MAILREC < $LOG
else
$MAIL -s "Backup $LOG" $MAILREC < $LOG
fi
fi
umount $CIFSLOMOUNT