fixing control plane backup too
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 2s
Test Kubernetes backup scripts / test-automated-nfs-backup (push) Successful in 3s
Test Kubernetes backup scripts / test-k3s-control-plane-config-backup (push) Successful in 4s

This commit is contained in:
2026-07-22 08:38:47 -03:00
parent cafbb01200
commit 0f7c56d7eb
3 changed files with 43 additions and 32 deletions

View File

@@ -44,13 +44,14 @@ DATE_BIN="${DATE_BIN:-date}"
REALPATH_BIN="${REALPATH_BIN:-realpath}"
REMOTE_BASH_BIN="${REMOTE_BASH_BIN:-bash}"
# Notifications follow the JSON formats used by automated-nfs-backup.sh.
# Notifications use the Haven Notify v2 backup and error contracts.
# Set either URL to an empty value to disable that notification.
NOTIFY_SUCCESS_URL="${NOTIFY_SUCCESS_URL-http://notify.haven/template/notify/backup}"
NOTIFY_FAILURE_URL="${NOTIFY_FAILURE_URL-http://notify.haven/template/notify/error}"
NOTIFY_SUCCESS_URL="${NOTIFY_SUCCESS_URL-http://notify.haven/api/v2/notifications/backup}"
NOTIFY_FAILURE_URL="${NOTIFY_FAILURE_URL-http://notify.haven/api/v2/notifications/error}"
NOTIFY_TITLE="${NOTIFY_TITLE:-Kubernetes}"
NOTIFY_ASSET="${NOTIFY_ASSET:-k3s control-plane config}"
NOTIFY_CALLER="${NOTIFY_CALLER:-K3s control-plane config backup}"
NOTIFY_SOURCE="${NOTIFY_SOURCE:-$NOTIFY_CALLER}"
declare -a ARCHIVE_ENTRIES=()
declare -a SERVICE_FILE_LIST=()
@@ -66,6 +67,7 @@ ARCHIVE_SIZE_BYTES=0
CLEANUP_KEEP_COUNT_INT=0
LOCK_FD=""
FAILURE_NOTIFICATION_SENT=0
BACKUP_STARTED_SECONDS="$SECONDS"
json_escape() {
local value="${1:-}"
@@ -75,18 +77,10 @@ json_escape() {
printf '%s' "$value"
}
backup_size_mb() {
local bytes="${1:-0}"
if (( bytes <= 0 )); then
printf '0'
return 0
fi
printf '%s' "$(( (bytes + 1048575) / 1048576 ))"
}
send_backup_notification() {
local url="${1:-}"
local size_mb="${2:-0}"
local size_bytes="${2:-0}"
local duration_seconds="${3:-0}"
[[ -z "$url" ]] && return 0
local payload
@@ -94,7 +88,9 @@ send_backup_notification() {
{
"title": "$(json_escape "$NOTIFY_TITLE")",
"asset": "$(json_escape "$NOTIFY_ASSET")",
"backupSizeInMB": ${size_mb}
"sizeBytes": ${size_bytes},
"durationSeconds": ${duration_seconds},
"source": "$(json_escape "$NOTIFY_SOURCE")"
}
EOF
)
@@ -109,19 +105,20 @@ EOF
send_backup_failure_notification() {
local url="${1:-}"
local size_mb="${2:-0}"
local size_bytes="${2:-0}"
[[ -z "$url" ]] && return 0
local payload
payload=$(cat <<EOF
{
"caller": "$(json_escape "$NOTIFY_CALLER")",
"source": "$(json_escape "$NOTIFY_SOURCE")",
"message": "Control-plane configuration backup failed",
"critical": true,
"severity": "critical",
"errorCode": "K3S_CONTROL_PLANE_BACKUP_FAILED",
"extra": [
{
"name": "Backup Size (MB)",
"value": "${size_mb}"
"name": "Archive size",
"value": "${size_bytes} B"
}
]
}
@@ -141,7 +138,7 @@ notify_failure_once() {
return 0
fi
FAILURE_NOTIFICATION_SENT=1
send_backup_failure_notification "$NOTIFY_FAILURE_URL" "$(backup_size_mb "$ARCHIVE_SIZE_BYTES")" || true
send_backup_failure_notification "$NOTIFY_FAILURE_URL" "$ARCHIVE_SIZE_BYTES" || true
}
cleanup() {
@@ -469,7 +466,8 @@ main() {
# Notifications are informational: an unavailable notification endpoint
# must not change the result of a completed backup.
send_backup_notification "$NOTIFY_SUCCESS_URL" "$(backup_size_mb "$ARCHIVE_SIZE_BYTES")" || true
local duration_seconds=$((SECONDS - BACKUP_STARTED_SECONDS))
send_backup_notification "$NOTIFY_SUCCESS_URL" "$ARCHIVE_SIZE_BYTES" "$duration_seconds" || true
}
main "$@"