Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Ntfy.sh notification support #161

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,13 @@
# MAIL_WHEN_SUCCESS="TRUE"
# MAIL_WHEN_FAILURE="TRUE"
# TIMEZONE="UTC"
# NTFY_ENABLE="FALSE"
# NTFY_SERVER="https://ntfy.sh"
# NTFY_TOPIC="vaultwarden-backup"
# NTFY_USERNAME=""
# NTFY_PASSWORD=""
# NTFY_TOKEN=""
# NTFY_PRIORITY_SUCCESS=3
# NTFY_PRIORITY_FAILURE=5
# NTFY_WHEN_SUCCESS="TRUE"
# NTFY_WHEN_FAILURE="TRUE"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ COPY scripts/*.sh /app/

RUN chmod +x /app/*.sh \
&& mkdir -m 777 /bitwarden \
&& apk add --no-cache 7zip bash mariadb-client postgresql16-client sqlite supercronic s-nail tzdata \
&& apk add --no-cache 7zip bash mariadb-client postgresql16-client sqlite supercronic s-nail tzdata curl \
&& apk info --no-cache -Lq mariadb-client | grep -vE '/bin/mariadb$' | grep -vE '/bin/mariadb-dump$' | xargs -I {} rm -f "/{}" \
&& ln -sf "${LOCALTIME_FILE}" /etc/localtime \
&& addgroup -g "${USER_ID}" "${USER_NAME}" \
Expand Down
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ And the following ways of notifying backup results are supported.

- Ping (send on completion, start, success, or failure)
- Mail (SMTP based, send on success and on failure)
- Ntfy.sh (Configurable server address, Username/password, token and no authentication supported)

<br>

Expand Down Expand Up @@ -390,6 +391,69 @@ Sends an email when the backup fails.

Default: `TRUE`

#### NTFY_ENABLE

Enable ntfy.sh integration, send notification to a ntfy.sh service once a backup succeeds or fails

Default: `False`

#### NTFY_SERVER

Server address of the ntfy.sh service, including protocol. Example: `https://ntfy.sh`

Default: `''`

#### NTFY_TOPIC

Topic where notifications are send to.

**Warning: If using a public ntfy.sh service, topics may not be password-protected, so choose a name that's not easy to guess (or use password-protection)**

Default: `vaultwarden-backup`

#### NTFY_USERNAME

Username when using username + password based authentication. Leave empty if you don't want to use authentication or want to use authentication based on a token.

Default: `''`


#### NTFY_PASSWORD

Password when using username + password basedauthentication. Leave empty if you don't want to use authentication or want to use authentication based on a token.

Default: `''`

#### NTFY_TOKEN

Token when using token based authentication. Leave empty if you don't want to use authentication or want to use authentication based on an username + password.

Default: `''`

#### NTFY_PRIORITY_SUCCESS

The priority of the ntfy notification when a backup succeeds.

Default: `3`

#### NTFY_PRIORITY_FAILURE

The priority of the ntfy notification when a backup fails.

Default: `5`

#### NTFY_WHEN_SUCCESS

Send a ntfy notification when a backup succeeds. (NTFY_ENABLE must also be 'TRUE')

Default: `TRUE`

#### NTFY_WHEN_FAILURE="TRUE"

Send a ntfy notification when a backup fails. (NTFY_ENABLE must also be 'TRUE')

Default: `TRUE`

#### DATA_DIR

This folder stores the data of vaultwarden.
Expand Down
6 changes: 6 additions & 0 deletions scripts/backup.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function backup_db_postgresql() {
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_ntfy_content "FALSE" "Backup failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Backup postgresql database failed."

exit 1
fi
Expand All @@ -61,6 +62,7 @@ function backup_db_mysql() {
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_ntfy_content "FALSE" "Backup failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Backup mysql database failed."

exit 1
fi
Expand Down Expand Up @@ -170,6 +172,8 @@ function upload() {
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_ntfy_content "FALSE" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Upload file not found."


exit 1
fi
Expand All @@ -193,6 +197,7 @@ function upload() {
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_ntfy_content "FALSE" "File upload failed at $(date +"%Y-%m-%d %H:%M:%S %Z"). Reason: Upload file not found."

exit 1
fi
Expand Down Expand Up @@ -236,6 +241,7 @@ clear_dir
clear_history

send_mail_content "TRUE" "The file was successfully uploaded at $(date +"%Y-%m-%d %H:%M:%S %Z")."
send_ntfy_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"

Expand Down
10 changes: 10 additions & 0 deletions scripts/entrypoint.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ if [[ "$1" == "mail" ]]; then
exit 0
fi

# ntfy test
if [[ "$1" == "ntfy" ]]; then
export_env_file
init_env_ntfy

send_ntfy "vaultwarden Backup Test" "Your ntfy looks configured correctly." "3"

exit 0
fi

# restore
if [[ "$1" == "restore" ]]; then
. /app/restore.sh
Expand Down
153 changes: 153 additions & 0 deletions scripts/includes.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,84 @@ function send_ping() {
fi
}



########################################
# Send ntfy message
# Arguments:
# ntfy subject
# ntfy content
# ntfy priority
# Outputs:
# ntfy send result
########################################
function send_ntfy() {

if [[ -z $NTFY_SERVER ]]; then
color red "No ntfy server configured"
exit 0
fi

# Initialize the curl command
curl_cmd="curl -s -w '%{http_code}' -o /dev/stdout"

# Add the authentication header based on available credentials
if [[ -n "$NTFY_PASSWORD" ]]; then
auth_base64=$(echo -n "$NTFY_USERNAME:$NTFY_PASSWORD" | base64 -w 0)
curl_cmd+=" -H 'Authorization: Basic $auth_base64'"
elif [[ -n "$NTFY_TOKEN" ]]; then
curl_cmd+=" -H \"Authorization: Bearer $NTFY_TOKEN\""
fi

# Add other headers and options
curl_cmd+=" -H 'Title: $1' -H 'X-Priority: $3' -d '$2' '$NTFY_SERVER/$NTFY_TOPIC'"


# Execute the constructed curl command
response=$(eval $curl_cmd)

# Extract the HTTP response code (last 3 characters of the response)
response_code="${response: -3}"

# Extract the response body (all except the last 3 characters)
response_body="${response:0:-3}"

# Check the HTTP response code
if [[ "$response_code" -eq 200 ]]; then
color blue "ntfy has been sent successfully"
else
color red "ntfy sending has failed with response code $response_code"
echo "Response body:"
echo "$response_body"
fi

}


########################################
# Send ntfy message content
# Arguments:
# backup succesfull
# notification content
########################################
function send_ntfy_content() {
if [[ "${NTFY_ENABLE}" == "FALSE" ]]; then
return
fi


# successful
if [[ "$1" == "TRUE" && "${NTFY_WHEN_SUCCESS}" == "TRUE" ]]; then
send_ntfy "vaultwarden Backup Success" "$2" "$NTFY_PRIORITY_SUCCESS"
fi

# failed
if [[ "$1" == "FALSE" && "${NTFY_WHEN_FAILURE}" == "TRUE" ]]; then
send_ntfy "vaultwarden Backup Failed" "$2" "$NTFY_PRIORITY_FAILURE"
fi

}

########################################
# Configure PostgreSQL password file.
# Arguments:
Expand Down Expand Up @@ -253,6 +331,7 @@ function init_env() {
init_env_db
init_env_ping
init_env_mail
init_env_ntfy

# CRON
get_env CRON
Expand Down Expand Up @@ -364,6 +443,19 @@ function init_env() {
fi
color yellow "TIMEZONE: ${TIMEZONE}"
color yellow "========================================"

color yellow "NTFY_ENABLE: ${NTFY_ENABLE}"
color yellow "NTFY_SERVER: ${NTFY_SERVER}"
color yellow "NTFY_TOPIC: ${NTFY_TOPIC}"
color yellow "NTFY_USERNAME: ${NTFY_USERNAME}"
color yellow "NTFY_PASSWORD: ${#NTFY_PASSWORD} Chars"
color yellow "NTFY_TOKEN: ${#NTFY_TOKEN} Chars"
color yellow "NTFY_PRIORITY_SUCCESS: ${NTFY_PRIORITY_SUCCESS}"
color yellow "NTFY_PRIORITY_FAILURE: ${NTFY_PRIORITY_FAILURE}"
color yellow "NTFY_WHEN_SUCCESS: ${NTFY_WHEN_SUCCESS}"
color yellow "NTFY_WHEN_FAILURE: ${NTFY_WHEN_FAILURE}"

color yellow "========================================"
}

function init_env_dir() {
Expand Down Expand Up @@ -496,3 +588,64 @@ function init_env_mail() {
MAIL_WHEN_FAILURE="TRUE"
fi
}


function init_env_ntfy() {
# NTFY_ENABLE
# NTFY_SERVER
get_env NTFY_ENABLE
if [[ "${NTFY_ENABLE^^}" == "TRUE" ]]; then
NTFY_ENABLE="TRUE"
else
NTFY_ENABLE="FALSE"
fi

# NTFY_SERVER
get_env NTFY_SERVER
NTFY_SERVER="${NTFY_SERVER:-""}"

# NTFY_TOPIC
get_env NTFY_TOPIC
NTFY_TOPIC="${NTFY_TOPIC:-"vaultwarden-backup"}"

# NTFY_USERNAME
get_env NTFY_USERNAME
NTFY_USERNAME="${NTFY_USERNAME:-""}"

# NTFY_PASSWORD
get_env NTFY_PASSWORD
NTFY_PASSWORD="${NTFY_PASSWORD:-""}"

# NTFY_TOKEN
get_env NTFY_TOKEN
NTFY_TOKEN="${NTFY_TOKEN:-""}"

# NTFY_TOKEN
get_env NTFY_TOKEN
NTFY_TOKEN="${NTFY_TOKEN:-""}"

# NTFY_PRIORITY_SUCCESS
get_env NTFY_PRIORITY_SUCCESS
NTFY_PRIORITY_SUCCESS="${NTFY_PRIORITY_SUCCESS:-"3"}"


# NTFY_PRIORITY_FAILURE
get_env NTFY_PRIORITY_FAILURE
NTFY_PRIORITY_FAILURE="${NTFY_PRIORITY_FAILURE:-"5"}"

# NTFY_WHEN_SUCCESS
get_env NTFY_WHEN_SUCCESS
if [[ "${NTFY_WHEN_SUCCESS^^}" == "FALSE" ]]; then
NTFY_WHEN_SUCCESS="FALSE"
else
NTFY_WHEN_SUCCESS="TRUE"
fi

# NTFY_WHEN_FAILURE
get_env NTFY_WHEN_FAILURE
if [[ "${NTFY_WHEN_FAILURE^^}" == "FALSE" ]]; then
NTFY_WHEN_FAILURE="FALSE"
else
NTFY_WHEN_FAILURE="TRUE"
fi
}