Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(update): pg_dump pre-upgrade backup for PostgreSQL installs

Juan Denisa61eab1509a1
diff --git a/scripts/test/test_update.sh b/scripts/test/test_update.sh
index d584dc60..edeeca58 100644
--- a/scripts/test/test_update.sh
+++ b/scripts/test/test_update.sh
@@ -377,6 +377,51 @@ else
bad "a racy plain-cp of the live serverkit.db is still in update.sh"
fi
+# --------------------------------------------------------------------------
+# T10e — PostgreSQL installs get a real pre-upgrade safety net: backup_current
+# must pg_dump the external DB (sanitizing SQLAlchemy driver suffixes libpq
+# can't parse) and must HALT when the dump fails — updating a shared,
+# migrate-in-place database with no dump is flying without a parachute.
+# --------------------------------------------------------------------------
+t="$WORK/t10e"; mkdir -p "$t/serverkit" "$t/backups"
+printf 'DATABASE_URL=postgresql+psycopg2://user:[email protected]:5432/serverkit\n' > "$t/serverkit/.env"
+cat > "$STUB_BIN/pg_dump" <<EOF
+#!/usr/bin/env bash
+printf '%s\n' "\$1" > "$WORK/t10e/url.txt"
+out=""; prev=""
+for a in "\$@"; do [ "\$prev" = "-f" ] && out="\$a"; prev="\$a"; done
+: > "\$out"
+exit \${PG_DUMP_RC:-0}
+EOF
+chmod +x "$STUB_BIN/pg_dump"
+bk_rc=0
+(
+ set -Eeuo pipefail
+ DRY_RUN=0 INSTALL_DIR="$t/serverkit" BACKUP_DIR="$t/backups"
+ backup_current
+) >/dev/null 2>&1 || bk_rc=$?
+dump_file="$(ls "$t/backups"/serverkit-pre-upgrade-*.dump 2>/dev/null | head -1)"
+if [ "$bk_rc" -eq 0 ] && [ -n "$dump_file" ] \
+ && [ "$(cat "$WORK/t10e/url.txt" 2>/dev/null)" = "postgresql://user:[email protected]:5432/serverkit" ]; then
+ ok "backup_current pg_dumps PostgreSQL installs (driver suffix sanitized)"
+else
+ bad "backup_current postgres: rc=$bk_rc, dump=[$dump_file], url=[$(cat "$WORK/t10e/url.txt" 2>/dev/null)]"
+fi
+rm -rf "$t/backups"; mkdir -p "$t/backups"
+bk_rc=0
+(
+ set -Eeuo pipefail
+ DRY_RUN=0 INSTALL_DIR="$t/serverkit" BACKUP_DIR="$t/backups"
+ export PG_DUMP_RC=1
+ backup_current
+) >/dev/null 2>&1 || bk_rc=$?
+if [ "$bk_rc" -ne 0 ]; then
+ ok "backup_current halts when the PostgreSQL dump fails (no safety net, no update)"
+else
+ bad "backup_current proceeded despite a failed pg_dump (rc=0)"
+fi
+rm -f "$STUB_BIN/pg_dump"
+
# --------------------------------------------------------------------------
# T11 — zero-downtime regression: reload_nginx_graceful must RELOAD a running
# nginx and must NEVER stop it. Host nginx fronts every managed app, so a stop
diff --git a/scripts/update.sh b/scripts/update.sh
index 2e6b7a51..6cedbf90 100644
--- a/scripts/update.sh
+++ b/scripts/update.sh
@@ -861,6 +861,22 @@ backup_current() {
run_or_dry copy_sqlite_db "$db_file" "$backup_file" \
|| halt "Database backup failed — refusing to update without a safety net"
good "Database backed up to $backup_file"
+ elif grep -qE '^DATABASE_URL=postgres' "$active/.env" 2>/dev/null; then
+ # PostgreSQL: the DB is external and SHARED between slots — there is
+ # no per-slot file to snapshot, and the migration runs in place. A
+ # pre-upgrade dump is the only safety net AND the only way back:
+ # without it, a rollback hands the old code a newer schema.
+ backup_file="$BACKUP_DIR/serverkit-pre-upgrade-$(date +%Y%m%d-%H%M%S).dump"
+ command -v pg_dump >/dev/null 2>&1 \
+ || halt "PostgreSQL install but pg_dump is unavailable — install postgresql-client so the update has a safety net"
+ # Strip SQLAlchemy driver suffixes (postgresql+psycopg2://) that
+ # libpq can't parse, and any wrapping quotes from the .env value.
+ local pg_url
+ pg_url="$(grep -E '^DATABASE_URL=' "$active/.env" | head -1 | cut -d= -f2- \
+ | tr -d "\"'" | sed -E 's|^postgres(ql)?\+[a-z0-9]+://|postgresql://|')"
+ run_or_dry pg_dump "$pg_url" -Fc -f "$backup_file" \
+ || halt "PostgreSQL backup failed — refusing to update without a safety net"
+ good "Database backed up to $backup_file"
else
warn "No SQLite database at $db_file — skipping DB backup"
fi