zkt26/sk1/backup.sh
2026-05-12 23:55:32 +02:00

97 lines
3.1 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
set -euo pipefail
# ==========================================
# LOGGER — source the shared library
# ==========================================
# shellcheck source=lib/logger.sh
source "$(dirname "$0")/lib/logger.sh" "backup"
trap 'log_error "Failure at line $LINENO. See $LOG_FILE"' ERR
# ==========================================
# VARIABLES
# ==========================================
NAMESPACE="vigimeteo"
DB_LABEL="app=vigimeteo-db"
DB_USER_OVERRIDE="${DB_USER:-postgres}"
DB_NAME="postgres"
OUTPUT_DIR="$(dirname "$0")/sql"
TIMESTAMP="$(date +%Y%m%d_%H%M%S)"
OUTPUT_FILE="${OUTPUT_DIR}/backup_${TIMESTAMP}.sql"
# ==========================================
log_info "========================================"
log_info " Vigimétéo Database Backup"
log_info " Full log : $LOG_FILE"
log_info "========================================"
# ==========================================
log_info "STEP 1 — Locate the PostgreSQL pod"
# ==========================================
log_debug "Looking for a running pod with label '$DB_LABEL' in namespace '$NAMESPACE'..."
POD=$(kubectl get pod \
--namespace "$NAMESPACE" \
--selector "$DB_LABEL" \
--field-selector="status.phase=Running" \
-o jsonpath='{.items[0].metadata.name}' 2>/dev/null || true)
if [ -z "$POD" ]; then
log_error "No running pod found with label '$DB_LABEL' in namespace '$NAMESPACE'."
log_error "Is the cluster running? kubectl get pods -n $NAMESPACE"
exit 1
fi
log_info "Target pod: $POD"
# ==========================================
log_info "STEP 2 — Verify database readiness"
# ==========================================
log_debug "Checking pg_isready on pod '$POD'..."
if ! kubectl exec --namespace "$NAMESPACE" "$POD" -- \
pg_isready -U "$DB_USER_OVERRIDE" -d "$DB_NAME" -q; then
log_error "PostgreSQL is not ready on pod '$POD'. Aborting backup."
exit 1
fi
log_info "PostgreSQL is ready."
# ==========================================
log_info "STEP 3 — Run pg_dump and save to $OUTPUT_FILE"
# ==========================================
mkdir -p "$OUTPUT_DIR"
log_info "Dumping database '$DB_NAME' (user: $DB_USER_OVERRIDE)..."
kubectl exec --namespace "$NAMESPACE" "$POD" -- \
pg_dump \
--username "$DB_USER_OVERRIDE" \
--dbname "$DB_NAME" \
--clean \
--if-exists \
--no-password \
> "$OUTPUT_FILE"
# Verify the file was produced and is non-empty
if [ ! -s "$OUTPUT_FILE" ]; then
log_error "Backup file '$OUTPUT_FILE' is empty or was not created."
rm -f "$OUTPUT_FILE"
exit 1
fi
FILESIZE=$(du -sh "$OUTPUT_FILE" | cut -f1)
log_info "Backup written: $OUTPUT_FILE ($FILESIZE)"
# ==========================================
log_info "========================================"
log_info "BACKUP COMPLETE"
log_info " File : $OUTPUT_FILE"
log_info " Size : $FILESIZE"
log_info " Log : $LOG_FILE"
log_info ""
log_info "To restore:"
log_info " bash backup.sh # create a new backup first"
log_info " kubectl exec -i -n $NAMESPACE $POD -- \\"
log_info " psql -U $DB_USER_OVERRIDE -d $DB_NAME < $OUTPUT_FILE"
log_info "========================================"