From a1a7a46a1fa13f2dfb6d1d4ff5c410ccc2ab1554 Mon Sep 17 00:00:00 2001 From: Lucas Costa Beyeler Date: Sat, 7 Apr 2018 16:10:59 -0300 Subject: [PATCH] Issue #59 - New Feature: Enable/Disable mail notification --- project/config/zmbackup.conf | 11 +++++ project/lib/bash/MiscAction.sh | 2 +- project/lib/bash/NotifyAction.sh | 77 +++++++++++++++++--------------- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/project/config/zmbackup.conf b/project/config/zmbackup.conf index 962ded8..aff9646 100644 --- a/project/config/zmbackup.conf +++ b/project/config/zmbackup.conf @@ -65,6 +65,17 @@ LDAPPASS={OSE_INSTALL_LDAPPASS} LOGFILE=/opt/zimbra/log/zmbackup.log +# ENABLE_EMAIL_NOTIFY - Enable or disable mail notification during a backup routine. +# OPTIONS: +# - all: Enable all the mail notification +# - start: Enable only the start mail notification +# - finish: Enable only the finish mail notification +# - error: Enable only for error messages +# - none: Disable mail notification +# DEFAULT: all + +ENABLE_EMAIL_NOTIFY=all + # EMAIL_NOTIFY - After each zmbackup execution, going everything okay or not, a mail # is sended to someone to inform that the process is concluded, and # show a report of each account backed up. diff --git a/project/lib/bash/MiscAction.sh b/project/lib/bash/MiscAction.sh index 1602129..9212c77 100644 --- a/project/lib/bash/MiscAction.sh +++ b/project/lib/bash/MiscAction.sh @@ -12,7 +12,7 @@ function on_exit(){ if [[ $ERRCODE -eq 1 ]]; then notify_finish $SESSION $STYPE "FAILURE" elif [[ $ERRCODE -eq 0 && ! -z $SESSION ]]; then - notify_finish $SESSION $STYPE "SUCCESS" + notify_finish $SESSION "$STYPE" "SUCCESS" fi fi rm -rf $TEMPSESSION $TEMPACCOUNT $TEMPINCACCOUNT $TEMPDIR $MESSAGE $TEMPSQL $FAILURE diff --git a/project/lib/bash/NotifyAction.sh b/project/lib/bash/NotifyAction.sh index d20be16..0f2ee7d 100644 --- a/project/lib/bash/NotifyAction.sh +++ b/project/lib/bash/NotifyAction.sh @@ -11,18 +11,20 @@ ################################################################################ function notify_begin() { - printf "Subject: Zmbackup - Backup routine for $1 start at $(date)" > $MESSAGE - printf "\nGreetings Administrator," >> $MESSAGE - printf "\n\nThis is an automatic message to inform you that the process for $2 BACKUP that you scheduled started right now." >> $MESSAGE - printf " Depending on the ammount of accounts and/or data to be backed up, this process can take some hours before conclude." >> $MESSAGE - printf "\nDon't worry, we will inform you when the process finish." >> $MESSAGE - printf "\n\nRegards," >> $MESSAGE - printf "\nZmbackup Team" >> $MESSAGE - sendmail $EMAIL_NOTIFY < $MESSAGE - if [[ $? -eq 0 ]]; then - logger -i -p local7.info "Zmbackup: Mail sended to $EMAIL_NOTIFY to notify about the backup routine begin." - else - logger -i -p local7.info "Zmbackup: Cannot send mail for $EMAIL_NOTIFY - $ERR." + if [[ "$ENABLE_EMAIL_NOTIFY" == "all" || "$ENABLE_EMAIL_NOTIFY" == "start" ]]; then + printf "Subject: Zmbackup - Backup routine for $1 start at $(date)" > $MESSAGE + printf "\nGreetings Administrator," >> $MESSAGE + printf "\n\nThis is an automatic message to inform you that the process for $2 BACKUP that you scheduled started right now." >> $MESSAGE + printf " Depending on the ammount of accounts and/or data to be backed up, this process can take some hours before conclude." >> $MESSAGE + printf "\nDon't worry, we will inform you when the process finish." >> $MESSAGE + printf "\n\nRegards," >> $MESSAGE + printf "\nZmbackup Team" >> $MESSAGE + sendmail $EMAIL_NOTIFY < $MESSAGE + if [[ $? -eq 0 ]]; then + logger -i -p local7.info "Zmbackup: Mail sended to $EMAIL_NOTIFY to notify about the backup routine begin." + else + logger -i -p local7.info "Zmbackup: Cannot send mail for $EMAIL_NOTIFY - $ERR." + fi fi } @@ -39,30 +41,33 @@ function notify_begin() ################################################################################ function notify_finish() { - # Loading the variables - SIZE=$(du -h $WORKDIR/$1 | awk {'print $1'}) - if [[ "$1" == "mbox"* ]]; then - QTDE=$(ls $WORKDIR/$1/*.tgz | wc -l) - else - QTDE=$(ls $WORKDIR/$1/*.ldiff | wc -l) - fi + if [[ "$ENABLE_EMAIL_NOTIFY" == "all" ]] || [[ "$ENABLE_EMAIL_NOTIFY" == "finish" && "$3" == "SUCCESS" ]] || [[ "$ENABLE_EMAIL_NOTIFY" == "error" && "$3" == "FAILURE" ]] ; then + + # Loading the variables + SIZE=$(du -h $WORKDIR/$1 | awk {'print $1'}) + if [[ "$1" == "mbox"* ]]; then + QTDE=$(ls $WORKDIR/$1/*.tgz | wc -l) + else + QTDE=$(ls $WORKDIR/$1/*.ldiff | wc -l) + fi - # The message - printf "Subject: Zmbackup - Backup routine for $1 complete at $(date) - $3" > $MESSAGE - printf "\nGreetings Administrator," >> $MESSAGE - printf "\n\nThis is an automatic message to inform you that the process for $2 BACKUP that you scheduled ended right now." >> $MESSAGE - printf "\nHere some information about this session:" >> $MESSAGE - printf "\n\nSize: $SIZE" >> $MESSAGE - printf "\nAccounts: $QTDE" >> $MESSAGE - printf "\nStatus: $3" >> $MESSAGE - printf "\n\nRegards," >> $MESSAGE - printf "\nZmbackup Team" >> $MESSAGE - printf "\n\nSummary of files:\n" >> $MESSAGE - cat $TEMPSESSION >> $MESSAGE - ERR=$((sendmail $EMAIL_NOTIFY < $MESSAGE ) 2>&1) - if [[ $? -eq 0 ]]; then - logger -i -p local7.info "Zmbackup: Mail sended to $EMAIL_NOTIFY to notify about the backup routine conclusion." - else - logger -i -p local7.info "Zmbackup: Cannot send mail for $EMAIL_NOTIFY - $ERR." + # The message + printf "Subject: Zmbackup - Backup routine for $1 complete at $(date) - $3" > $MESSAGE + printf "\nGreetings Administrator," >> $MESSAGE + printf "\n\nThis is an automatic message to inform you that the process for $2 BACKUP that you scheduled ended right now." >> $MESSAGE + printf "\nHere some information about this session:" >> $MESSAGE + printf "\n\nSize: $SIZE" >> $MESSAGE + printf "\nAccounts: $QTDE" >> $MESSAGE + printf "\nStatus: $3" >> $MESSAGE + printf "\n\nRegards," >> $MESSAGE + printf "\nZmbackup Team" >> $MESSAGE + printf "\n\nSummary of files:\n" >> $MESSAGE + cat $TEMPSESSION >> $MESSAGE + ERR=$((sendmail $EMAIL_NOTIFY < $MESSAGE ) 2>&1) + if [[ $? -eq 0 ]]; then + logger -i -p local7.info "Zmbackup: Mail sended to $EMAIL_NOTIFY to notify about the backup routine conclusion." + else + logger -i -p local7.info "Zmbackup: Cannot send mail for $EMAIL_NOTIFY - $ERR." + fi fi }