Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): refuse restore into a live database without --force

Yudhi Armyndharisd5d39f69bedf
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index eb9c67395..7921de0ab 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -604,7 +604,9 @@ docker compose down
# 2. Restore from an archive produced by scripts/backup.sh
# (databases land on MAIN_DATABASE_NAME / DATABASE_NAME, default ./data/... — the same paths
# the app reads; non-DB state follows OPENWA_DATA_DIR. Pass --strict to refuse an archive
-# whose CONSISTENCY-WARNING marker reports plain-copied, possibly-torn database snapshots)
+# whose CONSISTENCY-WARNING marker reports plain-copied, possibly-torn database snapshots.
+# Restoring over an existing install's live databases requires --force; without it the script
+# refuses to overwrite them)
./scripts/restore.sh ./backups/openwa-backup-<timestamp>.tar.gz
# 3. (Postgres only) the archive contains database.sql — import it manually:
diff --git a/scripts/restore.sh b/scripts/restore.sh
index f9e56cb57..4af64e970 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -7,11 +7,13 @@
# PostgreSQL dumps are staged for the explicit psql import printed at the end of the restore.
#
# Usage:
-# ./scripts/restore.sh <backup-archive.tar.gz> [--strict]
+# ./scripts/restore.sh <backup-archive.tar.gz> [--strict] [--force]
# Options:
# --strict refuse to restore an archive whose CONSISTENCY-WARNING marker reports
# plain-copied (possibly torn) database snapshots; without it the restore
# continues after a loud warning
+# --force overwrite databases that already hold a working install's data; without it
+# the restore refuses to touch a live target before changing anything
# Environment:
# MAIN_DATABASE_NAME restore target for the auth/audit DB (default: ./data/main.sqlite)
# DATABASE_NAME restore target for the SQLite data store (default: ./data/openwa.sqlite)
@@ -31,25 +33,29 @@ set -euo pipefail
umask 077
STRICT=0
+FORCE=0
ARCHIVE=""
for arg in "$@"; do
case "$arg" in
--strict)
STRICT=1
;;
+ --force)
+ FORCE=1
+ ;;
-h | --help)
- echo "Usage: $0 <backup-archive.tar.gz> [--strict]"
+ echo "Usage: $0 <backup-archive.tar.gz> [--strict] [--force]"
exit 0
;;
-*)
echo "Unknown option: $arg" >&2
- echo "Usage: $0 <backup-archive.tar.gz> [--strict]" >&2
+ echo "Usage: $0 <backup-archive.tar.gz> [--strict] [--force]" >&2
exit 1
;;
*)
if [ -n "$ARCHIVE" ]; then
echo "Unexpected extra argument: $arg" >&2
- echo "Usage: $0 <backup-archive.tar.gz> [--strict]" >&2
+ echo "Usage: $0 <backup-archive.tar.gz> [--strict] [--force]" >&2
exit 1
fi
ARCHIVE="$arg"
@@ -182,8 +188,24 @@ snapshot_external_db() {
esac
}
+# A restore target that already holds a working install's tables is LIVE: overwriting it destroys
+# real data, and the pre-restore snapshot is a convenience, not a recovery guarantee. Probe with
+# the same sqlite3 CLI backup.sh snapshots with; a missing file — or one with no tables yet, as a
+# fresh install leaves behind — is safe to restore over. Without the CLI there is no way to prove
+# the file empty, so any non-empty target counts as live rather than guessed safe.
+db_appears_live() {
+ target="$1"
+ [ -f "$target" ] || return 1
+ if command -v sqlite3 >/dev/null 2>&1; then
+ tables="$(sqlite3 "$target" "SELECT count(*) FROM sqlite_master;" 2>/dev/null)" || return 0
+ [ "${tables:-0}" -gt 0 ]
+ else
+ [ -s "$target" ]
+ fi
+}
+
if [ -z "$ARCHIVE" ] || [ ! -f "$ARCHIVE" ]; then
- echo "Usage: $0 <backup-archive.tar.gz> [--strict]" >&2
+ echo "Usage: $0 <backup-archive.tar.gz> [--strict] [--force]" >&2
exit 1
fi
@@ -216,6 +238,24 @@ if [ -f "$STAGE/CONSISTENCY-WARNING" ]; then
log "WARN: continuing anyway; verify data integrity after the restore (re-run with --strict to make this fatal)"
fi
+# Refuse to overwrite a live database without --force, BEFORE any existing state is touched (the
+# same rule the --strict refusal above follows). Only the databases this archive actually carries
+# are checked — a target the archive omits is left alone either way.
+if [ "$FORCE" -ne 1 ]; then
+ LIVE_TARGETS=""
+ if [ -f "$STAGE/main.sqlite" ] && db_appears_live "$MAIN_DB"; then
+ LIVE_TARGETS="$LIVE_TARGETS $MAIN_DB"
+ fi
+ if [ -f "$STAGE/openwa.sqlite" ] && db_appears_live "$DATA_DB"; then
+ LIVE_TARGETS="$LIVE_TARGETS $DATA_DB"
+ fi
+ if [ -n "$LIVE_TARGETS" ]; then
+ echo "[restore] ERROR: database target(s) appear live:$LIVE_TARGETS" >&2
+ echo "[restore] they already hold a working install's data — stop the app, then re-run with --force to overwrite them" >&2
+ exit 1
+ fi
+fi
+
# Safety snapshot of whatever is there now.
if [ -d "$DATA_DIR" ] && [ -n "$(ls -A "$DATA_DIR" 2>/dev/null || true)" ]; then
SAFETY="${DATA_DIR%/}.pre-restore-$RESTORE_TIMESTAMP"