The following code copy a complete folder (that has 3 folders inside), then check if errors are generated and send email if there's errors, the it proceeds to create 3 different tar files and finally move this tar files to a mounted network drive, every name has been changed or somehow altered in order to comply with my company security policy.
#! /usr/bin/env bash
readonly SUBJECT="BACKUP"
readonly TO="[email protected]"
readonly MESSAGE="~/backupMessageError.txt"
readonly TOALL="[email protected], [email protected]"
readonly MESSAGEALL="~/backupMessageSuccess.txt"
backup () {
if grep -qs '/mount/dir/' /proc/mounts; then
rsync --exclude /a/folder/ -ravz /backup/source /backup/destination/ 2> ~/backupMessageError.txt
if checker $1; then
mail
else
compression
fi
else
mount -o hard,nolock IP:/volume1/folder /mount/dir/ 2> ~/backupMessageError.txt
if checker $1; then
mail
else
rsync --exclude /a/folder/ -ravz /backup/source/ /backup/destination/ 2>> ~/backupMessageError.txt
if checker $1; then
mail
else
compression
fi
fi
fi
}
compression (){
tar -I pigz -cf backupFolder1.tar.gz folder1 2>> ~/backupMessageError.txt
tar -I pigz -cf backupFolder2.tar.gz folder2 2>> ~/backupMessageError.txt
tar -I pigz -cf backupFolder3.tar.gz folder3 2>> ~/backupMessageError.txt
if checker $1; then
mail
else
storage
fi
}
storage (){
mv /a/folder/backupFolder1.tar.gz /mount/dir/ 2>> ~/backupMessageError.txt
mv /a/folder/backupFolder2.tar.gz /mount/dir/ 2>> ~/backupMessageError.txt
mv /a/folder/backupFolder3.tar.gz /mount/dir/ 2>> ~/backupMessageError.txt
if checker $1; then
mail
fi
}
checker (){
if [ "$(wc -l < ~/backupMessageError.txt)" -ge 1 ];then
return 0;
else
return 1;
fi
}
mail () {
if checker $1;then
mail -s "$SUBJECT" "$TO" < $MESSAGE
else
mail -s "$SUBJECT" "$TOALL" < $MESSAGEALL
fi
}
backup