1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #!/bin/bash
- #Read the backup folders and configuration variables from the config script
- . $1
- #Local variables
- globalErrors=0
- logFilePath="${logPath}${logFileName}"
- touch $tmpLogFilePath
- touch $logFilePath
- echo $logFilePath
- #Terminate if the last backup has not finished yet
- if pidof -o %PPID -x "$0" > /dev/null; then
- echo "Backup job already running. Exiting..."
- exit
- fi
- #Run the backup jobs as defined in the backup_folders array from the config
- echo "Starting backup job(s)" | tee $tmpLogFilePath
- for sourceFolder in "${!backup_folders[@]}"
- do
- mkdir -p ${backup_folders[$sourceFolder]}
- while [ "$retryAttempts" -ge "0" ]
- do
- echo "Starting Job" | tee -a $tmpLogFilePath
- echo "SRC: ${sourceFolder}" | tee -a $tmpLogFilePath
- echo "DEST: ${backup_folders[$sourceFolder]}" | tee -a $tmpLogFilePath
- date | tee -a $tmpLogFilePath
- #SSH Host settings need to be set in the ssh client config!
- rsync --delete --out-format="%t %f %''b" -avz $sourceFolder ${backup_folders[$sourceFolder]} | tee -a $tmpLogFilePath 2>&1
- rsyncExitCode=$?
- echo "Ending backup job" | tee -a $tmpLogFilePath
- date | tee -a $tmpLogFilePath
- echo "" | tee -a $tmpLogFilePath
- if [ $rsyncExitCode -eq 0 ]; then
- break
- else
- echo "Error: rsync exited with code $rsyncExitCode. Retrying..." | tee -a $tmpLogFilePath
- retryAttempts=$((retryAttempts-1))
- globalErrors=$((globalErrors+1))
- fi
- done
- done
- #copy over and delete older logs
- cat $tmpLogFilePath > $logFilePath
- find $logPath -type f -mtime +$rotationDays -name '*.log' -execdir rm -- '{}' \;
- #Mail Notifications
- if [ $globalErrors -eq 0 ]; then
- #send mail for success
- echo -e "To:$mailTo\nFrom:$mailFrom\nSubject:Backup SUCCESSFUL on $(date +'%c')\n\n$(cat $tmpLogFilePath)" | sendmail $mailTo
- else
- #send mail for failure
- echo -e "To:$mailTo\nFrom:$mailFrom\nSubject:Backup FAILED on $(date +'%c')\n\n$(cat $tmpLogFilePath)" | sendmail $mailTo
- fi
- #delete tmp file
- rm $tmpLogFilePath
|