diff --git a/.env b/.env index e2ada6b..df02d5d 100644 --- a/.env +++ b/.env @@ -11,6 +11,9 @@ # BACKUP_FILE_SUFFIX="%Y%m%d" # BACKUP_KEEP_DAYS="0" # PING_URL="" +# PING_URL_WHEN_STARTS="" +# PING_URL_WHEN_SUCCESS="" +# PING_URL_WHEN_ERROR="" # MAIL_SMTP_ENABLE="FALSE" # MAIL_SMTP_VARIABLES="" # MAIL_TO="" diff --git a/README.md b/README.md index e90bd8d..af00f30 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ This tool supports backing up the following files or directories. And the following ways of notifying backup results are supported. -- Ping (only send on success) +- Ping (send on completion, start, success, or failure) - Mail (SMTP based, send on success and on failure)
@@ -330,8 +330,20 @@ Default: `UTC` #### PING_URL +Use [healthcheck.io](https://healthchecks.io/) url or similar cron monitoring to perform `GET` requests after a backup **no matter if it succeeds or fails**. + +#### PING_URL_WHEN_STARTS + +Use [healthcheck.io](https://healthchecks.io/) url or similar cron monitoring to perform `GET` requests at the **beginning** of a backup. + +#### PING_URL_WHEN_SUCCESS + Use [healthcheck.io](https://healthchecks.io/) url or similar cron monitoring to perform `GET` requests after a **successful** backup. +#### PING_URL_WHEN_ERROR + +Use [healthcheck.io](https://healthchecks.io/) url or similar cron monitoring to perform `GET` requests after a backup **fails**. + #### MAIL_SMTP_ENABLE Starting from v1.19.0, we will be using [`s-nail`](https://www.sdaoden.eu/code-nail.html) instead of [`heirloom-mailx`](https://www.systutorials.com/docs/linux/man/1-heirloom-mailx/) to send emails. diff --git a/README_zh.md b/README_zh.md index d6baf27..60b092d 100644 --- a/README_zh.md +++ b/README_zh.md @@ -42,7 +42,7 @@ 并且支持以下通知备份结果的方式。 -- Ping (仅成功时发送) +- Ping (完成,开始,成功或失败时发送) - Mail (基于 SMTP,成功时和失败时都会发送)
@@ -328,8 +328,20 @@ Rclone 全局参数,详见 [flags](https://rclone.org/flags/)。 #### PING_URL +使用 [healthcheck.io](https://healthchecks.io/) 地址或者其他类似的 cron 监控,以便在备份**完成(不论成功或失败)**后执行 `GET` 请求。 + +#### PING_URL_WHEN_STARTS + +使用 [healthcheck.io](https://healthchecks.io/) 地址或者其他类似的 cron 监控,以便在备份**开始**前执行 `GET` 请求。 + +#### PING_URL_WHEN_SUCCESS + 使用 [healthcheck.io](https://healthchecks.io/) 地址或者其他类似的 cron 监控,以便在备份**成功**后执行 `GET` 请求。 +#### PING_URL_WHEN_ERROR + +使用 [healthcheck.io](https://healthchecks.io/) 地址或者其他类似的 cron 监控,以便在备份**失败**后执行 `GET` 请求。 + #### MAIL_SMTP_ENABLE 从 v1.19.0 开始,本工具使用 [`s-nail`](https://www.sdaoden.eu/code-nail.html) 代替 [`heirloom-mailx`](https://www.systutorials.com/docs/linux/man/1-heirloom-mailx/) 发送邮件。 diff --git a/docker-compose.yml b/docker-compose.yml index d909c63..c0bd609 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -26,7 +26,10 @@ services: # ZIP_TYPE: 'zip' # BACKUP_FILE_SUFFIX: '%Y%m%d' # BACKUP_KEEP_DAYS: 0 - # PING_URL: '' + # PING_URL='' + # PING_URL_WHEN_STARTS='' + # PING_URL_WHEN_SUCCESS='' + # PING_URL_WHEN_ERROR='' # MAIL_SMTP_ENABLE: 'FALSE' # MAIL_SMTP_VARIABLES: '' # MAIL_TO: '' diff --git a/scripts/backup.sh b/scripts/backup.sh index 4eed606..5f1e8ef 100644 --- a/scripts/backup.sh +++ b/scripts/backup.sh @@ -43,6 +43,8 @@ function backup_db_postgresql() { if [[ $? != 0 ]]; then color red "backup vaultwarden postgresql database failed" + send_ping "${PING_URL_WHEN_ERROR}" "error" + 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." exit 1 @@ -56,6 +58,8 @@ function backup_db_mysql() { if [[ $? != 0 ]]; then color red "backup vaultwarden mysql database failed" + send_ping "${PING_URL_WHEN_ERROR}" "error" + 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." exit 1 @@ -163,6 +167,8 @@ function upload() { if [[ ! -e "${UPLOAD_FILE}" ]]; then color red "upload file not found" + send_ping "${PING_URL_WHEN_ERROR}" "error" + 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." exit 1 @@ -184,6 +190,8 @@ function upload() { done if [[ "${HAS_ERROR}" == "TRUE" ]]; then + send_ping "${PING_URL_WHEN_ERROR}" "error" + send_ping "${PING_URL}" "completion" send_mail_content "FALSE" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z")." exit 1 @@ -214,6 +222,9 @@ function clear_history() { color blue "running the backup program at $(date +"%Y-%m-%d %H:%M:%S %Z")" init_env + +send_ping "${PING_URL_WHEN_STARTS}" "start" + check_rclone_connection clear_dir @@ -225,6 +236,7 @@ clear_dir clear_history send_mail_content "TRUE" "The file was successfully uploaded at $(date +"%Y-%m-%d %H:%M:%S %Z")." -send_ping +send_ping "${PING_URL}" "completion" +send_ping "${PING_URL_WHEN_SUCCESS}" "success" color none "" diff --git a/scripts/includes.sh b/scripts/includes.sh index 127c4f8..b12905f 100644 --- a/scripts/includes.sh +++ b/scripts/includes.sh @@ -127,18 +127,19 @@ function send_mail_content() { ######################################## # Send health check ping. # Arguments: -# None +# url +# ping name ######################################## function send_ping() { - if [[ -z "${PING_URL}" ]]; then + if [[ -z "${1}" ]]; then return fi - wget "${PING_URL}" -T 15 -t 10 -O /dev/null -q + wget "${1}" -T 15 -t 10 -O /dev/null -q if [[ $? != 0 ]]; then - color red "ping sending failed" + color red "${2} ping sending failed" else - color blue "ping send was successfully" + color blue "${2} ping send was successfully" fi } @@ -250,6 +251,7 @@ function init_env() { init_env_dir init_env_db + init_env_ping init_env_mail # CRON @@ -304,10 +306,6 @@ function init_env() { BACKUP_FILE_DATE="$(echo "${BACKUP_FILE_DATE:-"%Y%m%d"}${BACKUP_FILE_DATE_SUFFIX}" | sed 's/[^0-9a-zA-Z%_-]//g')" BACKUP_FILE_DATE_FORMAT="$(echo "${BACKUP_FILE_SUFFIX:-"${BACKUP_FILE_DATE}"}" | sed 's/\///g')" - # PING_URL - get_env PING_URL - PING_URL="${PING_URL:-""}" - # TIMEZONE get_env TIMEZONE local TIMEZONE_MATCHED_COUNT=$(ls "/usr/share/zoneinfo/${TIMEZONE}" 2> /dev/null | wc -l) @@ -349,6 +347,15 @@ function init_env() { if [[ -n "${PING_URL}" ]]; then color yellow "PING_URL: ${PING_URL}" fi + if [[ -n "${PING_URL_WHEN_STARTS}" ]]; then + color yellow "PING_URL_WHEN_STARTS: ${PING_URL_WHEN_STARTS}" + fi + if [[ -n "${PING_URL_WHEN_SUCCESS}" ]]; then + color yellow "PING_URL_WHEN_SUCCESS: ${PING_URL_WHEN_SUCCESS}" + fi + if [[ -n "${PING_URL_WHEN_ERROR}" ]]; then + color yellow "PING_URL_WHEN_ERROR: ${PING_URL_WHEN_ERROR}" + fi color yellow "MAIL_SMTP_ENABLE: ${MAIL_SMTP_ENABLE}" if [[ "${MAIL_SMTP_ENABLE}" == "TRUE" ]]; then color yellow "MAIL_TO: ${MAIL_TO}" @@ -440,6 +447,24 @@ function init_env_db() { fi } +function init_env_ping() { + # PING_URL + get_env PING_URL + PING_URL="${PING_URL:-""}" + + # PING_URL_WHEN_STARTS + get_env PING_URL_WHEN_STARTS + PING_URL_WHEN_STARTS="${PING_URL_WHEN_STARTS:-""}" + + # PING_URL_WHEN_SUCCESS + get_env PING_URL_WHEN_SUCCESS + PING_URL_WHEN_SUCCESS="${PING_URL_WHEN_SUCCESS:-""}" + + # PING_URL_WHEN_ERROR + get_env PING_URL_WHEN_ERROR + PING_URL_WHEN_ERROR="${PING_URL_WHEN_ERROR:-""}" +} + function init_env_mail() { # MAIL_SMTP_ENABLE # MAIL_TO