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

This commit is contained in:
Christoph 2024-07-08 14:40:36 +02:00
parent 47dc51b783
commit 89d974defc
2 changed files with 125 additions and 36 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
# ==========
@ -245,6 +212,10 @@ DEFAULT_NC_DB_BACKUP_DIR="/data/backup/sql"
DEFAULT_BACKUP_OTHER_DIRECTORIES="/etc /home /root /var/spool/cron"
DEFAULT_ADMIN_EMAIL="root"
DEFAULT_COMPANY="O.OPEN"
DEFAULT_FROM_ADDRESS="borg-backup@$(hostname -f)"
# ----------
# Read Configurations from $conf_file
@ -258,6 +229,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
# ----------
@ -301,8 +314,8 @@ BORG_LOG_FILE="${BORG_LOG_DIR}/${backup_date}.log"
# By default: Borg won't follow symlinks
#
BORG_BACKUP_DIRS=""
for _dir in $BACKUP_OTHER_DIRECTORIES ; do
if [[ -h "${_dir}" ]] ; then
for _dir in $BACKUP_OTHER_DIRECTORIES ; do
if [[ -h "${_dir}" ]] ; then
BORG_BACKUP_DIRS="$BORG_BACKUP_DIRS $(realpath "${_dir}")"
fi
BORG_BACKUP_DIRS="$BORG_BACKUP_DIRS ${_dir}"
@ -325,7 +338,7 @@ export BORG_REPO
# For 'Warning: The repository at location … was previously located at ..'
#
# This has to be set when the repository has been created by user and
# This has to be set when the repository has been created by user and
# the script is called by cron
#
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
@ -401,7 +414,12 @@ echo "" >> ${BORG_LOG_FILE}
echo "NC_PATH=\"${NC_PATH}\"" >> ${BORG_LOG_FILE}
echo "NC_DB_BACKUP_DIR=\"${NC_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}
echo "PHP_COMMAND=\"${PHP_COMMAND}\"" >> ${BORG_LOG_FILE}
echo "" >> ${BORG_LOG_FILE}
@ -669,5 +687,41 @@ 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

@ -130,3 +130,38 @@
#
#BACKUP_OTHER_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"