getRemoteBackup.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/bin/bash
  2. #Read the backup folders and configuration variables from the config script
  3. . $1
  4. #Local variables
  5. globalErrors=0
  6. logFilePath="${logPath}${logFileName}"
  7. touch $tmpLogFilePath
  8. touch $logFilePath
  9. echo $logFilePath
  10. #Terminate if the last backup has not finished yet
  11. if pidof -o %PPID -x "$0" > /dev/null; then
  12. echo "Backup job already running. Exiting..."
  13. exit
  14. fi
  15. #Run the backup jobs as defined in the backup_folders array from the config
  16. echo "Starting backup job(s)" | tee $tmpLogFilePath
  17. for sourceFolder in "${!backup_folders[@]}"
  18. do
  19. mkdir -p ${backup_folders[$sourceFolder]}
  20. while [ "$retryAttempts" -ge "0" ]
  21. do
  22. echo "Starting Job" | tee -a $tmpLogFilePath
  23. echo "SRC: ${sourceFolder}" | tee -a $tmpLogFilePath
  24. echo "DEST: ${backup_folders[$sourceFolder]}" | tee -a $tmpLogFilePath
  25. date | tee -a $tmpLogFilePath
  26. #SSH Host settings need to be set in the ssh client config!
  27. rsync --delete --out-format="%t %f %''b" -avz $sourceFolder ${backup_folders[$sourceFolder]} | tee -a $tmpLogFilePath 2>&1
  28. rsyncExitCode=$?
  29. echo "Ending backup job" | tee -a $tmpLogFilePath
  30. date | tee -a $tmpLogFilePath
  31. echo "" | tee -a $tmpLogFilePath
  32. if [ $rsyncExitCode -eq 0 ]; then
  33. break
  34. else
  35. echo "Error: rsync exited with code $rsyncExitCode. Retrying..." | tee -a $tmpLogFilePath
  36. retryAttempts=$((retryAttempts-1))
  37. globalErrors=$((globalErrors+1))
  38. fi
  39. done
  40. done
  41. #copy over and delete older logs
  42. cat $tmpLogFilePath > $logFilePath
  43. find $logPath -type f -mtime +$rotationDays -name '*.log' -execdir rm -- '{}' \;
  44. #Mail Notifications
  45. if [ $globalErrors -eq 0 ]; then
  46. #send mail for success
  47. echo -e "To:$mailTo\nFrom:$mailFrom\nSubject:Backup SUCCESSFUL on $(date +'%c')\n\n$(cat $tmpLogFilePath)" | sendmail $mailTo
  48. else
  49. #send mail for failure
  50. echo -e "To:$mailTo\nFrom:$mailFrom\nSubject:Backup FAILED on $(date +'%c')\n\n$(cat $tmpLogFilePath)" | sendmail $mailTo
  51. fi
  52. #delete tmp file
  53. rm $tmpLogFilePath