code.fastix.org

Dateiansicht:

Datei:Projekte -> Linux,bash:Platzsparendes nächtliches Backup -> backup-nightly.sh
md5:37f2ea2c5a8bae9b14e674b47ae2238e
sha1:2fc1c4436428ef85f9f2b37b6bfad3c0e431367d
Download-Link:Download
  1. #!/bin/bash
  2.  
  3. ### Konfiguration:
  4. # Wovon soll ein Backup gemacht werden?
  5. sourceDir='/';
  6.  
  7. # Wohin soll das Backup geschrieben werden?
  8. backupDir='/data/backup';
  9.  
  10. # Was soll nicht im Backup sein? (Datei)
  11. backupExludesFile='/root/bin/backup.exludes';
  12.  
  13. # Was soll dennoch im Backup sein? (Datei)
  14. backupIncludesFile='/root/bin/backup.includes';
  15.  
  16. # Name des Backup. Wird Bestandteil des Dateinamens:
  17. backupName='nightly';
  18.  
  19. # Wie viele allte Backups sollen behalten werden:
  20. maxOldBackups=28;
  21.  
  22. # Dienste, die in der angegebenen Reihenfolge vor dem Backup anzuhalten sind:
  23. servicesStopBevore=(mysql);
  24.  
  25. # Dienste, die in der angegebenen Teihenfolge nach dem Backup zu starten sind:
  26. servicesStartAfter=(mysql);
  27.  
  28. ### Wenn ab hier geändert wird, muss das Skript angepasst werden
  29. YMD="$(date +%F)";
  30.  
  31. backupDateName="${YMD}-${backupName}";
  32. backupRegex="^2[0-9]{3}-[0-9]{2}-[0-9]{2}-${backupName}$";
  33.  
  34. backupLogFile="${YMD}-${backupName}.log";
  35. backupErrorLogFile="${YMD}-${backupName}.err.log";
  36.  
  37.  
  38. if [ -d "${backupDir}" ]; then
  39.  
  40.     oldDir=$(pwd);
  41.     cd ${backupDir};
  42.  
  43.     date > "${backupLogFile}";
  44.     if [ -f "${backupLogFile}" ]; then
  45.  
  46.         date > "${backupErrorLogFile}";
  47.  
  48.         ### Löschen der alten Backups:
  49.         countBackups=$(ls -d * | grep -P "${backupRegex}" | wc -l);
  50.         if [ ${countBackups} -gt ${maxOldBackups} ]; then
  51.             let newcounter=0;
  52.             for dir in $(ls -d * | grep -P "${backupRegex}" | sort); do
  53.                 newCounter=$(($newCounter+1));
  54.                 rest=$((${countBackups}-${maxOldBackups}));
  55.                 if [ ${newCounter} -le ${rest} ]; then
  56.                     rm -rf "${dir}" 2>> "${backupErrorLogFile}" &&
  57.                     echo "Backup gelöscht: ${dir}" >> "${backupLogFile}";
  58.                     oldDate=$(echo "${dir}" | cut  -d '-' -f 1,2,3);
  59.                     oldBackupLogFile="${oldDate}-${backupName}.log";
  60.                     rm "${oldBackupLogFile}" &&
  61.                     echo "altes Logfile gelöscht: ${oldBackupLogFile}" >> "${backupLogFile}";
  62.                     oldBackupLogFile="${oldDate}-${backupName}.err.log";
  63.                     rm "${oldBackupLogFile}" &&
  64.                     echo "altes Error-Logfile gelöscht: ${oldBackupLogFile}" >> "${backupLogFile}";
  65.                 fi
  66.             done
  67.         fi
  68.  
  69.         ### Finden des letzten Backups:
  70.  
  71.         lastBackup="$(ls -d * | grep -P "${backupRegex}" | sort | tail -n1)";
  72.         if [ "${lastBackup}" = "${backupDateName}" ]; then
  73.             lastBackup="";
  74.         fi
  75.  
  76.         ### Anlegen des neuen Backups:
  77.  
  78.         ## Stoppen der Dienste
  79.         for service in ${servicesStopBevore[@]}; do
  80.             service ${service} stop;
  81.         done;
  82.  
  83.         if [ -z ${lastBackup} ]; then
  84.             # erstes Backup:
  85.             rsync -avu --exclude-from "${backupExludesFile}" --include-from "${backupIncludesFile}" "${sourceDir}" "${backupDir}/${backupDateName}" >> "${backupLogFile}" 2>> "${backupErrorLogFile}";
  86.         else
  87.             # wenn schon ein Backup existiert:
  88.             rsync -avuH --link-dest="${backupDir}/${lastBackup}" --exclude-from "${backupExludesFile}" --include-from "${backupIncludesFile}" "${sourceDir}" "${backupDir}/${backupDateName}" >> "${backupLogFile}" 2>> "${backupErrorLogFile}";
  89.         fi
  90.  
  91.         ## Starten der Dienste
  92.         for service in ${servicesStartAfter[@]}; do
  93.             service ${service} start;
  94.         done;
  95.  
  96.         cd "${oldDir}";
  97.  
  98.     else
  99.         logger -t "nightly-backup" "Fatal: Backup-Dir ${backupDir} kann nicht beschrieben werden.";
  100.     fi
  101. else
  102.     logger -t "nightly-backup" "Fatal: Backup-Dir ${backupDir} nicht verfügbar!";
  103. fi