feat: add Gotify notifications for Restic backup status
- Implemented a function to send notifications via Gotify for backup success, warnings, and failures. - Notifications include details such as date and commit information. - Added checks for Gotify configuration and fallback for JSON encoding.
This commit is contained in:
@@ -9,6 +9,44 @@ if [ -f "$HOME/.restic-env" ]; then
|
||||
. "$HOME/.restic-env"
|
||||
fi
|
||||
|
||||
# Function to send Gotify notification
|
||||
send_gotify_notification() {
|
||||
local title="$1"
|
||||
local message="$2"
|
||||
local priority="${3:-5}"
|
||||
|
||||
# Check if Gotify is configured
|
||||
if [ -z "$GOTIFY_URL" ] || [ -z "$GOTIFY_APP_TOKEN" ]; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Send notification (fire and forget, don't fail on error)
|
||||
# Use jq if available for proper JSON encoding, otherwise use simple approach
|
||||
if command -v jq >/dev/null 2>&1; then
|
||||
local json_payload
|
||||
json_payload=$(jq -n \
|
||||
--arg title "$title" \
|
||||
--arg message "$message" \
|
||||
--argjson priority "$priority" \
|
||||
'{title: $title, message: $message, priority: $priority}')
|
||||
|
||||
curl -sSf -X POST "${GOTIFY_URL}/message?token=${GOTIFY_APP_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$json_payload" \
|
||||
>/dev/null 2>&1 || true
|
||||
else
|
||||
# Fallback: simple JSON encoding (replace " with \" and newlines with \n)
|
||||
local escaped_title escaped_message
|
||||
escaped_title=$(echo "$title" | sed 's/"/\\"/g')
|
||||
escaped_message=$(echo "$message" | sed 's/"/\\"/g' | sed ':a;N;$!ba;s/\n/\\n/g')
|
||||
|
||||
curl -sSf -X POST "${GOTIFY_URL}/message?token=${GOTIFY_APP_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"title\":\"${escaped_title}\",\"message\":\"${escaped_message}\",\"priority\":${priority}}" \
|
||||
>/dev/null 2>&1 || true
|
||||
fi
|
||||
}
|
||||
|
||||
echo "💾 Creating Restic backup..."
|
||||
|
||||
if ! command -v restic >/dev/null 2>&1; then
|
||||
@@ -71,12 +109,33 @@ restic -r "$RESTIC_REPO" backup \
|
||||
|
||||
if [ $RESTIC_EXIT_CODE -eq 0 ]; then
|
||||
echo "✅ Restic backup completed successfully"
|
||||
|
||||
# Send success notification
|
||||
send_gotify_notification \
|
||||
"Hördle Backup: Erfolgreich" \
|
||||
"Restic Backup wurde erfolgreich abgeschlossen.\nDatum: ${CURRENT_DATE}\nCommit: ${CURRENT_COMMIT_SHORT}" \
|
||||
5
|
||||
|
||||
exit 0
|
||||
elif [ $RESTIC_EXIT_CODE -eq 3 ]; then
|
||||
echo "⚠️ Restic backup completed with warnings (some files could not be read), continuing..."
|
||||
|
||||
# Send warning notification
|
||||
send_gotify_notification \
|
||||
"Hördle Backup: Mit Warnungen" \
|
||||
"Restic Backup wurde mit Warnungen abgeschlossen (einige Dateien konnten nicht gelesen werden).\nDatum: ${CURRENT_DATE}\nCommit: ${CURRENT_COMMIT_SHORT}" \
|
||||
7
|
||||
|
||||
exit 0
|
||||
else
|
||||
echo "⚠️ Restic backup failed (exit code: $RESTIC_EXIT_CODE), continuing deployment..."
|
||||
|
||||
# Send error notification
|
||||
send_gotify_notification \
|
||||
"Hördle Backup: Fehlgeschlagen" \
|
||||
"Restic Backup ist fehlgeschlagen (Exit Code: ${RESTIC_EXIT_CODE}).\nDatum: ${CURRENT_DATE}\nCommit: ${CURRENT_COMMIT_SHORT}" \
|
||||
9
|
||||
|
||||
exit 0
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user