getRemoteBackup.sh 3.1 KB

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