From cb59f1f9258662aebac6c8b837e53a75bdfc9300 Mon Sep 17 00:00:00 2001 From: Jun-te Kim Date: Sun, 14 Dec 2025 00:57:19 +0000 Subject: [PATCH] it is what it is --- .../scripts/backup_k8s_storage_to_s3.sh | 112 +++++++++++++----- 1 file changed, 83 insertions(+), 29 deletions(-) diff --git a/mist_infra/scripts/backup_k8s_storage_to_s3.sh b/mist_infra/scripts/backup_k8s_storage_to_s3.sh index 77c5136..e330647 100644 --- a/mist_infra/scripts/backup_k8s_storage_to_s3.sh +++ b/mist_infra/scripts/backup_k8s_storage_to_s3.sh @@ -1,42 +1,96 @@ #!/usr/bin/env bash set -euo pipefail -# ---------------------------------------------------------- -# Weekly full backup of all Kubernetes PV data -# ---------------------------------------------------------- +# -------------------------------------------------- +# Config +# -------------------------------------------------- +BACKUP_DATE="$(date +%Y-%m-%d)" +TMP_DIR="/tmp/backup-${BACKUP_DATE}" -SOURCE_DIR="/k8s_storage" -TMP_DIR="/tmp/mist-backups" -BUCKET="mist-backups" +K8S_STORAGE="/k8s_storage" +S3_BASE="s3://mist-backups/${BACKUP_DATE}" -DATE="$(date -u +%Y-%m-%d)" -TIMESTAMP="$(date -u +%Y-%m-%d_%H-%M-%S)" -HOST="$(hostname)" +POSTGRES_NAMESPACE="default" +POSTGRES_POD_LABEL="app=postgres" +POSTGRES_USER="postgres" +POSTGRES_DB="stripe_invoice" -ARCHIVE_NAME="k8s_storage_${HOST}_${TIMESTAMP}.tar.gz" -ARCHIVE_PATH="${TMP_DIR}/${ARCHIVE_NAME}" +mkdir -p "$TMP_DIR/postgres" -echo "=== Mist weekly PV backup ===" -echo "Source: ${SOURCE_DIR}" -echo "Archive: ${ARCHIVE_PATH}" -echo "Bucket: s3://${BUCKET}/${DATE}/" +echo "=== Backup date: $BACKUP_DATE ===" -mkdir -p "${TMP_DIR}" +# -------------------------------------------------- +# 1. Discover schemas (for documentation) +# -------------------------------------------------- +echo "=== Discovering Postgres schemas ===" + +POSTGRES_POD="$(kubectl get pods -n "$POSTGRES_NAMESPACE" \ + -l "$POSTGRES_POD_LABEL" \ + -o jsonpath='{.items[0].metadata.name}')" + +SCHEMAS="$(kubectl exec -n "$POSTGRES_NAMESPACE" "$POSTGRES_POD" -- \ + psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -Atc \ + "SELECT schema_name FROM information_schema.schemata + WHERE schema_name NOT IN ('pg_catalog', 'information_schema') + ORDER BY schema_name;")" + +# Write README +{ + echo "Database: ${POSTGRES_DB}" + echo "Schemas:" + for s in $SCHEMAS; do + echo "- $s" + done + echo + echo "Backup date: ${BACKUP_DATE}" + echo "Host: mist" +} > "$TMP_DIR/postgres/README.txt" + +echo "✓ Schemas documented" + +# -------------------------------------------------- +# 2. Postgres logical backup (FULL DB) +# -------------------------------------------------- +echo "=== Dumping Postgres database ===" + +kubectl exec -n "$POSTGRES_NAMESPACE" "$POSTGRES_POD" -- \ + pg_dump -U "$POSTGRES_USER" "$POSTGRES_DB" \ + | gzip > "$TMP_DIR/postgres/stripe_invoice.sql.gz" + +echo "✓ Postgres dump complete" + +# -------------------------------------------------- +# 3. Filesystem backup (best-effort) +# -------------------------------------------------- +echo "=== Archiving /k8s_storage ===" -echo "→ Creating tarball" tar \ - --numeric-owner \ - --xattrs \ - --acls \ - -czf "${ARCHIVE_PATH}" \ - -C "${SOURCE_DIR}" . + --ignore-failed-read \ + --warning=no-file-changed \ + -czf "$TMP_DIR/k8s_storage.tar.gz" \ + -C "$K8S_STORAGE" . -echo "→ Uploading to S3" -aws s3 cp \ - "${ARCHIVE_PATH}" \ - "s3://${BUCKET}/${DATE}/${ARCHIVE_NAME}" +echo "✓ Filesystem archive complete" -echo "→ Cleaning up local temp" -rm -f "${ARCHIVE_PATH}" +# -------------------------------------------------- +# 4. Upload to S3 +# -------------------------------------------------- +echo "=== Uploading to S3 ===" -echo "✅ Backup complete" +aws s3 cp "$TMP_DIR/k8s_storage.tar.gz" \ + "${S3_BASE}/k8s_storage.tar.gz" + +aws s3 cp "$TMP_DIR/postgres/stripe_invoice.sql.gz" \ + "${S3_BASE}/postgres/stripe_invoice.sql.gz" + +aws s3 cp "$TMP_DIR/postgres/README.txt" \ + "${S3_BASE}/postgres/README.txt" + +echo "✓ Upload complete" + +# -------------------------------------------------- +# 5. Cleanup +# -------------------------------------------------- +rm -rf "$TMP_DIR" + +echo "=== Backup finished successfully ==="