NoPaste

Rsync Script

von weshalb
Beschreibung:
Rsync Script
Snippet erstellt:
14.11.2018 09:56:56
Snippet wird automatisch gelöscht:
Niemals

Dein Code:
  1. #!/bin/bash
  2. # Simple backup with rsync
  3. # local-mode, tossh-mode, fromssh-mode
  4.  
  5. SOURCES=(/home)
  6. TARGET="/home/mbackup/mbackup"
  7. heute=$(date +%Y-%m-%d)
  8.  
  9.  
  10. # edit or comment with "#"
  11. LISTPACKAGES=listdebianpackages        # local-mode and tossh-mode
  12. MONTHROTATE=monthrotate                 # use DD instead of YYMMDD
  13.  
  14. RSYNCCONF=(--delete)
  15. #MOUNTPOINT="/media/daten"               # check local mountpoint
  16. #MAILREC="user@domain"
  17.  
  18. SSHUSER="mbackup"
  19. #FROMSSH="fromssh-server"
  20. TOSSH="server"
  21. SSHPORT=4892
  22.  
  23. ### do not edit ###
  24.  
  25. MOUNT="/bin/mount"; FGREP="/bin/fgrep"; SSH="/usr/bin/ssh"
  26. LN="/bin/ln"; ECHO="/bin/echo"; DATE="/bin/date"; RM="/bin/rm"
  27. DPKG="/usr/bin/dpkg"; AWK="/usr/bin/awk"; MAIL="/usr/bin/mail"
  28. CUT="/usr/bin/cut"; TR="/usr/bin/tr"; RSYNC="/usr/bin/rsync"
  29. LAST="last"; INC="--link-dest=$TARGET/$LAST"
  30.  
  31. LOG=$0.log
  32. $DATE > $LOG
  33.  
  34. if [ "${TARGET:${#TARGET}-1:1}" != "/" ]; then
  35.   TARGET=$TARGET/
  36. fi
  37.  
  38. if [ "$LISTPACKAGES" ] && [ -z "$FROMSSH" ]; then
  39.   $ECHO "$DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 | $TR '\n' ' '" >> $LOG
  40.   $DPKG --get-selections | $AWK '!/deinstall|purge|hold/'|$CUT -f1 |$TR '\n' ' '  >> $LOG  2>&1
  41. fi
  42.  
  43. if [ "$MOUNTPOINT" ]; then
  44.   MOUNTED=$($MOUNT | $FGREP "$MOUNTPOINT");
  45. fi
  46.  
  47. if [ -z "$MOUNTPOINT" ] || [ "$MOUNTED" ]; then
  48.   if [ -z "$MONTHROTATE" ]; then
  49.     TODAY=$($DATE +%y%m%d)
  50.   else
  51.     TODAY=$($DATE +%d)
  52.   fi
  53.  
  54.   if [ "$SSHUSER" ] && [ "$SSHPORT" ]; then
  55.     S="$SSH -p $SSHPORT -l $SSHUSER";
  56.   fi
  57.  
  58.   for SOURCE in "${SOURCES[@]}"
  59.     do
  60.       if [ "$S" ] && [ "$FROMSSH" ] && [ -z "$TOSSH" ]; then
  61.         $ECHO "$RSYNC -e \"$S\" -avR \"$FROMSSH:$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC"  >> $LOG
  62.         $RSYNC -e "$S" -avR "$FROMSSH:\"$SOURCE\"" "${RSYNCCONF[@]}" "$TARGET"$TODAY $INC >> $LOG 2>&1
  63.         if [ $? -ne 0 ]; then
  64.           ERROR=1
  65.         fi
  66.       fi
  67.       if [ "$S" ]  && [ "$TOSSH" ] && [ -z "$FROMSSH" ]; then
  68.         $ECHO "$RSYNC -e \"$S\" -avR \"$SOURCE\" ${RSYNCCONF[@]} \"$TOSSH:$TARGET$TODAY\" $INC " >> $LOG
  69.         $RSYNC -e "$S" -avR "$SOURCE" "${RSYNCCONF[@]}" "$TOSSH:\"$TARGET\"$TODAY" $INC >> $LOG 2>&1
  70.         if [ $? -ne 0 ]; then
  71.           ERROR=1
  72.         fi
  73.       fi
  74.       if [ -z "$S" ]; then
  75.         $ECHO "$RSYNC -avR \"$SOURCE\" ${RSYNCCONF[@]} $TARGET$TODAY $INC"  >> $LOG
  76.         $RSYNC -avR "$SOURCE" "${RSYNCCONF[@]}" "$TARGET"$TODAY $INC  >> $LOG 2>&1
  77.         if [ $? -ne 0 ]; then
  78.           ERROR=1
  79.         fi
  80.       fi
  81.   done
  82.  
  83.   if [ "$S" ] && [ "$TOSSH" ] && [ -z "$FROMSSH" ]; then
  84.     $ECHO "$SSH -p $SSHPORT -l $SSHUSER $TOSSH $LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG  
  85.     $SSH -p $SSHPORT -l $SSHUSER $TOSSH "$LN -nsf \"$TARGET\"$TODAY \"$TARGET\"$LAST" >> $LOG 2>&1
  86.     if [ $? -ne 0 ]; then
  87.       ERROR=1
  88.     fi
  89.   fi
  90.   if ( [ "$S" ] && [ "$FROMSSH" ] && [ -z "$TOSSH" ] ) || ( [ -z "$S" ] );  then
  91.     $ECHO "$LN -nsf $TARGET$TODAY $TARGET$LAST" >> $LOG
  92.     $LN -nsf "$TARGET"$TODAY "$TARGET"$LAST  >> $LOG 2>&1
  93.     if [ $? -ne 0 ]; then
  94.       ERROR=1
  95.     fi
  96.   fi
  97. else
  98.   $ECHO "$MOUNTPOINT not mounted" >> $LOG
  99.   ERROR=1
  100. fi
  101. $DATE >> $LOG
  102. if [ -n "$MAILREC" ]; then
  103.   if [ $ERROR ];then
  104.     $MAIL -s "Error Backup $LOG" $MAILREC < $LOG
  105.   else
  106.     $MAIL -s "Backup $LOG" $MAILREC < $LOG
  107.   fi
  108. fi

Quellcode

Hier kannst du den Code kopieren und ihn in deinen bevorzugten Editor einfügen. Alternativ kannst du den gesamten Eintrag auch als Datei herunterladen.