wiki:linux/Backup
#!/bin/bash
# Simple backup with rsync (version 2011-03-10)
# local-mode, tossh-mode, fromssh-mode

sudo mount -t cifs //192.168.0.100/cloud /srv -o username=[USERNAME] >/dev/null 2>/dev/null

if [ -z "$(ls /srv)" ]; then
    echo -e "\nBackupziel auf Server nicht bereit,\nbitte nochmal starten...\n\n"
    read -p "(Taste drücken um Fenster zu schließen!)" -n1 -s
    exit 1
fi

SOURCES="/home/[USERNAME]"
# path on server if `TOSSH` is set
# otherwise path to local path
TARGET="/srv/backup/"
# for testing purposes with `--dry-run` option
#RSYNCCONF="--exclude-from=/home/[USERNAME]/.local/share/scripts/exclude_[USERNAME] --dry-run"
RSYNCCONF="--exclude-from=/home/[USERNAME]/.local/share/scripts/exclude_[USERNAME]"
# to check if local mountpoint is ready
MOUNTPOINT="/cloud"
PACKAGES=0	
MONTHROTATE=1
#MAILREC="user@localhost"

### 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=../$LAST"

LOG='/home/[USERNAME]/backup_[USERNAME].log'
$DATE > $LOG

if [ $PACKAGES -eq 1 ] && [ -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

MOUNTED=$($MOUNT | $FGREP "$MOUNTPOINT");
if [ -z "$MOUNTPOINT" ] || [ -n "$MOUNTED" ]; then
  if [ $MONTHROTATE -eq 1 ]; then
    TODAY=$($DATE +%d)
  else
    TODAY=$($DATE +%y%m%d)
  fi

  if [ "$SSHUSER" ] && [ "$SSHPORT" ]; then
    S="$SSH -p $SSHPORT -l $SSHUSER";
  fi

  for SOURCE in $($ECHO $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

sudo umount /srv
Last modified 4 years ago Last modified on Apr 6, 2015, 4:58:55 PM