implement ntfy service

This commit is contained in:
Marc Koch 2023-09-01 10:47:27 +02:00
parent 183b16acde
commit 0e7bceeec6
3 changed files with 54 additions and 1 deletions

View File

@ -9,7 +9,16 @@ export BORG_RSH="ssh -i $HOME/.ssh/id_ed25519"
export BORG_REPO="ssh://borguser@borgserver.com:22/~/backups/$USER/$HOST" export BORG_REPO="ssh://borguser@borgserver.com:22/~/backups/$USER/$HOST"
# See the section "Passphrase notes" for more infos. # See the section "Passphrase notes" for more infos.
export BORG_PASSPHRASE='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' export BORG_PASSPHRASE="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# This array must containing everything you want to include in the backup # This array must containing everything you want to include in the backup
export BORG_TARGETS=("$HOME") export BORG_TARGETS=("$HOME")
# [optional] Use a ntfy service to publish backup results
# export NTFY=true
# [optional] ntfy url with topic
# export NTFY_URL="https://ntfy.sh/myborgbackup"
# [optional] ntfy token
# export NTFY_TOKEN="tk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

View File

@ -114,6 +114,14 @@ nano $HOME/.borg-env
- Set a strong passphrase - Set a strong passphrase
- Define everything you want to include in your backup as an **absolute path** in the `BORG_TARGETS` array - Define everything you want to include in your backup as an **absolute path** in the `BORG_TARGETS` array
### Publish backup result via _nfty_
You can use a [_ntfy_](https://docs.ntfy.sh/) instance to publish the result of your backup process to a _ntfy_ topic. Just set the corresponding environment variables in the `.borg-env`:
- Set `NTFY` to `true`
- Set `NTFY_URL` to the URL of your preferred _ntfy_ instance including the desired topic, e.g. `"https://ntfy.sh/myborgbackup"`
- Set your _ntfy_ token if necessary
Make the script executable. Make the script executable.
```bash ```bash

View File

@ -16,6 +16,33 @@ info() {
echo "$( date ) BORGBACKUP $* " >> /var/log/backup.log; echo "$( date ) BORGBACKUP $* " >> /var/log/backup.log;
} }
# function to send ntfy messages
ntfy() {
local message="$1"
local priority="$2"
local ntfy_url="${NTFY_URL}"
local ntfy_token="${NTFY_TOKEN}"
if [ -z "${ntfy_url}" ]; then
info "NTFY_URL is not set. Cannot send notification."
return
fi
# construct curl command with or without token
if [ -z "${ntfy_token}" ]; then
local curl_command="curl -H 'Priority: ${priority}' -d '${message}' '${ntfy_url}'"
else
local curl_command="curl -H 'Authorization: Bearer ${ntfy_token}' -H 'Priority: ${priority}' -d '${message}' '${ntfy_url}'"
fi
# send curl and log result
if eval "${curl_command}"; then
info "Notification sent successfully: ${message}"
else
info "Failed to send notification: ${message}"
fi
}
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
info "Starting backup" info "Starting backup"
@ -75,9 +102,18 @@ fi
if [ ${global_exit} -eq 0 ]; then if [ ${global_exit} -eq 0 ]; then
info "Borg Backups finished successfully" info "Borg Backups finished successfully"
if [ "${NTFY}" = "true" ]; then
ntfy "${HOSTNAME}: Borg Backups finished successfully" "low"
fi
elif [ ${global_exit} -eq 1 ]; then elif [ ${global_exit} -eq 1 ]; then
info "Borg Backups finished with warnings" info "Borg Backups finished with warnings"
if [ "${NTFY}" = "true" ]; then
ntfy "${HOSTNAME}: Borg Backups finished with warnings" "urgent"
fi
else else
info "Borg Backups finished with errors" info "Borg Backups finished with errors"
if [ "${NTFY}" = "true" ]; then
ntfy "${HOSTNAME}: Borg Backups finished with errors" "urgent"
fi
fi fi
exit ${global_exit} exit ${global_exit}