From 3de4f98d24b7d5eca170835a1c5d005988823e35 Mon Sep 17 00:00:00 2001 From: ttionya Date: Fri, 6 Sep 2024 07:37:24 +0000 Subject: [PATCH] feat: improve notification method --- scripts/backup.sh | 22 ++++----------- scripts/includes.sh | 69 +++++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/scripts/backup.sh b/scripts/backup.sh index 6a4a774..18f7ae6 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -43,9 +43,7 @@ function backup_db_postgresql() { if [[ $? != 0 ]]; then color red "backup vaultwarden postgresql database failed" - send_ping "${PING_URL_WHEN_FAILURE}" "failure" - send_ping "${PING_URL}" "completion" - send_mail_content "FALSE" "Backup failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Backup postgresql database failed." + send_notification "failure" "Backup failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Backup postgresql database failed." exit 1 fi @@ -58,9 +56,7 @@ function backup_db_mysql() { if [[ $? != 0 ]]; then color red "backup vaultwarden mysql database failed" - send_ping "${PING_URL_WHEN_FAILURE}" "failure" - send_ping "${PING_URL}" "completion" - send_mail_content "FALSE" "Backup failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Backup mysql database failed." + send_notification "failure" "Backup failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Backup mysql database failed." exit 1 fi @@ -167,9 +163,7 @@ function upload() { if [[ ! -e "${UPLOAD_FILE}" ]]; then color red "upload file not found" - send_ping "${PING_URL_WHEN_FAILURE}" "failure" - send_ping "${PING_URL}" "completion" - send_mail_content "FALSE" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Upload file not found." + send_notification "failure" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Upload file not found." exit 1 fi @@ -190,9 +184,7 @@ function upload() { done if [[ "${HAS_ERROR}" == "TRUE" ]]; then - send_ping "${PING_URL_WHEN_FAILURE}" "failure" - send_ping "${PING_URL}" "completion" - send_mail_content "FALSE" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z")." + send_notification "failure" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z")." exit 1 fi @@ -223,7 +215,7 @@ color blue "running the backup program at $(date +"%Y-%m-%d %H:%M:%S %Z")" init_env -send_ping "${PING_URL_WHEN_START}" "start" +send_notification "start" "Start backup at $(date +"%Y-%m-%d %H:%M:%S %Z")" check_rclone_connection @@ -235,8 +227,6 @@ upload clear_dir clear_history -send_mail_content "TRUE" "The file was successfully uploaded at $(date +"%Y-%m-%d %H:%M:%S %Z")." -send_ping "${PING_URL}" "completion" -send_ping "${PING_URL_WHEN_SUCCESS}" "success" +send_notification "success" "The file was successfully uploaded at $(date +"%Y-%m-%d %H:%M:%S %Z")." color none "" diff --git a/scripts/includes.sh b/scripts/includes.sh index 8b82672..12c4fba 100644 --- a/scripts/includes.sh +++ b/scripts/includes.sh @@ -102,28 +102,6 @@ function send_mail() { fi } -######################################## -# Send mail. -# Arguments: -# backup successful -# mail content -######################################## -function send_mail_content() { - if [[ "${MAIL_SMTP_ENABLE}" == "FALSE" ]]; then - return - fi - - # successful - if [[ "$1" == "TRUE" && "${MAIL_WHEN_SUCCESS}" == "TRUE" ]]; then - send_mail "vaultwarden Backup Success" "$2" - fi - - # failed - if [[ "$1" == "FALSE" && "${MAIL_WHEN_FAILURE}" == "TRUE" ]]; then - send_mail "vaultwarden Backup Failed" "$2" - fi -} - ######################################## # Send health check or notification ping. # Arguments: @@ -153,16 +131,57 @@ function send_ping() { CURL_OPTIONS=$(echo "${CURL_OPTIONS}" | sed "s/%{subject}/$2/g") CURL_OPTIONS=$(echo "${CURL_OPTIONS}" | sed "s/%{content}/$3/g") - local CURL_COMMAND="curl -m 15 --retry 10 --retry-delay 3 -o /dev/null -s${CURL_OPTIONS:+" ${CURL_OPTIONS}"} ${CURL_URL}" + local CURL_COMMAND="curl -m 15 --retry 10 --retry-delay 1 -o /dev/null -s${CURL_OPTIONS:+" ${CURL_OPTIONS}"} \"${CURL_URL}\"" + + if [[ "${PING_DEBUG}" == "TRUE" ]]; then + color yellow "curl command: ${CURL_COMMAND}" + fi eval "${CURL_COMMAND}" if [[ $? != 0 ]]; then - color red "$2 ping sending has failed" + color red "$1 ping sending has failed" else - color blue "$2 ping has been sent successfully" + color blue "$1 ping has been sent successfully" fi } +######################################## +# Send notification. +# Arguments: +# status (start / success / failure) +# notification content +######################################## +function send_notification() { + local SUBJECT_START="vaultwarden Backup Start" + local SUBJECT_SUCCESS="vaultwarden Backup Success" + local SUBJECT_FAILURE="vaultwarden Backup Failed" + + case "$1" in + start) + // ping + send_ping "start" "${SUBJECT_START}" "$2" + ;; + success) + // mail + if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" && "${MAIL_WHEN_SUCCESS}" == "TRUE" ]]; then + send_mail "${SUBJECT_SUCCESS}" "$2" + fi + // ping + send_ping "success" "${SUBJECT_SUCCESS}" "$2" + send_ping "completion" "${SUBJECT_SUCCESS}" "$2" + ;; + failure) + // mail + if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" && "${MAIL_WHEN_FAILURE}" == "TRUE" ]]; then + send_mail "${SUBJECT_FAILURE}" "$2" + fi + // ping + send_ping "failure" "${SUBJECT_FAILURE}" "$2" + send_ping "completion" "${SUBJECT_FAILURE}" "$2" + ;; + esac +} + ######################################## # Configure PostgreSQL password file. # Arguments: