|
@@ -1,43 +1,79 @@
|
|
|
#!/bin/bash
|
|
|
|
|
|
-#TODO:
|
|
|
+#Read the backup folders from the config script outside the git repo
|
|
|
+. $1
|
|
|
+#Example script
|
|
|
+#declare -A backup_folders
|
|
|
+#backup_folders["/opt/SourceFolderA"]="opt/backup/folderA"
|
|
|
+#backup_folders["/opt/SourceFolderB"]="opt/backup/folderB"
|
|
|
|
|
|
+#Configuration
|
|
|
+logPath=/home/sebastian/ARCHIVE/sebastian/3000_BACKUP/
|
|
|
+logFileName="$(date +'%Y-%m-%d_%H-%M')_backupjobs.log"
|
|
|
+tmpLogFilePath=/tmp/tmp_backup.log
|
|
|
+retryAttempts=2
|
|
|
+
|
|
|
+#Delete logs older than rotationDays
|
|
|
+rotationDays=14
|
|
|
+
|
|
|
+#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
|
|
|
|
|
|
-logFilePath=/home/sebastian/ARCHIVE/sebastian/3000_BACKUP/sebastianvendt.de.log
|
|
|
-tmpLogFilePath=/tmp/tmp_backup.log
|
|
|
-retryAttempts=1
|
|
|
-touch $tmpLogFilePath
|
|
|
+echo "Starting backup job(s)" | tee $tmpLogFilePath
|
|
|
+
|
|
|
|
|
|
-while [ "$retryAttempts" -ge "0" ]
|
|
|
+for sourceFolder in "${!backup_folders[@]}"
|
|
|
do
|
|
|
- date >> $tmpLogFilePath
|
|
|
- echo "Starting backup job" >> $tmpLogFilePath
|
|
|
- rsync --delete --out-format="%t %f %''b" -avze 'ssh -p 9753 -i /home/sebastian/sshKeys/rsyncOnSebastianvendt.de.pem' rsync@sebastianvendt.de:/var/lib/psa/dumps/domains/sebastianvendt.de/ /home/sebastian/ARCHIVE/sebastian/3000_BACKUP/sebastianvendt.de | tee -a $tmpLogFilePath 2>&1
|
|
|
- rsyncExitCode=$?
|
|
|
- echo "Ending backup job" >> $tmpLogFilePath
|
|
|
- date >> $tmpLogFilePath
|
|
|
- echo "" >> $tmpLogFilePath
|
|
|
-
|
|
|
- if [ $rsyncExitCode -eq 0 ]; then
|
|
|
- break
|
|
|
- fi
|
|
|
- retryAttempts=$((retryAttempts-1))
|
|
|
+ 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
|
|
|
-cat $tmpLogFilePath >> $logFilePath
|
|
|
+#copy over and delete older logs
|
|
|
+cat $tmpLogFilePath > $logFilePath
|
|
|
+
|
|
|
+find $logPath -type f -mtime +$rotationDays -name '*.log' -execdir rm -- '{}' \;
|
|
|
|
|
|
-if [ $rsyncExitCode -eq 0 ]; then
|
|
|
-#send mail for success
|
|
|
+#Mail Notifications
|
|
|
+if [ $globalErrors -eq 0 ]; then
|
|
|
+ #send mail for success
|
|
|
echo -e "To:mail@sebastianvendt.de\nFrom:Sebastian@manaslu.home\nSubject:Backup SUCCESSFUL on $(date +'%c')\n\n$(cat $tmpLogFilePath)" | sendmail mail@sebastianvendt.de
|
|
|
else
|
|
|
-#send mail for failure
|
|
|
+ #send mail for failure
|
|
|
echo -e "To:mail@sebastianvendt.de\nFrom:Sebastian@manaslu.home\nSubject:Backup FAILED on $(date +'%c')\n\n$(cat $tmpLogFilePath)" | sendmail mail@sebastianvendt.de
|
|
|
fi
|
|
|
|
|
|
-#delete file
|
|
|
+#delete tmp file
|
|
|
rm $tmpLogFilePath
|