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): pick the backup engine by DATABASE_URL, not by which files exist

Juan Denis5bf5e50f624b
diff --git a/.github/workflows/discord-notify.yml b/.github/workflows/discord-notify.yml
index 2053f13b..76e77969 100644
--- a/.github/workflows/discord-notify.yml
+++ b/.github/workflows/discord-notify.yml
@@ -16,6 +16,17 @@ jobs:
run: |
set -euo pipefail
+ # Repository secrets are NOT exposed to `pull_request` runs triggered
+ # from a fork, so a merged fork PR would reach curl with an empty URL
+ # and fail the job for no useful reason. Skip cleanly instead. The
+ # fix is deliberately NOT `pull_request_target`: that would hand a
+ # write-scoped token and this webhook to an outsider-triggered run,
+ # which is a poor trade for a chat notification.
+ if [ -z "${DISCORD_WEBHOOK_URL:-}" ]; then
+ echo "DISCORD_WEBHOOK_URL unavailable (fork PR?) — skipping notification."
+ exit 0
+ fi
+
# Read everything from the event payload via jq: PR text is
# attacker-controlled input, so it must never be interpolated
# into the script (backticks/$() would execute).
diff --git a/scripts/test/test_update.sh b/scripts/test/test_update.sh
index 31e4d388..e9472265 100644
--- a/scripts/test/test_update.sh
+++ b/scripts/test/test_update.sh
@@ -421,6 +421,54 @@ if [ "$bk_rc" -ne 0 ]; then
else
bad "backup_current proceeded despite a failed pg_dump (rc=0)"
fi
+
+# The engine is decided by DATABASE_URL, never by which files happen to exist.
+# An install migrated SQLite -> PostgreSQL keeps its old serverkit.db (every
+# deploy copies it forward on file-existence alone), and backing THAT up
+# instead of dumping left PRE_UPGRADE_DUMP unset — quietly disabling
+# migrate_database's auto-restore, which reads it as "${PRE_UPGRADE_DUMP:-}",
+# on the one engine that migrates in place and cannot roll back by switching
+# slots. Needs no python3: the PostgreSQL path is pg_dump + pg_restore only.
+t="$WORK/t10e2"; mkdir -p "$t/serverkit/backend/instance" "$t/backups"
+printf 'DATABASE_URL=postgresql://user:[email protected]:5432/serverkit\n' > "$t/serverkit/.env"
+printf 'stale sqlite file left over from before the postgres migration\n' \
+ > "$t/serverkit/backend/instance/serverkit.db"
+lo_rc=0
+(
+ set -Eeuo pipefail
+ DRY_RUN=0 INSTALL_DIR="$t/serverkit" BACKUP_DIR="$t/backups"
+ backup_current
+ printf '%s\n' "${PRE_UPGRADE_DUMP:-}" > "$t/dump-var.txt"
+) >/dev/null 2>&1 || lo_rc=$?
+lo_dump="$(ls "$t/backups"/serverkit-pre-upgrade-*.dump 2>/dev/null | head -1)"
+lo_db="$(ls "$t/backups"/serverkit-pre-upgrade-*.db 2>/dev/null | head -1)"
+if [ "$lo_rc" -eq 0 ] && [ -n "$lo_dump" ] && [ -z "$lo_db" ] \
+ && [ "$(cat "$t/dump-var.txt" 2>/dev/null)" = "$lo_dump" ]; then
+ ok "a leftover serverkit.db never diverts a PostgreSQL install away from pg_dump"
+else
+ bad "pg install w/ stale sqlite file: rc=$lo_rc, dump=[$lo_dump], db=[$lo_db], PRE_UPGRADE_DUMP=[$(cat "$t/dump-var.txt" 2>/dev/null)]"
+fi
+
+# An engine this updater cannot dump (MySQL) must say so. "No SQLite database"
+# reads like a benign nothing-to-do while the update proceeds with no safety
+# net at all. The warning names the SCHEME only — a DATABASE_URL carries a
+# password, and this text lands in the update log.
+t="$WORK/t10e3"; mkdir -p "$t/serverkit" "$t/backups"
+printf 'DATABASE_URL=mysql://user:[email protected]:3306/serverkit\n' > "$t/serverkit/.env"
+my_rc=0
+my_out="$(
+ set -Eeuo pipefail
+ DRY_RUN=0 INSTALL_DIR="$t/serverkit" BACKUP_DIR="$t/backups"
+ backup_current 2>&1
+)" || my_rc=$?
+if [ "$my_rc" -eq 0 ] \
+ && printf '%s' "$my_out" | grep -q "mysql" \
+ && printf '%s' "$my_out" | grep -q "NO database backup" \
+ && ! printf '%s' "$my_out" | grep -q "hunter2"; then
+ ok "an undumpable engine is named in the warning, without leaking the password"
+else
+ bad "mysql warning: rc=$my_rc, out=[$my_out]"
+fi
rm -f "$STUB_BIN/pg_dump" "$STUB_BIN/pg_restore"
# --------------------------------------------------------------------------
diff --git a/scripts/update.sh b/scripts/update.sh
index e715ee4c..a0a19bdb 100644
--- a/scripts/update.sh
+++ b/scripts/update.sh
@@ -840,8 +840,8 @@ atomic_switch() {
# "database disk image is malformed" — which then sinks the migration in the
# new slot, and silently poisons the pre-upgrade backup that was supposed to
# be the safety net. The SQLite online backup API snapshots a consistent DB
-# even while the panel is writing. Python 3.11/3.12 is guaranteed by
-# pre-flight; without it we fail rather than fall back to a racy cp.
+# even while the panel is writing, using any interpreter that can import
+# sqlite3; with none available we fail rather than fall back to a racy cp.
# ---------------------------------------------------------------------------
# Snapshotting or integrity-checking a SQLite file needs nothing more than an
# interpreter carrying the stdlib `sqlite3` module — NOT the 3.11/3.12 that
@@ -930,22 +930,17 @@ backup_current() {
active="$(active_real_dir)"
db_file="$active/backend/instance/serverkit.db"
- if [ -f "$db_file" ]; then
- backup_file="$BACKUP_DIR/serverkit-pre-upgrade-$(date +%Y%m%d-%H%M%S).db"
- # A torn backup is worse than none: it looks like a safety net but
- # restores a malformed DB. Snapshot consistently, VERIFY the result,
- # and refuse to proceed without a proven-valid net. Failed or invalid
- # artifacts are removed so they are never mistaken for a good backup.
- if ! run_or_dry copy_sqlite_db "$db_file" "$backup_file"; then
- rm -f "$backup_file"
- halt "Database backup failed — refusing to update without a safety net"
- fi
- if [ "$DRY_RUN" != "1" ] && ! verify_sqlite_db "$backup_file"; then
- rm -f "$backup_file"
- halt "Database backup verification failed — refusing to update without a VALID safety net"
- fi
- good "Database backed up to $backup_file"
- elif grep -qE '^DATABASE_URL=postgres' "$active/.env" 2>/dev/null; then
+ # Branch on the DECLARED engine, not on which files happen to exist. An
+ # install migrated SQLite -> PostgreSQL keeps its old serverkit.db on disk
+ # (both deploy paths copy it forward on file-existence alone, so it is
+ # carried into every future slot), and a file-first test then backed up
+ # that abandoned file, skipped the dump, and left PRE_UPGRADE_DUMP unset —
+ # silently disabling the auto-restore in migrate_database, which reads it
+ # as "${PRE_UPGRADE_DUMP:-}" and just does nothing. PostgreSQL is the ONE
+ # install type that migrates in place and cannot roll back by switching
+ # slots, so that dump is its only way home. migrate_database has always
+ # keyed off DATABASE_URL; this keeps the two functions in agreement.
+ if 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:
@@ -967,8 +962,34 @@ backup_current() {
# automatic restore if the in-place migration fails.
PRE_UPGRADE_DUMP="$backup_file"
good "Database backed up to $backup_file"
+ elif [ -f "$db_file" ]; then
+ backup_file="$BACKUP_DIR/serverkit-pre-upgrade-$(date +%Y%m%d-%H%M%S).db"
+ # A torn backup is worse than none: it looks like a safety net but
+ # restores a malformed DB. Snapshot consistently, VERIFY the result,
+ # and refuse to proceed without a proven-valid net. Failed or invalid
+ # artifacts are removed so they are never mistaken for a good backup.
+ if ! run_or_dry copy_sqlite_db "$db_file" "$backup_file"; then
+ rm -f "$backup_file"
+ halt "Database backup failed — refusing to update without a safety net"
+ fi
+ if [ "$DRY_RUN" != "1" ] && ! verify_sqlite_db "$backup_file"; then
+ rm -f "$backup_file"
+ halt "Database backup verification failed — refusing to update without a VALID safety net"
+ fi
+ good "Database backed up to $backup_file"
else
- warn "No SQLite database at $db_file — skipping DB backup"
+ # Neither engine matched. Say which one, and say plainly that the
+ # update is proceeding unprotected — "No SQLite database" reads like a
+ # benign nothing-to-do on, say, a MySQL install that in fact has no
+ # safety net at all.
+ local db_scheme
+ db_scheme="$(grep -E '^DATABASE_URL=' "$active/.env" 2>/dev/null | head -1 \
+ | cut -d= -f2- | tr -d "\"'" | cut -d: -f1)"
+ if [ -n "$db_scheme" ] && [ "$db_scheme" != "sqlite" ]; then
+ warn "DATABASE_URL uses '$db_scheme', which this updater cannot dump — proceeding with NO database backup"
+ else
+ warn "No SQLite database at $db_file — skipping DB backup"
+ fi
fi
local tree_backup