This commit is contained in:
30
backup.sh
30
backup.sh
@@ -115,16 +115,26 @@ cleanup_old_backups() {
|
||||
done
|
||||
}
|
||||
|
||||
# Function to send notification (placeholder for webhook/email integration)
|
||||
# Function to send notification to healthchecks.io
|
||||
send_notification() {
|
||||
local status="$1"
|
||||
local message="$2"
|
||||
|
||||
if [[ -n "${WEBHOOK_URL:-}" ]]; then
|
||||
curl -X POST "${WEBHOOK_URL}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"status\": \"${status}\", \"message\": \"${message}\", \"timestamp\": \"$(date -Iseconds)\"}" \
|
||||
|| log "Failed to send notification"
|
||||
if [[ -n "${HEALTHCHECKS_URL:-}" ]]; then
|
||||
case "$status" in
|
||||
"start")
|
||||
# Ping start endpoint
|
||||
curl -fsS -m 10 --retry 5 "${HEALTHCHECKS_URL}/start" > /dev/null || log "Failed to send start notification"
|
||||
;;
|
||||
"success")
|
||||
# Ping success endpoint (default)
|
||||
curl -fsS -m 10 --retry 5 "${HEALTHCHECKS_URL}" > /dev/null || log "Failed to send success notification"
|
||||
;;
|
||||
"error"|"fail")
|
||||
# Ping fail endpoint with log data
|
||||
curl -fsS -m 10 --retry 5 --data-raw "$message" "${HEALTHCHECKS_URL}/fail" > /dev/null || log "Failed to send failure notification"
|
||||
;;
|
||||
esac
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -132,9 +142,13 @@ send_notification() {
|
||||
main() {
|
||||
log "Starting PostgreSQL backup process"
|
||||
|
||||
# Send start notification
|
||||
send_notification "start" "PostgreSQL backup process started"
|
||||
|
||||
# Validate required environment variables
|
||||
if [[ -z "${S3_BUCKET}" || -z "${S3_ACCESS_KEY_ID}" || -z "${S3_SECRET_ACCESS_KEY}" || -z "${S3_ENDPOINT}" ]]; then
|
||||
log "ERROR: Missing required environment variables (S3_BUCKET, S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY, S3_ENDPOINT)"
|
||||
send_notification "fail" "Missing required environment variables"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -145,7 +159,7 @@ main() {
|
||||
log "Testing database connection"
|
||||
if ! PGPASSWORD="${POSTGRES_PASSWORD}" psql -h "${POSTGRES_HOST}" -p "${POSTGRES_PORT}" -U "${POSTGRES_USER}" -d postgres -c "SELECT 1" > /dev/null 2>&1; then
|
||||
log "ERROR: Cannot connect to PostgreSQL database"
|
||||
send_notification "error" "Cannot connect to PostgreSQL database"
|
||||
send_notification "fail" "Cannot connect to PostgreSQL database"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -169,7 +183,7 @@ main() {
|
||||
send_notification "success" "All PostgreSQL database backups completed successfully"
|
||||
else
|
||||
log "Some database backups failed"
|
||||
send_notification "error" "Some PostgreSQL database backups failed"
|
||||
send_notification "fail" "Some PostgreSQL database backups failed"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user