feat: make gotify optional (#16)
All checks were successful
All checks were successful
Reviewed-on: #16 Co-authored-by: Timo Behrendt <t.behrendt@t00n.de> Co-committed-by: Timo Behrendt <t.behrendt@t00n.de>
This commit was merged in pull request #16.
This commit is contained in:
20
README.md
20
README.md
@@ -1,6 +1,6 @@
|
||||
# BackupSidecar
|
||||
|
||||
BackupSidecar is a lightweight backup solution designed to run as a cron job in Kubernetes. It automates backups using Restic and supports both directory and PostgreSQL database backups. Notifications are sent via Gotify to keep you informed of backup results.
|
||||
BackupSidecar is a lightweight backup solution designed to run as a cron job in Kubernetes. It automates backups using Restic and supports both directory and PostgreSQL database backups. Optional notifications can be sent via Gotify to keep you informed of backup results.
|
||||
|
||||
## Configuration
|
||||
|
||||
@@ -15,9 +15,10 @@ These variables apply to both directory and PostgreSQL backups.
|
||||
- **`RESTIC_REPOSITORY`** _(required)_ - The URI of the Restic repository (e.g., `rest:http://your-rest-server:8000/backup`).
|
||||
- **`RESTIC_REST_USERNAME`** _(optional)_ - The username for REST server authentication.
|
||||
- **`RESTIC_REST_PASSWORD`** _(optional)_ - The password for REST server authentication.
|
||||
- **`GOTIFYHOST`** _(required)_ - The Gotify server URL.
|
||||
- **`GOTIFYTOKEN`** _(required)_ - The API token for Gotify.
|
||||
- **`GOTIFYTOPIC`** _(required)_ - The topic under which backup notifications will be sent.
|
||||
- **`ENABLE_GOTIFY`** _(optional)_ - Enable Gotify notifications. Set to `true` to enable, any other value or unset disables notifications. Defaults to `true`.
|
||||
- **`GOTIFYHOST`** _(required when ENABLE_GOTIFY=true)_ - The Gotify server URL.
|
||||
- **`GOTIFYTOKEN`** _(required when ENABLE_GOTIFY=true)_ - The API token for Gotify.
|
||||
- **`GOTIFYTOPIC`** _(required when ENABLE_GOTIFY=true)_ - The topic under which backup notifications will be sent.
|
||||
|
||||
### Directory Backup
|
||||
|
||||
@@ -82,6 +83,8 @@ spec:
|
||||
value: "directory" # or "postgres"
|
||||
- name: SOURCEDIR
|
||||
value: "/data/source"
|
||||
- name: ENABLE_GOTIFY
|
||||
value: "true"
|
||||
- name: GOTIFYHOST
|
||||
value: "http://gotify.example.com"
|
||||
- name: GOTIFYTOKEN
|
||||
@@ -104,10 +107,17 @@ spec:
|
||||
|
||||
## Notifications
|
||||
|
||||
The script sends success or failure notifications via Gotify.
|
||||
The script can send success or failure notifications via Gotify when enabled. To enable notifications, set `ENABLE_GOTIFY=true` and provide the required Gotify configuration variables (`GOTIFYHOST`, `GOTIFYTOKEN`, `GOTIFYTOPIC`). When notifications are disabled, backup status messages are still logged to the console.
|
||||
|
||||
Example success notification:
|
||||
|
||||
```
|
||||
Backup successful. Snapshot 56ff6a909a44e01f67d2d88f9a76aa713d437809d7ed14a2361e28893f38befb: files new: 1, files changed: 0, data added: 1019 bytes in 0.277535184 sec
|
||||
```
|
||||
|
||||
When Gotify is disabled, you'll see a single message at startup indicating notifications are disabled, followed by normal backup status messages:
|
||||
|
||||
```
|
||||
2024-01-15T10:30:00 - Gotify notifications disabled. Backup status will be logged to console only.
|
||||
2024-01-15T10:30:05 - Backup successful. Snapshot 56ff6a909a44e01f67d2d88f9a76aa713d437809d7ed14a2361e28893f38befb: files new: 1, files changed: 0, data added: 1019 bytes in 0.277535184 sec
|
||||
```
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
#######################################
|
||||
# Date format for logging.
|
||||
#######################################
|
||||
LOG_DATE_FORMAT="%Y-%m-%dT%T"
|
||||
|
||||
#######################################
|
||||
# Log a message with a timestamp.
|
||||
# Arguments:
|
||||
# Message to log.
|
||||
#######################################
|
||||
log() {
|
||||
echo "$(date +"$LOG_DATE_FORMAT") - $*"
|
||||
}
|
||||
|
||||
#######################################
|
||||
# Determine backup mode from the environment only.
|
||||
# Valid values: "directory" or "postgres".
|
||||
@@ -18,7 +32,7 @@ fi
|
||||
|
||||
for cmd in "${REQUIRED_CMDS[@]}"; do
|
||||
if ! command -v "$cmd" &>/dev/null; then
|
||||
echo "Error: Required command '$cmd' is not installed." >&2
|
||||
log "Error: Required command '$cmd' is not installed."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
@@ -26,10 +40,17 @@ done
|
||||
#######################################
|
||||
# Validate common required environment variables.
|
||||
#######################################
|
||||
# Gotify notification settings.
|
||||
: "${GOTIFYHOST:?Environment variable GOTIFYHOST is not set}"
|
||||
: "${GOTIFYTOKEN:?Environment variable GOTIFYTOKEN is not set}"
|
||||
: "${GOTIFYTOPIC:?Environment variable GOTIFYTOPIC is not set}"
|
||||
# Gotify notification settings (optional).
|
||||
# Set ENABLE_GOTIFY to "true" to enable notifications, any other value or unset disables them.
|
||||
ENABLE_GOTIFY="${ENABLE_GOTIFY:-true}"
|
||||
|
||||
if [ "$ENABLE_GOTIFY" = "true" ]; then
|
||||
: "${GOTIFYHOST:?Environment variable GOTIFYHOST is not set (required when ENABLE_GOTIFY=true)}"
|
||||
: "${GOTIFYTOKEN:?Environment variable GOTIFYTOKEN is not set (required when ENABLE_GOTIFY=true)}"
|
||||
: "${GOTIFYTOPIC:?Environment variable GOTIFYTOPIC is not set (required when ENABLE_GOTIFY=true)}"
|
||||
else
|
||||
log "Gotify notifications disabled. Backup status will be logged to console only."
|
||||
fi
|
||||
|
||||
# Restic encryption password.
|
||||
: "${RESTIC_PASSWORD:?Environment variable RESTIC_PASSWORD is not set}"
|
||||
@@ -62,23 +83,11 @@ case "$BACKUP_MODE" in
|
||||
esac
|
||||
|
||||
#######################################
|
||||
# Build the Gotify URL.
|
||||
# Build the Gotify URL (only if Gotify is enabled).
|
||||
#######################################
|
||||
if [ "$ENABLE_GOTIFY" = "true" ]; then
|
||||
GOTIFYURL="${GOTIFYHOST}/message?token=${GOTIFYTOKEN}"
|
||||
|
||||
#######################################
|
||||
# Date format for logging.
|
||||
#######################################
|
||||
LOG_DATE_FORMAT="%Y-%m-%dT%T"
|
||||
|
||||
#######################################
|
||||
# Log a message with a timestamp.
|
||||
# Arguments:
|
||||
# Message to log.
|
||||
#######################################
|
||||
log() {
|
||||
echo "$(date +"$LOG_DATE_FORMAT") - $*"
|
||||
}
|
||||
fi
|
||||
|
||||
#######################################
|
||||
# Send a notification via Gotify.
|
||||
@@ -87,6 +96,13 @@ log() {
|
||||
#######################################
|
||||
send_notification() {
|
||||
local message="$1"
|
||||
|
||||
# Only send notification if Gotify is enabled
|
||||
if [ "$ENABLE_GOTIFY" != "true" ]; then
|
||||
log "$message"
|
||||
return 0
|
||||
fi
|
||||
|
||||
if ! curl -s -X POST "$GOTIFYURL" -F "title=${GOTIFYTOPIC}" -F "message=${message}" >/dev/null; then
|
||||
log "Warning: Failed to send notification with message: ${message}"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user