This commit is contained in:
Jun-te Kim 2025-12-14 01:41:45 +00:00
parent 222f2d5779
commit c8573acbc1

View file

@ -6,7 +6,6 @@ set -euo pipefail
# ----------------------------- # -----------------------------
NAMESPACE="default" NAMESPACE="default"
PG_ENABLED=true PG_ENABLED=true
PG_SECRET_NAME="postgres-secret" PG_SECRET_NAME="postgres-secret"
PG_POD_SELECTOR="app=postgres" PG_POD_SELECTOR="app=postgres"
@ -31,10 +30,16 @@ echo "=== Backup started at $(date -u) ==="
if [[ "$PG_ENABLED" == "true" ]]; then if [[ "$PG_ENABLED" == "true" ]]; then
echo "=== Postgres pg_dump enabled ===" echo "=== Postgres pg_dump enabled ==="
POSTGRES_POD=$(kubectl get pod \ POSTGRES_POD=$(kubectl get pods \
-n "$NAMESPACE" \ -n "$NAMESPACE" \
-l "$PG_POD_SELECTOR" \ -l "$PG_POD_SELECTOR" \
-o jsonpath='{.items[0].metadata.name}') -o jsonpath='{.items[*].metadata.name}' | awk '{print $1}')
if [[ -z "$POSTGRES_POD" ]]; then
echo "❌ No Postgres pod found"
kubectl get pods -n "$NAMESPACE"
exit 1
fi
POSTGRES_USER=$(kubectl get secret "$PG_SECRET_NAME" \ POSTGRES_USER=$(kubectl get secret "$PG_SECRET_NAME" \
-n "$NAMESPACE" \ -n "$NAMESPACE" \
@ -53,6 +58,14 @@ if [[ "$PG_ENABLED" == "true" ]]; then
echo "✔ pg_dump complete ($(du -h "$BACKUP_DIR/postgres.sql" | cut -f1))" echo "✔ pg_dump complete ($(du -h "$BACKUP_DIR/postgres.sql" | cut -f1))"
fi fi
# -----------------------------
# Normalise permissions (IMPORTANT)
# -----------------------------
echo "=== Normalising permissions on $K8S_STORAGE_ROOT ==="
# Ensure backup user can read everything, without breaking apps
sudo chmod -R a+rX "$K8S_STORAGE_ROOT" || true
# ----------------------------- # -----------------------------
# Build tar exclude args # Build tar exclude args
# ----------------------------- # -----------------------------
@ -66,11 +79,14 @@ done
# ----------------------------- # -----------------------------
echo "=== Archiving k8s storage ===" echo "=== Archiving k8s storage ==="
tar -czf "$BACKUP_DIR/k8s_storage_$DATE.tar.gz" \ tar \
--ignore-failed-read \
--warning=no-file-changed \
-czf "$BACKUP_DIR/k8s_storage_$DATE.tar.gz" \
"${TAR_EXCLUDE_ARGS[@]}" \ "${TAR_EXCLUDE_ARGS[@]}" \
"$K8S_STORAGE_ROOT" "$K8S_STORAGE_ROOT"
echo "✔ k8s_storage archived" echo "✔ k8s_storage archived ($(du -h "$BACKUP_DIR/k8s_storage_$DATE.tar.gz" | cut -f1))"
# ----------------------------- # -----------------------------
# Upload to S3 # Upload to S3
@ -85,7 +101,9 @@ echo "✔ Uploaded to $S3_BUCKET"
# Restore instructions # Restore instructions
# ----------------------------- # -----------------------------
echo "" echo ""
echo "=== RESTORE GUIDE ===" echo "========================================"
echo "=== RESTORE GUIDE"
echo "========================================"
echo "" echo ""
echo "Restore volumes:" echo "Restore volumes:"
echo " sudo tar -xzf k8s_storage_$DATE.tar.gz -C /" echo " sudo tar -xzf k8s_storage_$DATE.tar.gz -C /"