Просмотр исходного кода

Adding mail notification in case of a script failure
Improving reliability
Moving log rotation to beginning to avoid accumulation of log files
when the script crashes

sebastian 2 недель назад
Родитель
Сommit
0183ad9836
2 измененных файлов с 46 добавлено и 10 удалено
  1. 7 0
      backupMgr/Readme.md
  2. 39 10
      backupMgr/getRemoteBackup.sh

+ 7 - 0
backupMgr/Readme.md

@@ -28,6 +28,13 @@ declare -A backup_folders
 backup_folders["backupuser@hostA.com:/opt/SourceFolderA"]="opt/backup/folderA"
 backup_folders["/opt/SourceFolderB"]="opt/backup/folderB;/path/to/SourceFolderBSpecific/excludes.txt"
 
+#Exclude files
+An example exclude file could look as follows
+```
+*appdata*
+*updated*
+```
+
 #Configuration
 logPath=/home/user/logs/backupjob/
 logFileName="$(date +'%Y-%m-%d_%H-%M')_backupjobs.log"

+ 39 - 10
backupMgr/getRemoteBackup.sh

@@ -1,5 +1,33 @@
 #!/bin/bash
 
+#Enable pipefail to receive the error code of rsync and not only tee
+set -Eeuo pipefail
+
+trap 'on_error $LINENO' ERR
+
+on_error() {
+    local line=$1
+    echo "ERROR: Script failed at line $line" >&2
+
+    if [ -f "$tmpLogFilePath" ]; then
+        mailContent=$(cat "$tmpLogFilePath")
+    else
+        mailContent="Log file not available"
+    fi
+
+    {
+        echo "To: $mailTo"
+        echo "From: $mailFrom"
+        echo "Subject: Backup FAILED (CRASH) on $(date +'%c')"
+        echo ""
+        echo "Script crashed at line $line"
+        echo ""
+        echo "$mailContent"
+    } | sendmail -t
+
+    exit 1
+}
+
 #Check if path to the config file was provided
 if [ -z "$1" ]
 then
@@ -15,6 +43,9 @@ fi
 globalErrors=0
 logFilePath="${logPath}${logFileName}"
 
+#Rotate logs
+find $logPath -type f -mtime +$rotationDays -name '*.log' -execdir rm -- '{}' \;
+
 touch $tmpLogFilePath
 touch $logFilePath
 
@@ -32,10 +63,11 @@ for sourceFolder in "${!backup_folders[@]}"
 do
 	#Exclude files
 	IFS=';' read -ra split <<< "${backup_folders[$sourceFolder]}"
-	#check if exclude file is given and file exists. Set to zero if not existing.
-	if [[ -n ${split[1]} ]] && ! [ -f ${split[1]} ]; then
-		echo "Error: Could not find exclude file ${split[1]}. Ignoring it." | tee -a $tmpLogFilePath
-		split[1]=""
+	#check if exclude file is not zero and file exists. Set to zero if not existing.
+	excludeDIRs=${split[1]-}
+	if [[ -n ${excludeDIRs} ]] && ! [ -f ${excludeDIRs} ]; then
+		echo "Error: Could not find exclude file ${excludeDIRs}. Ignoring it." | tee -a $tmpLogFilePath
+		excludeDIRs=""
 	fi
 
 	destinationFolder=${split[0]}
@@ -47,11 +79,10 @@ do
 		echo "Starting Job" | tee -a $tmpLogFilePath
 		echo "SRC: ${sourceFolder}" | tee -a $tmpLogFilePath
 		echo "DEST: ${destinationFolder}" | tee -a $tmpLogFilePath
-		echo "Exclude file: ${split[1]}" | tee -a $tmpLogFilePath
+		echo "Exclude file: ${excludeDIRs}" | tee -a $tmpLogFilePath
 		date | tee -a $tmpLogFilePath
 		#SSH Host settings need to be set in the ssh client config!
-		excludeDIRs=${split[1]}
-		rsync --delete --out-format="%t %f %''b" -avz ${excludeDIRs:+"--exclude-from=$excludeDIRs"} ${sourceFolder} ${destinationFolder} | tee -a $tmpLogFilePath 2>&1
+		rsync --delete --out-format="%t %f %''b" -avz ${excludeDIRs:+"--exclude-from=$excludeDIRs"} ${sourceFolder} ${destinationFolder} 2>&1 | tee -a $tmpLogFilePath 2>&1
 		rsyncExitCode=$?
 		echo "Ending backup job" | tee -a $tmpLogFilePath
 		date | tee -a $tmpLogFilePath
@@ -67,11 +98,9 @@ do
 	done
 done
 
-#copy over and delete older logs
+#copy over
 cat $tmpLogFilePath > $logFilePath
 
-find $logPath -type f -mtime +$rotationDays -name '*.log' -execdir rm -- '{}' \;
-
 #Mail Notifications
 if [ $globalErrors -eq 0 ]; then
 	#send mail for success