Merge pull request #120 from zertrin/fix-email-handling
Full rework of email handling
This commit is contained in:
commit
d7737e3045
1 changed files with 63 additions and 34 deletions
|
|
@ -336,47 +336,82 @@ check_logdir()
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mailcmd_sendmail() {
|
||||||
|
# based on http://linux.die.net/man/8/sendmail.sendmail
|
||||||
|
echo -e "From: ${EMAIL_FROM}\nSubject: ${EMAIL_SUBJECT}\n" | cat - "${LOGFILE}" | ${MAILCMD} "${EMAIL_TO}"
|
||||||
|
}
|
||||||
|
mailcmd_ssmtp() {
|
||||||
|
# based on http://linux.die.net/man/8/ssmtp
|
||||||
|
echo -e "From: ${EMAIL_FROM}\nSubject: ${EMAIL_SUBJECT}\n" | cat - "${LOGFILE}" | ${MAILCMD} "${EMAIL_TO}"
|
||||||
|
}
|
||||||
|
mailcmd_msmtp() {
|
||||||
|
# based on http://manpages.ubuntu.com/manpages/precise/en/man1/msmtp.1.html
|
||||||
|
echo -e "Subject: ${EMAIL_SUBJECT}\n" | cat - "${LOGFILE}" | ${MAILCMD} -f "${EMAIL_FROM}" -- "${EMAIL_TO}"
|
||||||
|
}
|
||||||
|
mailcmd_bsd_mailx() {
|
||||||
|
# based on http://man.he.net/man1/bsd-mailx
|
||||||
|
${MAILCMD} -s "${EMAIL_SUBJECT}" -a "From: ${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
|
||||||
|
}
|
||||||
|
mailcmd_heirloom_mailx() {
|
||||||
|
# based on http://heirloom.sourceforge.net/mailx/mailx.1.html
|
||||||
|
${MAILCMD} -s "${EMAIL_SUBJECT}" -S from="${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
|
||||||
|
}
|
||||||
|
mailcmd_nail() {
|
||||||
|
# based on http://linux.die.net/man/1/nail
|
||||||
|
${MAILCMD} -s "${EMAIL_SUBJECT}" -r "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
|
||||||
|
}
|
||||||
|
mailcmd_else() {
|
||||||
|
mailcmd_sendmail
|
||||||
|
}
|
||||||
|
|
||||||
email_logfile()
|
email_logfile()
|
||||||
{
|
{
|
||||||
if [ ! -z "${EMAIL_TO}" ]; then
|
if [ ! -z "${EMAIL_TO}" ]; then
|
||||||
|
|
||||||
MAILCMD=$(which "${MAIL}")
|
MAILCMD=$(which "${MAIL}")
|
||||||
|
MAILCMD_REALPATH=$(readlink -e "${MAILCMD}")
|
||||||
|
MAILCMD_BASENAME=${MAILCMD_REALPATH##*/}
|
||||||
|
|
||||||
if [ ! -x "${MAILCMD}" ]; then
|
if [ ! -x "${MAILCMD}" ]; then
|
||||||
echo -e "Email couldn't be sent. ${MAIL} not available." >> "${LOGFILE}"
|
echo -e "Email couldn't be sent. ${MAIL} not available." >> "${LOGFILE}"
|
||||||
else
|
else
|
||||||
EMAIL_SUBJECT=${EMAIL_SUBJECT:="duplicity-backup alert ${LOG_FILE}"}
|
EMAIL_SUBJECT=${EMAIL_SUBJECT:="duplicity-backup ${BACKUP_STATUS:-"ERROR"} [${HOSTNAME}] ${LOG_FILE}"}
|
||||||
if [ "${MAIL}" = "ssmtp" ]; then
|
case ${MAIL} in
|
||||||
echo """Subject: ${EMAIL_SUBJECT}""" | cat - "${LOGFILE}" | ${MAILCMD} -s "${EMAIL_TO}"
|
ssmtp)
|
||||||
elif [ "${MAIL}" = "msmtp" ]; then
|
mailcmd_ssmtp;;
|
||||||
echo """Subject: ${EMAIL_SUBJECT}""" | cat - "${LOGFILE}" | ${MAILCMD} "${EMAIL_TO}"
|
msmtp)
|
||||||
elif [ "${MAIL}" = "mailx" ]; then
|
mailcmd_msmtp;;
|
||||||
EMAIL_FROM=${EMAIL_FROM:+"-r ${EMAIL_FROM}"}
|
mail|mailx)
|
||||||
${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
|
case ${MAILCMD_BASENAME} in
|
||||||
elif [ "${MAIL}" = "mail" ]; then
|
bsd-mailx)
|
||||||
case $(uname) in
|
mailcmd_bsd_mailx;;
|
||||||
FreeBSD|Darwin|DragonFly|OpenBSD)
|
heirloom-mailx)
|
||||||
${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_TO}" -- < "${LOGFILE}"
|
mailcmd_heirloom_mailx;;
|
||||||
;;
|
*)
|
||||||
*)
|
mailcmd_else;;
|
||||||
${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" -- -f "${EMAIL_FROM}" < "${LOGFILE}"
|
esac
|
||||||
;;
|
;;
|
||||||
esac
|
sendmail)
|
||||||
elif [[ "${MAIL}" = "sendmail" ]]; then
|
mailcmd_sendmail;;
|
||||||
(echo """Subject: ${EMAIL_SUBJECT}""" ; cat "${LOGFILE}") | ${MAILCMD} -f "${EMAIL_FROM}" "${EMAIL_TO}"
|
nail)
|
||||||
elif [ "${MAIL}" = "nail" ]; then
|
mailcmd_nail;;
|
||||||
${MAILCMD} -s """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
|
*)
|
||||||
else
|
mailcmd_else;;
|
||||||
${MAILCMD} """${EMAIL_SUBJECT}""" "${EMAIL_FROM}" "${EMAIL_TO}" < "${LOGFILE}"
|
esac
|
||||||
fi
|
|
||||||
echo -e "Email alert sent to ${EMAIL_TO} using ${MAIL}" >> "${LOGFILE}"
|
echo -e "Email notification sent to ${EMAIL_TO} using ${MAIL}" >> "${LOGFILE}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
send_notification()
|
send_notification()
|
||||||
{
|
{
|
||||||
|
NOTIFICATION_CONTENT="duplicity-backup ${BACKUP_STATUS:-"ERROR"} [${HOSTNAME}] - \`${LOGFILE}\`"
|
||||||
if [ ! -z "${NOTIFICATION_SERVICE}" ]; then
|
if [ ! -z "${NOTIFICATION_SERVICE}" ]; then
|
||||||
if [ "${NOTIFICATION_SERVICE}" = "slack" ]; then
|
if [ "${NOTIFICATION_SERVICE}" = "slack" ]; then
|
||||||
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"${NOTIFICATION_CONTENT}\", \"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"icon_emoji\": \":${SLACK_EMOJI}:\"}" "${SLACK_HOOK_URL}"
|
curl -X POST -H 'Content-type: application/json' --data "{\"text\": \"${NOTIFICATION_CONTENT}\", \"channel\": \"${SLACK_CHANNEL}\", \"username\": \"${SLACK_USERNAME}\", \"icon_emoji\": \":${SLACK_EMOJI}:\"}" "${SLACK_HOOK_URL}"
|
||||||
|
|
||||||
|
echo -e "Slack notification sent to channel ${SLACK_CHANNEL}" >> "${LOGFILE}"
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
@ -858,22 +893,16 @@ esac
|
||||||
|
|
||||||
echo -e "--------- END DUPLICITY-BACKUP SCRIPT ---------\n" >> "${LOGFILE}"
|
echo -e "--------- END DUPLICITY-BACKUP SCRIPT ---------\n" >> "${LOGFILE}"
|
||||||
|
|
||||||
# send email
|
|
||||||
if [ "${BACKUP_ERROR}" ]; then
|
if [ "${BACKUP_ERROR}" ]; then
|
||||||
EMAIL_SUBJECT="BACKUP ERROR: ${EMAIL_SUBJECT}"
|
BACKUP_STATUS="ERROR"
|
||||||
else
|
else
|
||||||
EMAIL_SUBJECT="BACKUP OK: ${EMAIL_SUBJECT}"
|
BACKUP_STATUS="OK"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
# send email
|
||||||
[[ ${BACKUP_ERROR} || ! "$EMAIL_FAILURE_ONLY" = "yes" ]] && email_logfile
|
[[ ${BACKUP_ERROR} || ! "$EMAIL_FAILURE_ONLY" = "yes" ]] && email_logfile
|
||||||
|
|
||||||
# send notification
|
# send notification
|
||||||
if [ "${BACKUP_ERROR}" ]; then
|
|
||||||
NOTIFICATION_CONTENT="BACKUP ERROR: ${HOSTNAME} - \`$LOGFILE\`"
|
|
||||||
else
|
|
||||||
NOTIFICATION_CONTENT="BACKUP OK: ${HOSTNAME} - \`$LOGFILE\`"
|
|
||||||
fi
|
|
||||||
|
|
||||||
[[ ${BACKUP_ERROR} || ! "$NOTIFICATION_FAILURE_ONLY" = "yes" ]] && send_notification
|
[[ ${BACKUP_ERROR} || ! "$NOTIFICATION_FAILURE_ONLY" = "yes" ]] && send_notification
|
||||||
|
|
||||||
# remove old logfiles
|
# remove old logfiles
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue