getRemoteBackup.sh 2.5 KB

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