borg-backup.sh: add alert e-mail messages..

This commit is contained in:
Christoph 2024-07-08 14:39:52 +02:00
parent 93c1310cfc
commit 47dc51b783
2 changed files with 122 additions and 34 deletions

View File

@ -180,39 +180,6 @@ if $terminal ; then
fi
# -------------
# - Job is already running?
# -------------
# - If job already runs, stop execution..
# -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum="$(date +"%d.%m.%Y %H:%M")"
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e " Exiting now.."
echo ""
for _email in ${alert_email_arr[@]} ; do
echo -e "To:${_email}\n${content_type}\nSubject:Error cronjob `basename $0` -- $datum\n${msg}\n" \
| sendmail -F "Error `hostname -f`" -f $sender_address $_email
done
exit 1
fi
# ==========
# - Begin Main Script
# ==========
@ -248,6 +215,10 @@ DEFAULT_MYSQL_CREDENTIALS="-u root -S /run/mysqld/mysqld.sock"
DEFAULT_PSQL_SYSTEM_USER="postgres"
DEFAULT_ADMIN_EMAIL="root"
DEFAULT_COMPANY="O.OPEN"
DEFAULT_FROM_ADDRESS="borg-backup@$(hostname -f)"
# ----------
# Read Configurations from $conf_file
# ----------
@ -259,6 +230,48 @@ else
fi
# -------------
# - Job is already running?
# -------------
[[ -z "${ADMIN_EMAIL}" ]] && ADMIN_EMAIL="${DEFAULT_ADMIN_EMAIL}"
[[ -z "${COMPANY}" ]] && COMPANY="${DEFAULT_COMPANY}"
[[ -z "${FROM_ADDRESS}" ]] && FROM_ADDRESS="${DEFAULT_FROM_ADDRESS}"
# - If job already runs, stop execution..
# -
if mkdir "$LOCK_DIR" 2> /dev/null ; then
## - Remove lockdir when the script finishes, or when it receives a signal
trap "clean_up 1" SIGHUP SIGINT SIGTERM
else
datum="$(date +"%d.%m.%Y %H:%M")"
msg="[ Error ]: A previos instance of \"`basename $0`\" seems already be running.\n\n Exiting now.."
datum="$(date +"%d.%m.%Y")"
content_type='Content-Type: text/plain;\n charset="utf-8"'
subject="Error Borg Backup - ${COMPANY} - ${datum}"
subject_utf8="$(echo "$subject" | iconv -t UTF8)"
subject_utf8_encoded="=?utf-8?B?$(echo $subject_utf8 | base64 --wrap=0)?="
echo -e "To:${ADMIN_EMAIL}\n${content_type}\nSubject:${subject_utf8_encoded}\n${msg}\n" | /usr/sbin/sendmail -F "Errors BACKUP ${COMPANY}" -f ${FROM_ADDRESS} ${ADMIN_EMAIL}
echo ""
echo "[ Error ]: A previos instance of that script \"`basename $0`\" seems already be running."
echo ""
echo -e " Exiting now.."
echo ""
exit 1
fi
# ----------
# Set Parameter values
# ----------
@ -388,6 +401,10 @@ echo "" >> ${BORG_LOG_FILE}
echo "DB_TYPE=\"${DB_TYPE}\"" >> ${BORG_LOG_FILE}
echo "DB_BACKUP_DIR=\"${DB_BACKUP_DIR}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
echo "ADMIN_EMAIL=\"${ADMIN_EMAIL}\"" >> ${BORG_LOG_FILE}
echo "COMPANY=\"${COMPANY}\"" >> ${BORG_LOG_FILE}
echo "FROM_ADDRESS=\"${FROM_ADDRESS}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
if [[ -n "${DB_TYPE}" ]] && [[ "${DB_TYPE,,}" = "!none" ]]; then
echo "" >> ${BORG_LOG_FILE}
@ -637,4 +654,42 @@ durationReadable=$(printf "%02d hours %02d minutes %02d seconds" $durationHour $
echo -e "\n\n###### Server backup finished on ${endDateReadable} (${durationReadable}) ######\n" >> ${BORG_LOG_FILE}
# Send Summery
#
echo -e -n " Send Summary.."
datum="$(date +"%d.%m.%Y %H:%M")"
filesystem_usage="$(df -h)"
msg_head_line="\n\n# ---\n# Server backup finished on ${endDateReadable} (${durationReadable})\n# ---\n\n"
msg="$(cat "${BORG_LOG_FILE}")"
datum="$(date +"%d.%m.%Y")"
content_type='Content-Type: text/plain;\n charset="utf-8"'
subject="Borg Backup - ${COMPANY} - ${datum}"
subject_utf8="$(echo "$subject" | iconv -t UTF8)"
subject_utf8_encoded="=?utf-8?B?$(echo $subject_utf8 | base64 --wrap=0)?="
cat <<EOF >> ${BORG_LOG_FILE}
echo -e "To:${ADMIN_EMAIL}\n${content_type}\nSubject:${subject_utf8_encoded}\n\${filesystem_usage}\n\${msg_head_line}\n\${msg}\n" | /usr/sbin/sendmail -F "BACKUP ${COMPANY}" -f ${FROM_ADDRESS} ${ADMIN_EMAIL}
EOF
echo -e "To:${ADMIN_EMAIL}\n${content_type}\nSubject:${subject_utf8_encoded}\n${filesystem_usage}\n${msg_head_line}\n${msg}\n" | /usr/sbin/sendmail -F "BACKUP ${COMPANY}" -f ${FROM_ADDRESS} ${ADMIN_EMAIL} > ${log_file} 2>&1
if [[ $? -gt 0 ]] ; then
echo_failed
error "$(cat "${log_file}")"
else
echo_ok
fi
clean_up 0

View File

@ -139,7 +139,6 @@
#PSQL_SYSTEM_USER="postgres"
# DB_BACKUP_DIR
#
# Directory in which current database dumps are stored (and transferred to the repository)
@ -167,3 +166,37 @@
#
#BACKUP_DIRECTORIES="/etc /home /root /var/spool/cron"
# ---
# Alert Parameters
# ---
# ADMIN_EMAIL
#
# Send Informations about the backup prozess to this e-mail address
#
# Defaults to:
#
# ADMIN_EMAIL="root"
#
#ADMIN_EMAIL="root"
# FROM_ADDRESS
#
# Defaults to:
#
# "borg-backup@$(hostname -f)"
#
#FROM_ADDRESS=""
# COMPANY
#
# DEFAULTS to
# Defaults to:
#
# COMPANY="O.OPEN"
#
#COMPANY="O.OPEN"