Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(restore): resolve target paths from the restored configuration

Yudhi Armyndharis293f0dda6d6b
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index 6544e9ab5..7ff55bfb4 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -631,7 +631,9 @@ OPENWA_DATA_DIR=/srv/openwa/data \
>
> The scripts resolve every other path the way the application does: an explicit environment value
> first, then `./.env`, then `<data dir>/.env.generated`. Settings made through Dashboard >
-> Infrastructure therefore apply without being restated on the command line. Two caveats when
+> Infrastructure therefore apply without being restated on the command line. A restore reads that
+> third layer from the archive's `.env.generated` when the archive carries one, because that copy
+> replaces the target's and is the one the restored app reads. Two caveats when
> operating directly on the host mount: a path recorded inside the container (`/app/data/...`) is not
> host-visible, so override it in the environment; and a value written with quotes or a trailing `#`
> comment is reported and skipped rather than guessed at, so pass those explicitly too.
diff --git a/scripts/lib-env.sh b/scripts/lib-env.sh
index 351d85716..a0a86bd0a 100644
--- a/scripts/lib-env.sh
+++ b/scripts/lib-env.sh
@@ -40,8 +40,12 @@ openwa_env_file_value() {
printf '%s' "$value"
}
-# openwa_resolve <key> <default> — the application's precedence: environment, then ./.env, then
-# <data dir>/.env.generated, then the built-in default. Requires DATA_DIR to be set by the caller.
+# Layer 3. Set here rather than read from the environment, so it can never arrive from an operator's
+# shell; restore.sh points it at the archive's copy, which replaces this file during the restore.
+OPENWA_GENERATED_ENV="${DATA_DIR:-./data}/.env.generated"
+
+# openwa_resolve <key> <default> - the application's precedence: environment, then ./.env, then
+# $OPENWA_GENERATED_ENV, then the built-in default. Requires DATA_DIR to be set before sourcing.
openwa_resolve() {
local key="$1" fallback="$2" current value layer
current="$(printenv "$key" 2>/dev/null || true)"
@@ -49,7 +53,7 @@ openwa_resolve() {
printf '%s' "$current"
return 0
fi
- for layer in "./.env" "${DATA_DIR:-./data}/.env.generated"; do
+ for layer in "./.env" "$OPENWA_GENERATED_ENV"; do
value="$(openwa_env_file_value "$layer" "$key")"
if [ -n "$value" ]; then
printf '%s' "$value"
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 9d2fcc222..4bbba9afb 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -70,27 +70,6 @@ done
DATA_DIR="${OPENWA_DATA_DIR:-./data}"
# shellcheck source=scripts/lib-env.sh
. "$(dirname "$0")/lib-env.sh"
-# Database targets resolve exactly like the app: an explicit environment value, then ./.env, then the
-# dashboard's <data dir>/.env.generated, else the fixed ./data defaults. They may legitimately live
-# outside OPENWA_DATA_DIR. This reads the config of the install being restored INTO, which is why it
-# happens here rather than after the archive's own .env.generated is written over it further down.
-MAIN_DB="$(openwa_resolve MAIN_DATABASE_NAME ./data/main.sqlite)"
-DATA_DB="$(openwa_resolve DATABASE_NAME ./data/openwa.sqlite)"
-SESSIONS_DIR="$(openwa_resolve SESSION_DATA_PATH "$DATA_DIR/sessions")"
-BAILEYS_DIR="$(openwa_resolve BAILEYS_AUTH_DIR "$DATA_DIR/baileys")"
-MEDIA_DIR="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media")"
-# Installed plugin code. The app defaults this to <dataDir>/plugins — the same tree as the
-# registry and each plugin's ctx.storage below — so an unset PLUGINS_DIR must resolve there
-# too, or the archive silently omits the plugin packages.
-PLUGIN_PACKAGES_DIR="$(openwa_resolve PLUGINS_DIR "$DATA_DIR/plugins")"
-# Plugin registry + every plugin's persisted ctx.storage. The app puts them at <dataDir>/plugins,
-# where dataDir is PLUGIN_STATE_DIR when that is set and ./data otherwise, so the knob has to be
-# resolved here exactly like PLUGINS_DIR above. Hardcoding $DATA_DIR/plugins meant an operator who
-# moved plugin state got an archive with neither the registry nor any plugin's storage in it, and
-# a restore that put nothing back. Resolved under its own name because the knob names the ROOT,
-# not the plugins directory inside it.
-PLUGIN_STATE_ROOT="$(openwa_resolve PLUGIN_STATE_DIR "$DATA_DIR")"
-PLUGIN_STATE_DIR="$PLUGIN_STATE_ROOT/plugins"
RESTORE_TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
RESOLVED_CWD="$(pwd -P)"
@@ -287,6 +266,32 @@ while IFS= read -r entry; do
done < <(tar -tzf "$ARCHIVE")
tar -xzf "$ARCHIVE" -C "$STAGE"
+# Targets resolve exactly like the app: an explicit environment value, then ./.env, then the
+# dashboard's .env.generated, else the fixed ./data defaults. They may legitimately live outside
+# OPENWA_DATA_DIR. The archive's .env.generated replaces the target's further down, so when it carries
+# one, that copy is the third layer: the restored app reads its paths, and state placed where the
+# replaced file pointed would never be opened. Resolved before anything is validated or written.
+if [ -f "$STAGE/.env.generated" ]; then
+ OPENWA_GENERATED_ENV="$STAGE/.env.generated"
+fi
+MAIN_DB="$(openwa_resolve MAIN_DATABASE_NAME ./data/main.sqlite)"
+DATA_DB="$(openwa_resolve DATABASE_NAME ./data/openwa.sqlite)"
+SESSIONS_DIR="$(openwa_resolve SESSION_DATA_PATH "$DATA_DIR/sessions")"
+BAILEYS_DIR="$(openwa_resolve BAILEYS_AUTH_DIR "$DATA_DIR/baileys")"
+MEDIA_DIR="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media")"
+# Installed plugin code. The app defaults this to <dataDir>/plugins — the same tree as the
+# registry and each plugin's ctx.storage below — so an unset PLUGINS_DIR must resolve there
+# too, or the archive silently omits the plugin packages.
+PLUGIN_PACKAGES_DIR="$(openwa_resolve PLUGINS_DIR "$DATA_DIR/plugins")"
+# Plugin registry + every plugin's persisted ctx.storage. The app puts them at <dataDir>/plugins,
+# where dataDir is PLUGIN_STATE_DIR when that is set and ./data otherwise, so the knob has to be
+# resolved here exactly like PLUGINS_DIR above. Hardcoding $DATA_DIR/plugins meant an operator who
+# moved plugin state got an archive with neither the registry nor any plugin's storage in it, and
+# a restore that put nothing back. Resolved under its own name because the knob names the ROOT,
+# not the plugins directory inside it.
+PLUGIN_STATE_ROOT="$(openwa_resolve PLUGIN_STATE_DIR "$DATA_DIR")"
+PLUGIN_STATE_DIR="$PLUGIN_STATE_ROOT/plugins"
+
# backup.sh archives state directories by content. An archive from before it followed symlinks
# carries a link instead, which on this host may point at the very directory the restore empties
# before refilling it from that link. Refuse it before any existing state is touched.
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index 81c8bd64a..4e4f8f355 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -9,7 +9,7 @@
# (d) the cp fallback writes a CONSISTENCY-WARNING marker into the archive, restore warns
# but continues, and restore --strict refuses
# (e) the archive min-content check rejects (and deletes) an archive missing a required DB
-# (f) data/.env.generated supplies paths the environment does not
+# (f) data/.env.generated supplies paths the environment does not, and restore reads the archive's copy
# (g) PLUGIN_STATE_DIR plugin state is archived and restored at the configured root
# (h) restore refuses a live target without --force, before touching anything
# (i) the data-store half of that guard refuses on its own
@@ -26,6 +26,7 @@
# without sqlite3)
# (r) a symlinked state dir is archived by content and restored through the link, and an archive
# member that is itself a symlink is refused
+# (s) restored state lands where the restored data/.env.generated points, below ./.env
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -290,18 +291,25 @@ fi
if [ "$(db_fingerprint "$F/extract/openwa.sqlite")" != "foxtrot-live-data" ]; then
fail "(f) backup archived the stale default data DB instead of the one data/.env.generated names"
fi
-# restore.sh must read the SAME layer, or it writes the databases somewhere backup.sh never looked.
+# restore.sh must read the same layer, from the file in effect AFTER the restore: the archive's
+# .env.generated replaces the target's, so databases placed where the target's old file pointed
+# would be ones the restored app never opens. Here the live databases are gone, as in a disaster
+# recovery, and the target's own file still names other paths.
+rm -f "$F/live/auth.sqlite" "$F/live/store.sqlite"
printf 'DATABASE_TYPE=sqlite\nMAIN_DATABASE_NAME=%s\nDATABASE_NAME=%s\n' \
"$F/restore/auth.sqlite" "$F/restore/store.sqlite" >"$F/restore/state/.env.generated"
(
cd "$F/restore"
OPENWA_DATA_DIR="$F/restore/state" "$RESTORE" "$ARCHIVE_F" >/dev/null
)
-if [ "$(db_fingerprint "$F/restore/auth.sqlite")" != "foxtrot-live-main" ]; then
- fail "(f) restore ignored the MAIN_DATABASE_NAME in data/.env.generated"
+if [ "$(db_fingerprint "$F/live/auth.sqlite")" != "foxtrot-live-main" ]; then
+ fail "(f) restore ignored the MAIN_DATABASE_NAME in the archive's data/.env.generated"
fi
-if [ "$(db_fingerprint "$F/restore/store.sqlite")" != "foxtrot-live-data" ]; then
- fail "(f) restore ignored the DATABASE_NAME in data/.env.generated"
+if [ "$(db_fingerprint "$F/live/store.sqlite")" != "foxtrot-live-data" ]; then
+ fail "(f) restore ignored the DATABASE_NAME in the archive's data/.env.generated"
+fi
+if [ -e "$F/restore/auth.sqlite" ] || [ -e "$F/restore/store.sqlite" ]; then
+ fail "(f) restore wrote the databases where the replaced data/.env.generated pointed"
fi
# An explicit environment value must still win — that is the app's precedence, not ours to change.
(
@@ -778,5 +786,46 @@ if [ "$(db_fingerprint "$R/live/main.sqlite")" != "romeo-live-main" ] || [ ! -f
fi
pass "(r) a symlinked media dir is archived by content, refilled through the link, and a linked member is refused"
+echo ""
+echo "==> (s) state lands where the restored data/.env.generated points"
+# Dashboard > Infrastructure writes SESSION_DATA_PATH and STORAGE_LOCAL_PATH to data/.env.generated.
+# The restore installs the archive's copy of that file, so the state has to go where it points, not
+# to the defaults a fresh target would otherwise resolve.
+S="$WORK/s"
+mkdir -p "$S/src/data/custom-sessions/session-s1" "$S/src/data/custom-media" "$S/dst" "$S/dst-env"
+make_fixture "$S/src/data/main.sqlite" "sierra-main"
+make_fixture "$S/src/data/openwa.sqlite" "sierra-data"
+printf 'sierra-session\n' >"$S/src/data/custom-sessions/session-s1/marker"
+printf 'sierra-media\n' >"$S/src/data/custom-media/a.jpg"
+printf 'SESSION_DATA_PATH=./data/custom-sessions\nSTORAGE_LOCAL_PATH=./data/custom-media\n' >"$S/src/data/.env.generated"
+(
+ cd "$S/src"
+ BACKUP_DIR="$S/out" "$BACKUP" >/dev/null
+)
+ARCHIVE_S="$(ls "$S"/out/openwa-backup-*.tar.gz)"
+(
+ cd "$S/dst"
+ "$RESTORE" "$ARCHIVE_S" >/dev/null
+)
+if [ "$(cat "$S/dst/data/custom-sessions/session-s1/marker" 2>/dev/null || true)" != "sierra-session" ]; then
+ fail "(s) the sessions did not land at the SESSION_DATA_PATH the restored data/.env.generated names"
+fi
+if [ "$(cat "$S/dst/data/custom-media/a.jpg" 2>/dev/null || true)" != "sierra-media" ]; then
+ fail "(s) the media did not land at the STORAGE_LOCAL_PATH the restored data/.env.generated names"
+fi
+if [ -e "$S/dst/data/sessions" ] || [ -e "$S/dst/data/media" ]; then
+ fail "(s) state was restored to the default paths as well"
+fi
+# ./.env still wins over the restored file, as it does in the app.
+printf 'SESSION_DATA_PATH=./data/env-sessions\n' >"$S/dst-env/.env"
+(
+ cd "$S/dst-env"
+ "$RESTORE" "$ARCHIVE_S" >/dev/null
+)
+if [ "$(cat "$S/dst-env/data/env-sessions/session-s1/marker" 2>/dev/null || true)" != "sierra-session" ]; then
+ fail "(s) a SESSION_DATA_PATH in ./.env lost to the restored data/.env.generated"
+fi
+pass "(s) restored state follows the restored data/.env.generated, and ./.env still wins"
+
echo ""
echo "All smoke tests passed!"