From f253018def3eaa4cf77d92d7bf5e9cb47f88fbd4 Mon Sep 17 00:00:00 2001 From: Stefan Sterz Date: Fri, 21 Jul 2017 17:08:44 +0200 Subject: [PATCH] add inpsiration to Readme --- README.md | 9 +++-- backup.sh | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 2 deletions(-) create mode 100755 backup.sh diff --git a/README.md b/README.md index e2e7b9c..a9dd1b7 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ -# backup +# Backup +A flexible backup script for backing up linux system using rsync, ssh, cron and other common shell commands. + +## Inspiration +[Backup mit RSYNC](https://wiki.ubuntuusers.de/Skripte/Backup_mit_RSYNC/) +[rsync linux backup script](https://ctaas.de/rsync.htm) +[Easy Automated Snapshot-Style Backups with Linux and Rsync](http://www.mikerubel.org/computers/rsync_snapshots/) -A backup script for linux systems using rsync and ssh. \ No newline at end of file diff --git a/backup.sh b/backup.sh new file mode 100755 index 0000000..213dc7b --- /dev/null +++ b/backup.sh @@ -0,0 +1,106 @@ +#!/bin/bash +# Simple backup with rsync +# local-mode, tossh-mode, fromssh-mode + +SOURCES=(/root /etc /home /boot ) +TARGET="/home/sterzy/backup" + +# 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="hi@sterzy.com" + +SSHUSER="sterzy" +#FROMSSH="fromssh-server" +TOSSH="backup-pi" +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:$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC" >> $LOG + $RSYNC -e "$S" -avR "$FROMSSH:\"$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 \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$TODAY\" $INC " >> $LOG + $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$TODAY" $INC >> $LOG 2>&1 + if [ $? -ne 0 ]; then + ERROR=1 + fi + fi + if [ -z "$S" ]; then + $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC" >> $LOG + $RSYNC -avR "$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