feat: enhance Gotify notification handling in backup script
- Added loading of environment variables from a .env file. - Extracted Gotify configuration from docker-compose.yml if not set. - Improved notification sending with success and error messages based on curl exit codes. - Ensured Gotify notifications are only sent if properly configured.
This commit is contained in:
@@ -9,6 +9,23 @@ if [ -f "$HOME/.restic-env" ]; then
|
||||
. "$HOME/.restic-env"
|
||||
fi
|
||||
|
||||
# Load .env file if present (for Gotify variables)
|
||||
if [ -f ".env" ]; then
|
||||
# shellcheck source=/dev/null
|
||||
set -a
|
||||
. ".env"
|
||||
set +a
|
||||
fi
|
||||
|
||||
# Extract Gotify variables from docker-compose.yml if not set
|
||||
if [ -z "$GOTIFY_URL" ] && [ -f "docker-compose.yml" ]; then
|
||||
GOTIFY_URL=$(grep -oP 'GOTIFY_URL=\K[^\s]+' docker-compose.yml | head -1 | tr -d '"' | tr -d "'" || echo "")
|
||||
fi
|
||||
|
||||
if [ -z "$GOTIFY_APP_TOKEN" ] && [ -f "docker-compose.yml" ]; then
|
||||
GOTIFY_APP_TOKEN=$(grep -oP 'GOTIFY_APP_TOKEN=\K[^\s]+' docker-compose.yml | head -1 | tr -d '"' | tr -d "'" || echo "")
|
||||
fi
|
||||
|
||||
# Function to send Gotify notification
|
||||
send_gotify_notification() {
|
||||
local title="$1"
|
||||
@@ -17,9 +34,12 @@ send_gotify_notification() {
|
||||
|
||||
# Check if Gotify is configured
|
||||
if [ -z "$GOTIFY_URL" ] || [ -z "$GOTIFY_APP_TOKEN" ]; then
|
||||
echo "⚠️ Gotify not configured (GOTIFY_URL or GOTIFY_APP_TOKEN not set), skipping notification"
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo "📢 Sending Gotify notification..."
|
||||
|
||||
# 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
|
||||
@@ -30,20 +50,34 @@ send_gotify_notification() {
|
||||
--argjson priority "$priority" \
|
||||
'{title: $title, message: $message, priority: $priority}')
|
||||
|
||||
local curl_exit_code=0
|
||||
curl -sSf -X POST "${GOTIFY_URL}/message?token=${GOTIFY_APP_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$json_payload" \
|
||||
>/dev/null 2>&1 || true
|
||||
>/dev/null 2>&1 || curl_exit_code=$?
|
||||
|
||||
if [ $curl_exit_code -eq 0 ]; then
|
||||
echo "✅ Gotify notification sent successfully"
|
||||
else
|
||||
echo "⚠️ Failed to send Gotify notification (curl exit code: $curl_exit_code)"
|
||||
fi
|
||||
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')
|
||||
|
||||
local curl_exit_code=0
|
||||
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
|
||||
>/dev/null 2>&1 || curl_exit_code=$?
|
||||
|
||||
if [ $curl_exit_code -eq 0 ]; then
|
||||
echo "✅ Gotify notification sent successfully"
|
||||
else
|
||||
echo "⚠️ Failed to send Gotify notification (curl exit code: $curl_exit_code)"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -138,4 +172,3 @@ else
|
||||
|
||||
exit 0
|
||||
fi
|
||||
|
||||
|
||||
Reference in New Issue
Block a user