Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): stop backup and restore on unparseable env lines (#1783)

Yudhi Armyndharis00b6045e2377
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ac874fb2d..200ffd477 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Stop `backup.sh` and `restore.sh` on an env line they cannot parse instead of falling back to the default path.
- Verify the PostgreSQL server certificate in `backup.sh` when `DATABASE_SSL=true`, as the app does.
- Dead-letter undispatched ingress events instead of deleting them when they age out of the dedup window.
- Keep a re-sent ingress delivery redrivable when an earlier dead letter for its id was already redriven.
@@ -61,6 +62,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Upgrade notes (behavior changes)
+- `backup.sh` and `restore.sh` exit with status 2 when `./.env` or `.env.generated` sets a key in a form they cannot parse; fix the line or pass the key in the environment.
- With `DATABASE_SSL=true`, `backup.sh` now fails against a PostgreSQL server whose certificate the app would reject; trust a private CA through `NODE_EXTRA_CA_CERTS` or `PGSSLROOTCERT`.
## [0.24.0] - 2026-10-03
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index abe8210b4..fcc6b53f9 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -726,12 +726,35 @@ OPENWA_DATA_DIR=/srv/openwa/data \
> first, then `./.env`, then `<data dir>/.env.generated`. Settings made through Dashboard >
> 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 quoted value followed by a `#` comment, a
-> double-quoted value with backslash escapes, or a `KEY: value` line is reported and resolves to the
-> script default, so pass those explicitly too. Blanks around `=`, CRLF line endings, a value in one
-> pair of quotes and a `#` comment after an unquoted value are read as the app reads them.
+> replaces the target's and is the one the restored app reads. 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.
+>
+> On every install, inside a container or not, a quoted value followed by a `#` comment or not
+> closed on its line, a quoted value containing its own quote character or ending in a backslash, a
+> double-quoted value with a backslash, or a `KEY: value` line in `./.env` or `.env.generated` stops
+> the script with an error naming the key, before anything is archived or restored. So does a bare
+> CR, U+2028 or U+2029 on any line naming the key, a comment included, since the app starts a new
+> line there; a NUL or a byte that is not UTF-8 on a line setting the key; a byte-order mark or a
+> Unicode blank (such as a no-break space) before the key, around its `=` or at either end of its
+> value; a bare `NAME:` line right before the key's line, which the app reads as `NAME`'s value;
+> and a key whose value the app takes from a later line, either a bare `KEY` line followed, past
+> any blank lines, by one starting with `=` or an empty `KEY=` followed by a quoted value. The app
+> reads such a line, so neither the script default nor a guess is safe to use. `DATABASE_NAME` is
+> not read by a PostgreSQL backup through `DATABASE_URL` or by the restore of a PostgreSQL archive,
+> and an unreadable `ENGINE_TYPE` only skips the warning about missing Baileys state.
+> Move the comment to its own line and keep the quotes (an unquoted value ends at a `#` and loses
+> its outer blanks), wrap the value in a quote character it does not contain, single-quote a value
+> whose backslashes are literal and not at its end (inside double quotes the app turns `\n` and `\r`
+> into line breaks; if that is intended, pass the key in the environment), write `KEY=value` on one
+> line for `KEY: value` and for a value on a later line, give a bare `NAME:` line a value or remove
+> it, save the file as UTF-8 with ASCII blanks and LF or CRLF line endings, or pass the key in the
+> environment. A restore reads the archive's `.env.generated`, which cannot be edited in place, so a
+> key it holds in such a form has to be passed in the environment. Blanks around `=`, CRLF line
+> endings, a value in one pair of quotes and a `#` comment after an unquoted value are read as the
+> app reads them. The scripts read the file line by line, so a line for the key inside a quoted
+> value that spans several lines, another key's or an earlier one for the same key, is taken as a
+> setting, although the app reads it as part of that value.
**Verification:**
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 5583a0f26..ad9bc0974 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -80,7 +80,11 @@ TIMESTAMP="$(date +%Y%m%d-%H%M%S)"
# below only bases the non-DB state directories — deriving DB paths from it would back up files the
# app never reads.
MAIN_DB="$(openwa_resolve MAIN_DATABASE_NAME ./data/main.sqlite)"
-DATA_DB="$(openwa_resolve DATABASE_NAME ./data/openwa.sqlite)"
+# With DATABASE_TYPE=postgres, DATABASE_NAME names the database instead, read below only when pg_dump
+# needs it, so a line for it the scripts cannot parse stops only a backup that uses it.
+if [ "$DATABASE_TYPE" != "postgres" ]; then
+ DATA_DB="$(openwa_resolve DATABASE_NAME ./data/openwa.sqlite)"
+fi
SESSIONS_DIR="$(openwa_resolve SESSION_DATA_PATH "$DATA_DIR/sessions")"
BAILEYS_DIR="$(openwa_resolve BAILEYS_AUTH_DIR "$DATA_DIR/baileys")"
MEDIA_DIR="$(openwa_media_dir)"
@@ -239,12 +243,15 @@ if [ "$DATABASE_TYPE" = "postgres" ]; then
pg_dump "$DATABASE_URL_RESOLVED" >"$STAGE/database.sql"
else
# Same layered resolution as the paths above: a dashboard-provisioned Postgres keeps its
- # connection details in <data dir>/.env.generated, never in the operator's shell.
- PGPASSWORD="$(openwa_resolve DATABASE_PASSWORD '')" pg_dump \
- -h "$(openwa_resolve DATABASE_HOST localhost)" \
- -p "$(openwa_resolve DATABASE_PORT 5432)" \
- -U "$(openwa_resolve DATABASE_USERNAME openwa)" \
- "$(openwa_resolve DATABASE_NAME openwa)" >"$STAGE/database.sql"
+ # connection details in <data dir>/.env.generated, never in the operator's shell. Each is
+ # resolved on its own line, where set -e stops the run on a value the scripts cannot read; inside
+ # the pg_dump arguments that failure would be ignored and the default dumped instead.
+ PG_PASSWORD="$(openwa_resolve DATABASE_PASSWORD '')"
+ PG_HOST="$(openwa_resolve DATABASE_HOST localhost)"
+ PG_PORT="$(openwa_resolve DATABASE_PORT 5432)"
+ PG_USER="$(openwa_resolve DATABASE_USERNAME openwa)"
+ PG_NAME="$(openwa_resolve DATABASE_NAME openwa)"
+ PGPASSWORD="$PG_PASSWORD" pg_dump -h "$PG_HOST" -p "$PG_PORT" -U "$PG_USER" "$PG_NAME" >"$STAGE/database.sql"
fi
REQUIRED_MEMBERS+=("./database.sql")
else
@@ -279,7 +286,10 @@ if [ -d "$BAILEYS_DIR" ]; then
if [ -n "$(find -H "$BAILEYS_DIR" -mindepth 1 -maxdepth 1 -type d -print -quit)" ]; then
record_engine_state_note "baileys/ (recorded whenever Baileys state exists; it cannot show whether it was live)"
fi
-elif [ "$(openwa_resolve ENGINE_TYPE '')" = "baileys" ]; then
+# The warning is advisory, so an ENGINE_TYPE line the scripts cannot parse skips it instead of the backup.
+elif ! ENGINE_TYPE_RESOLVED="$(openwa_resolve ENGINE_TYPE '')"; then
+ log "WARN: ENGINE_TYPE could not be read (see above); skipping only the check for missing Baileys state"
+elif [ "$ENGINE_TYPE_RESOLVED" = "baileys" ]; then
log "WARN: ENGINE_TYPE=baileys but $BAILEYS_DIR was not found — restored sessions will require pairing"
fi
diff --git a/scripts/lib-env.sh b/scripts/lib-env.sh
index 88fea09ca..7a0e99c30 100644
--- a/scripts/lib-env.sh
+++ b/scripts/lib-env.sh
@@ -18,23 +18,84 @@
# Deliberately conservative: only the `KEY=value` forms dotenv reads plainly are honoured. Blanks
# around the `=` and the value, CRLF line endings, a value wrapped in one pair of quotes and a comment
# after an unquoted value are read as dotenv reads them. Anything else (a quoted value followed by a
-# comment, a double-quoted value with escapes, a `KEY: value` line) is reported rather than guessed
-# at, because a silently mis-parsed path is the exact failure this exists to prevent. Nothing here
-# exports anything: each key is looked up by name, so a stray entry in an operator's .env can never
-# reach the script's own environment.
+# comment, containing its own quote character or ending in a backslash, a double-quoted value with
+# escapes, a `KEY: value` line, a bare CR, U+2028 or U+2029 on a line naming the key, a NUL or byte
+# that is not UTF-8 on its line, a byte-order mark or Unicode blank next to the key, its `=` or the
+# ends of its value, a bare `NAME:` line just before it, a bare `KEY` or an empty `KEY=` the app
+# completes from a later line) stops the run rather than being guessed at: the app still reads such a
+# line, so neither a guess nor the default is safe to back up or restore to, and a silently
+# mis-resolved path is the exact failure this exists to prevent. The file is still read line by line:
+# a line for the key inside a quoted value that spans lines, whether another key's or an earlier one
+# of the same key, is taken as a setting, although the app reads it as part of that value. Nothing
+# here exports anything: each key is looked up by name, so a stray entry in an operator's .env can
+# never reach the script's own environment.
# openwa_env_file_value <file> <key> - print the value from one env-file layer. Returns 1 when the layer
-# does not set the key and 2 when it sets it in a form reported below; a blank value succeeds and prints
-# nothing.
+# does not set the key and 2 when it may set it in a form rejected below; a blank value succeeds and
+# prints nothing.
openwa_env_file_value() {
- local file="$1" key="$2" line value
+ local file="$1" key="$2" line match value n next
+ local keyline="^[[:space:]]*(export[[:space:]]+)?${key}[[:space:]]*([=:]|\$)"
+ # The code points beyond ASCII that dotenv's \s matches: the Unicode blanks and the byte-order mark.
+ local blank=$'\xc2\xa0|\xe1\x9a\x80|\xe2\x80[\x80-\x8a\xa8\xa9\xaf]|\xe2\x81\x9f|\xe3\x80\x80|\xef\xbb\xbf'
+ # A line of well-formed UTF-8, without NUL.
+ local utf8=$'([\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec\xee\xef][\x80-\xbf]{2}|\xed[\x80-\x9f][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})*'
[ -f "$file" ] || return 1
- # The last line naming the key wins, as in dotenv. `KEY: value` is matched only to be reported.
- line="$(grep -E "^[[:space:]]*(export[[:space:]]+)?${key}[[:space:]]*[=:]" "$file" 2>/dev/null | tail -n 1)" || true
+ # dotenv also takes a byte-order mark or a Unicode blank (a no-break space, say) for whitespace and a
+ # bare CR, U+2028 or U+2029 for a line break, and decodes the file as UTF-8, so a byte that is not UTF-8
+ # reaches the app as U+FFFD. grep and the trims below do none of that, so the key could be missed or
+ # read with other bytes. Such a line break on any line naming the key, comments included, is rejected,
+ # and so is a line setting the key with a blank before the key, around its `=` or at either end of an
+ # unquoted value, or with a NUL or a byte that is not UTF-8 anywhere. Every grep here runs with -a in
+ # the C locale: otherwise a NUL anywhere in the file, or a byte that is not UTF-8 in a UTF-8 locale, can
+ # hide the key's line. A NUL, which bash would drop from the line, becomes \377.
+ line="$(LC_ALL=C tr '\000' '\377' 2>/dev/null <"$file" | LC_ALL=C grep -aE "(^|[^A-Za-z0-9_.-])${key}([^A-Za-z0-9_.-]|\$)")" || true
+ if ! LC_ALL=C grep -qE $'\r.|\xe2\x80[\xa8\xa9]' <<<"$line"; then
+ line="$(LC_ALL=C grep -aE "^([[:space:]]|$blank)*(export([[:space:]]|$blank)+)?${key}([[:space:]]|$blank)*([=:]|\$)" <<<"$line")" || true
+ [ -z "$line" ] || LC_ALL=C grep -qvE "$keyline" <<<"$line" ||
+ LC_ALL=C grep -qE "^[^=:]*[=:][[:space:]]*($blank)|^[^#]*($blank)[[:space:]]*(#|\$)" <<<"$line" ||
+ LC_ALL=C grep -qvxE "$utf8" <<<"$line" || line=""
+ fi
+ if [ -n "$line" ]; then
+ echo "[config] ERROR: $file has a bare CR, U+2028 or U+2029 on a line naming $key, a NUL or a byte that is" >&2
+ echo "[config] not UTF-8 on a line setting it, or a byte-order mark or Unicode blank next to $key, its =" >&2
+ echo "[config] or the ends of its value. Save it as UTF-8 with plain ASCII blanks and LF or CRLF line" >&2
+ echo "[config] ends, or pass $key in the environment." >&2
+ return 2
+ fi
+ # dotenv reads the line after a bare `NAME:` line as NAME's value, so a line for the key right there
+ # does not set the key for the app, although the scripts would read it. dotenv also starts a line
+ # after a bare CR, U+2028 or U+2029, so `# note<U+2028>NAME:` is such a line too.
+ line="$(LC_ALL=C tr '\000' '\377' 2>/dev/null <"$file" | LC_ALL=C grep -aB1 -E "$keyline")" || true
+ if LC_ALL=C grep -qE "(^|"$'\r|\xe2\x80[\xa8\xa9]'")([[:space:]]|$blank)*(export([[:space:]]|$blank)+)?[A-Za-z0-9_.-]+:"$'\r'"?\$" <<<"$line"; then
+ echo "[config] ERROR: $file has a bare NAME: line just before a line setting $key, which the app reads as" >&2
+ echo "[config] NAME's value instead. Give NAME: a value or remove it, or pass $key in the environment." >&2
+ return 2
+ fi
+ # The last line naming the key wins, as in dotenv. `KEY: value` is matched only so that it stops the
+ # run. dotenv skips a bare `KEY` line (`export KEY`, say), unless the next line that is not blank
+ # starts with `=`: its `KEY\s*=` crosses line breaks, so that pair sets the key and stops the run too.
+ line=""
+ while IFS= read -r match; do
+ value="${match#*"$key"}"
+ if [ -z "${value//[[:space:]]/}" ]; then
+ next="$(LC_ALL=C tail -n +"$((${match%%:*} + 1))" "$file" | LC_ALL=C tr '\000' '\377' |
+ LC_ALL=C grep -avE -m 1 "^([[:space:]]|$blank)*\$")" || true
+ LC_ALL=C grep -qE "^([[:space:]]|$blank)*=" <<<"$next" || continue
+ fi
+ line="$match"
+ done < <(LC_ALL=C grep -anE "$keyline" "$file" 2>/dev/null)
[ -n "$line" ] || return 1
+ n="${line%%:*}"
+ line="${line#*:}"
value="${line#*"$key"}"
value="${value#"${value%%[![:space:]]*}"}"
case "$value" in
+ '')
+ echo "[config] ERROR: $file names $key on a line without an =, which the app pairs with the = that starts" >&2
+ echo "[config] a later line. Write $key=value on one line, or pass $key in the environment." >&2
+ return 2
+ ;;
=*)
value="${value#=}"
# Trim both ends, which also drops the CR of a CRLF line.
@@ -42,6 +103,7 @@ openwa_env_file_value() {
value="${value%"${value##*[![:space:]]}"}"
case "$value" in
\"*\\*) ;; # dotenv expands \n and \r inside double quotes
+ \'*\\\' | \`*\\\`) ;; # dotenv may take the backslash and quote for an escaped quote and read on
\"*\" | \'*\' | \`*\`)
# A quote inside means the pair does not wrap the whole value: a comment ending in one follows it.
case "${value:1:${#value}-2}" in
@@ -53,6 +115,18 @@ openwa_env_file_value() {
esac
;;
\"* | \'* | \`*) ;; # the quote does not close the value, as with a trailing comment
+ '')
+ # dotenv takes a quoted value that starts on a later line, past blank lines, as this key's.
+ next="$(LC_ALL=C tail -n +"$((n + 1))" "$file" | LC_ALL=C tr '\000' '\377' |
+ LC_ALL=C grep -avE -m 1 "^([[:space:]]|$blank)*\$")" || true
+ if LC_ALL=C grep -qE "^([[:space:]]|$blank)*[\"'\`]" <<<"$next"; then
+ echo "[config] ERROR: $file leaves $key empty on its line and starts a quoted value on a later line," >&2
+ echo "[config] which the app can read as $key's value. Write $key=value on one line, or pass $key in the" >&2
+ echo "[config] environment." >&2
+ return 2
+ fi
+ return 0
+ ;;
*)
# An unquoted value ends at a `#`, as in dotenv.
value="${value%%#*}"
@@ -62,8 +136,13 @@ openwa_env_file_value() {
esac
;;
esac
- echo "[config] WARN: $file sets $key in a form these scripts do not parse (a quoted value with a trailing" >&2
- echo "[config] comment or escapes, or KEY: value); using the default. Pass $key in the environment if it matters here." >&2
+ echo "[config] ERROR: $file sets $key in a form these scripts do not parse: a quoted value followed by a" >&2
+ echo "[config] comment or not closed on its line, a quoted value containing its own quote character or" >&2
+ echo "[config] ending in a backslash, a double-quoted value with a backslash, or KEY: value." >&2
+ echo "[config] Move a comment to its own line and keep the quotes, close the quote on the same line, wrap a" >&2
+ echo "[config] value in a quote character it does not contain, single-quote a value whose backslashes are" >&2
+ printf '%s\n' "[config] literal and not at its end (inside double quotes the app turns \\n and \\r into line breaks)," >&2
+ echo "[config] write $key=value for $key: value, or pass $key in the environment." >&2
return 2
}
@@ -87,7 +166,7 @@ openwa_writable() {
# existing ./data/media means ./data/media is in use.
openwa_media_dir() {
local dir
- dir="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media")"
+ dir="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media")" || return
case "$dir" in
./uploads | uploads)
if ! openwa_writable "$dir" || { [ ! -d "$dir" ] && [ -d ./data/media ]; }; then
@@ -106,6 +185,9 @@ 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.
+# Fails, printing nothing, when a layer sets the key in a form openwa_env_file_value rejects. Callers
+# must act on that status: set -e does for a top-level assignment, but not inside a function called
+# from a command substitution or for a substitution in a command's arguments.
openwa_resolve() {
local key="$1" fallback="$2" current value layer rc
current="$(printenv "$key" 2>/dev/null || true)"
@@ -115,7 +197,7 @@ openwa_resolve() {
fi
# The first layer that sets the key ends the lookup, even with a blank value: dotenv sets a blank
# line to '' and never overwrites a key already set, so the app reads its built-in default. A line
- # these scripts cannot parse still sets the key for the app, so it ends the lookup too.
+ # these scripts cannot parse still sets the key for the app, so it fails the lookup instead.
for layer in "./.env" "$OPENWA_GENERATED_ENV"; do
rc=0
value="$(openwa_env_file_value "$layer" "$key")" || rc=$?
@@ -124,10 +206,7 @@ openwa_resolve() {
printf '%s' "${value:-$fallback}"
return 0
;;
- 2)
- printf '%s' "$fallback"
- return 0
- ;;
+ 2) return 2 ;;
esac
done
printf '%s' "$fallback"
diff --git a/scripts/restore.sh b/scripts/restore.sh
index e4e9c7949..8cc05dff4 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -301,9 +301,14 @@ tar -xzf "$ARCHIVE" -C "$STAGE"
# 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"
+ log "Paths not set in the environment or ./.env come from the archive's .env.generated."
fi
MAIN_DB="$(openwa_resolve MAIN_DATABASE_NAME ./data/main.sqlite)"
-DATA_DB="$(openwa_resolve DATABASE_NAME ./data/openwa.sqlite)"
+# DATABASE_NAME is only a target for an archive carrying the SQLite data store; a PostgreSQL dump is
+# loaded by hand, so a line for it the scripts cannot parse does not stop that restore.
+if [ -f "$STAGE/openwa.sqlite" ]; then
+ DATA_DB="$(openwa_resolve DATABASE_NAME ./data/openwa.sqlite)"
+fi
SESSIONS_DIR="$(openwa_resolve SESSION_DATA_PATH "$DATA_DIR/sessions")"
BAILEYS_DIR="$(openwa_resolve BAILEYS_AUTH_DIR "$DATA_DIR/baileys")"
MEDIA_DIR="$(openwa_media_dir)"
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index a414aee41..ed47f4c32 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -27,8 +27,7 @@
# (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
-# (t) ./.env lines with CRLF endings, blanks around = or trailing blanks resolve as dotenv reads them,
-# and a `KEY: value` line is reported
+# (t) ./.env lines with CRLF endings, blanks around = or trailing blanks resolve as dotenv reads them
# (u) a state, database or data-dir file target the restore cannot write stops it before any database
# is written (skipped as root)
# (v) a leftover STORAGE_LOCAL_PATH=./uploads the app cannot create falls back to ./data/media in
@@ -48,8 +47,10 @@
# error still fails it, however long its output and whatever the host's locale
# (ae) the min-content check passes an archive whose listing outgrows a pipe buffer
# (af) the online SQLite backup waits out a writer holding the database lock (skipped without sqlite3)
-# (ag) quoted values, inline comments and `KEY=""` in ./.env resolve as dotenv reads them, and a line
-# the scripts cannot parse ends the lookup at the default instead of reading data/.env.generated
+# (ag) quoted values, inline comments and `KEY=""` in ./.env resolve as dotenv reads them, a NUL on
+# another line does not hide the key, and a line the scripts cannot parse fails the lookup, so
+# backup and restore stop before archiving or writing anything instead of using a default the app
+# may not read
# (ah) pg_dump makes the app's TLS check when DATABASE_SSL=true (falling back to the system CA store
# without node), and connects as before when it is not
#
@@ -62,7 +63,8 @@ set -euo pipefail
# case starts from none of them and sets exactly the paths it uses.
unset OPENWA_DATA_DIR BACKUP_DIR DATABASE_TYPE MAIN_DATABASE_NAME DATABASE_NAME SESSION_DATA_PATH \
BAILEYS_AUTH_DIR STORAGE_LOCAL_PATH PLUGINS_DIR PLUGIN_STATE_DIR OPENWA_RESTORE_SNAPSHOT_DIR BOOTSTRAP_KEY_FILE \
- DATABASE_URL DATABASE_SSL DATABASE_SSL_REJECT_UNAUTHORIZED PGSSLMODE PGSSLROOTCERT
+ ENGINE_TYPE DATABASE_URL DATABASE_HOST DATABASE_PORT DATABASE_USERNAME DATABASE_PASSWORD \
+ DATABASE_SSL DATABASE_SSL_REJECT_UNAUTHORIZED PGSSLMODE PGSSLROOTCERT
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKUP="$REPO_ROOT/scripts/backup.sh"
@@ -866,7 +868,7 @@ make_fixture "$T/src/live/store.sqlite" "tango-data"
make_fixture "$T/src/data/main.sqlite" "STALE-main"
make_fixture "$T/src/data/openwa.sqlite" "STALE-data"
printf 'tango-session\n' >"$T/src/data/sess/session-s1/marker"
-printf 'MAIN_DATABASE_NAME = %s\r\nDATABASE_NAME=%s\r\nSESSION_DATA_PATH=./data/sess \r\nBAILEYS_AUTH_DIR: ./data/bl\r\n' \
+printf 'MAIN_DATABASE_NAME = %s\r\nDATABASE_NAME=%s\r\nSESSION_DATA_PATH=./data/sess \r\n' \
"$T/src/live/auth.sqlite" "$T/src/live/store.sqlite" >"$T/src/.env"
set +e
OUT_T="$(cd "$T/src" && BACKUP_DIR="$T/out" "$BACKUP" 2>&1)"
@@ -886,9 +888,6 @@ fi
if [ "$(cat "$T/extract/sessions/session-s1/marker" 2>/dev/null || true)" != "tango-session" ]; then
fail "(t) a SESSION_DATA_PATH with trailing blanks did not resolve to the sessions dir"
fi
-if ! printf '%s' "$OUT_T" | grep -q 'sets BAILEYS_AUTH_DIR in a form these scripts do not parse'; then
- fail "(t) a \`KEY: value\` line was skipped without the warning"
-fi
printf 'DATABASE_NAME=./data/custom.sqlite\r\nMAIN_DATABASE_NAME = ./data/custom-main.sqlite \r\n' >"$T/dst/.env"
make_fixture "$T/dst/data/custom.sqlite" "tango-live"
set +e
@@ -911,7 +910,7 @@ fi
if [ -n "$(find "$T/dst/data" -name "*$(printf '\r')*" 2>/dev/null)" ]; then
fail "(t) restore created a file whose name ends in a carriage return"
fi
-pass "(t) CRLF, spaced and blank-padded ./.env lines resolve like dotenv, and \`KEY: value\` is reported"
+pass "(t) CRLF, spaced and blank-padded ./.env lines resolve like dotenv"
echo ""
echo "==> (u) a target the restore cannot write stops it before any database is written"
@@ -1407,11 +1406,12 @@ else
fi
echo ""
-echo "==> (ag) quoted, commented and empty-quoted ./.env values resolve as the app reads them"
+echo "==> (ag) quoted, commented and empty-quoted ./.env values resolve as the app reads them, and others stop the run"
# dotenv strips a value's quotes and an unquoted value's comment, and a key it has set keeps
# data/.env.generated from supplying it. The scripts skipped every such line and read the next layer,
# so .env.example's commented PLUGINS_DIR line and a `DATABASE_NAME=""` resolved to values the app
-# never uses.
+# never uses. A line the scripts cannot parse used to resolve to the default with a warning, so a
+# backup archived a stale database at the default path, or no media, and still exited 0.
AG="$WORK/ag"
mkdir -p "$AG/data"
cat >"$AG/.env" <<'ENV'
@@ -1422,26 +1422,285 @@ BAILEYS_AUTH_DIR=./data/bl#inline
SESSION_DATA_PATH="./data/sess" # quoted, then a comment
STORAGE_LOCAL_PATH="./data/media" # see "docs"
PLUGIN_STATE_DIR='./data/state' # it'
+ENGINE_TYPE: baileys
+BOOTSTRAP_KEY_FILE="./data/key\file"
ENV
printf 'DATABASE_NAME=./elsewhere/openwa.sqlite\nSESSION_DATA_PATH=./elsewhere/sess\n' >"$AG/data/.env.generated"
resolve_ag() {
(cd "$AG" && DATA_DIR=./data && . "$REPO_ROOT/scripts/lib-env.sh" && openwa_resolve "$1" "$2") 2>>"$AG/err"
}
-# A blank value and an unparsed line both resolve to the default, never to data/.env.generated.
+# A blank value resolves to the default, never to data/.env.generated.
for check in 'PLUGINS_DIR|./data/plugins' 'DATABASE_NAME|DEFAULT' 'MAIN_DATABASE_NAME|./data/quoted main.sqlite' \
- 'BAILEYS_AUTH_DIR|./data/bl' 'SESSION_DATA_PATH|DEFAULT' 'STORAGE_LOCAL_PATH|DEFAULT' 'PLUGIN_STATE_DIR|DEFAULT'; do
+ 'BAILEYS_AUTH_DIR|./data/bl'; do
key="${check%%|*}"
got="$(resolve_ag "$key" DEFAULT)"
if [ "$got" != "${check#*|}" ]; then
fail "(ag) $key resolved to '$got', expected '${check#*|}'"
fi
done
+# An unparsed line fails the lookup and prints no value: neither the default nor data/.env.generated.
# A comment ending in the value's own quote must not pass for the closing quote.
-if [ "$(grep -c 'do not parse' "$AG/err")" -ne 3 ] || ! grep -q 'sets SESSION_DATA_PATH in a form' "$AG/err" ||
- ! grep -q 'sets STORAGE_LOCAL_PATH in a form' "$AG/err" || ! grep -q 'sets PLUGIN_STATE_DIR in a form' "$AG/err"; then
- fail "(ag) the parse warning did not name exactly the three unparsed lines: $(cat "$AG/err")"
+for key in SESSION_DATA_PATH STORAGE_LOCAL_PATH PLUGIN_STATE_DIR ENGINE_TYPE BOOTSTRAP_KEY_FILE; do
+ set +e
+ got="$(resolve_ag "$key" DEFAULT)"
+ rc=$?
+ set -e
+ if [ "$rc" -eq 0 ] || [ -n "$got" ]; then
+ fail "(ag) $key in a form the scripts cannot parse resolved to '$got' (rc $rc) instead of failing"
+ fi
+ if ! grep -q "sets $key in a form" "$AG/err"; then
+ fail "(ag) the parse error did not name $key: $(cat "$AG/err")"
+ fi
+done
+if [ "$(grep -c 'do not parse' "$AG/err")" -ne 5 ]; then
+ fail "(ag) the parse error did not name exactly the five unparsed lines: $(cat "$AG/err")"
+fi
+
+# Both scripts stop on such a line before archiving or writing anything. The data store sits at a
+# custom path while a stale database is left at the default one, which the backup used to archive.
+AGB="$WORK/ag-backup"
+mkdir -p "$AGB/data/media" "$AGB/dst/data"
+make_fixture "$AGB/data/main.sqlite" "ag-main"
+make_fixture "$AGB/data/active.sqlite" "ag-active"
+make_fixture "$AGB/data/openwa.sqlite" "ag-STALE"
+(cd "$AGB" && BACKUP_DIR="$AGB/good" "$BACKUP" >/dev/null 2>&1) || fail "(ag) the fixture backup failed"
+ARCHIVE_AG="$(ls "$AGB"/good/openwa-backup-*.tar.gz)"
+for line in 'DATABASE_NAME="./data/active.sqlite" # data store' \
+ 'DATABASE_TYPE="postgres" # migrated' \
+ 'STORAGE_LOCAL_PATH="./data/media" # local disk'; do
+ key="${line%%[=:]*}"
+ printf '%s\n' "$line" >"$AGB/.env"
+ set +e
+ OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+ RC_AG=$?
+ set -e
+ if [ "$RC_AG" -eq 0 ]; then
+ fail "(ag) backup exited 0 with an unparsed $key line: $OUT_AG"
+ fi
+ if ! grep -q "sets $key in a form" <<<"$OUT_AG"; then
+ fail "(ag) backup did not name $key: $OUT_AG"
+ fi
+ if [ -n "$(ls "$AGB/out" 2>/dev/null)" ]; then
+ fail "(ag) backup left an archive behind for an unparsed $key line"
+ fi
+ if [ "$key" = DATABASE_TYPE ]; then
+ continue # restore does not read it
+ fi
+ printf '%s\n' "$line" >"$AGB/dst/.env"
+ set +e
+ OUT_AG="$(cd "$AGB/dst" && "$RESTORE" "$ARCHIVE_AG" --force 2>&1)"
+ RC_AG=$?
+ set -e
+ if [ "$RC_AG" -eq 0 ] || ! grep -q "sets $key in a form" <<<"$OUT_AG"; then
+ fail "(ag) restore did not stop on an unparsed $key line (rc $RC_AG): $OUT_AG"
+ fi
+ if [ -n "$(ls "$AGB/dst/data")" ]; then
+ fail "(ag) restore wrote into the target before stopping on an unparsed $key line"
+ fi
+done
+# dotenv also reads a line behind a byte-order mark or a Unicode blank, trims a no-break space off a
+# value and takes a bare CR for a line break. grep's [[:space:]] does not, so each of these used to
+# resolve to the stale default and archive it with exit 0. dotenv reads a byte that is not UTF-8 as
+# U+FFFD, which grep in a UTF-8 locale skipped the line for, and bash drops a NUL from the value.
+for line in '\357\273\277DATABASE_NAME=./data/active.sqlite' '\302\240DATABASE_NAME=./data/active.sqlite' \
+ 'DATABASE_NAME=./data/active.sqlite\302\240' 'LOG_LEVEL=info\rDATABASE_NAME=./data/active.sqlite' \
+ 'DATABASE_NAME=./data/active.sqlite # donn\351es' 'DATABASE_NAME=./data/act\000ive.sqlite' \
+ 'DATABASE_NAME\302\240=./data/active.sqlite' 'DATABASE_NAME=\342\200\257./data/active.sqlite' \
+ 'DATABASE_NAME=./data/active.sqlite\343\200\200 # data store' '# note\342\200\250DATABASE_NAME=./data/active.sqlite'; do
+ # shellcheck disable=SC2059 # the line's escapes are the point
+ printf "$line\n" >"$AGB/.env"
+ set +e
+ OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+ RC_AG=$?
+ set -e
+ if [ "$RC_AG" -eq 0 ] || [ -n "$(ls "$AGB/out" 2>/dev/null)" ] ||
+ ! grep -q 'on a line naming DATABASE_NAME' <<<"$OUT_AG"; then
+ fail "(ag) backup did not stop on $line (rc $RC_AG): $OUT_AG"
+ fi
+done
+# A NUL elsewhere in the file made grep treat it as binary and print nothing, so the default was used.
+printf 'LOG_LEVEL=a\000b\nDATABASE_NAME=./data/active.sqlite\n' >"$AGB/.env"
+OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/nul" "$BACKUP" 2>&1)" || fail "(ag) backup failed with a NUL on another line of ./.env"
+# bash 4.4 and later warn about a NUL in a command substitution's output, which reads like a fault.
+if grep -q 'null byte' <<<"$OUT_AG"; then
+ fail "(ag) a NUL on the line before DATABASE_NAME leaked a shell warning: $OUT_AG"
+fi
+rm -rf "$AGB/x" && mkdir -p "$AGB/x"
+tar -xzf "$(ls "$AGB"/nul/openwa-backup-*.tar.gz)" -C "$AGB/x"
+if [ "$(db_fingerprint "$AGB/x/openwa.sqlite")" != "ag-active" ]; then
+ fail "(ag) a NUL on another line of ./.env archived '$(db_fingerprint "$AGB/x/openwa.sqlite")' instead of the configured data store"
+fi
+# None of these stop the run, as none changes what the app reads: a comment or another key's value
+# naming the key holds a no-break space or a Latin-1 byte, the key's own inline comment or the middle
+# of its value holds a Unicode blank, or a bare `export KEY` follows the key's line, which dotenv skips.
+# ENGINE_TYPE only gates a warning, so a line the scripts cannot parse skips the warning, not the backup.
+make_fixture "$AGB/data/act"$'\343\200\200'"ive.sqlite" "ag-active"
+for content in 'DATABASE_NAME=./data/active.sqlite\nexport DATABASE_NAME' \
+ '# DATABASE_NAME\302\240is the data store\nDATABASE_NAME=./data/active.sqlite' \
+ '# DATABASE_NAME: donn\351es\nDATABASE_NAME=./data/active.sqlite' \
+ 'NOTE=see DATABASE_NAME\302\240below\nDATABASE_NAME=./data/active.sqlite' \
+ 'DATABASE_NAME=./data/active.sqlite # primary\302\240store' 'DATABASE_NAME=./data/act\343\200\200ive.sqlite' \
+ 'DATABASE_NAME=./data/active.sqlite\nENGINE_TYPE: baileys'; do
+ # shellcheck disable=SC2059 # the line's escapes are the point
+ printf "$content\n" >"$AGB/.env"
+ rm -rf "$AGB/kept" "$AGB/x" && mkdir -p "$AGB/x"
+ set +e
+ OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/kept" "$BACKUP" 2>&1)"
+ RC_AG=$?
+ set -e
+ if [ "$RC_AG" -ne 0 ]; then
+ fail "(ag) backup stopped on $content (rc $RC_AG): $OUT_AG"
+ fi
+ tar -xzf "$(ls "$AGB"/kept/openwa-backup-*.tar.gz)" -C "$AGB/x"
+ if [ "$(db_fingerprint "$AGB/x/openwa.sqlite")" != "ag-active" ]; then
+ fail "(ag) $content archived '$(db_fingerprint "$AGB/x/openwa.sqlite")' instead of the configured data store"
+ fi
+done
+if ! grep -q 'sets ENGINE_TYPE in a form' <<<"$OUT_AG" || ! grep -q 'skipping only the check for missing Baileys state' <<<"$OUT_AG"; then
+ fail "(ag) backup did not report the ENGINE_TYPE line it cannot parse and that it carried on: $OUT_AG"
+fi
+rm -f "$AGB/data/act"$'\343\200\200'"ive.sqlite"
+# dotenv reads the line after a bare `NAME:` line as NAME's value, so the app never sets the key from it.
+printf 'LOG_LEVEL:\nDATABASE_NAME=./data/active.sqlite\n' >"$AGB/.env"
+set +e
+OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -eq 0 ] || [ -n "$(ls "$AGB/out" 2>/dev/null)" ] ||
+ ! grep -q 'just before a line setting DATABASE_NAME' <<<"$OUT_AG"; then
+ fail "(ag) backup did not stop on a DATABASE_NAME line right after a bare LOG_LEVEL: line (rc $RC_AG): $OUT_AG"
+fi
+# dotenv's `KEY\s*=` and the quoted value after an empty `KEY=` cross line breaks, and dotenv starts a
+# line after U+2028, so each of these sets the key for the app, or hides its line, from another line.
+# The scripts used to read the default or the hidden line, and archived it with exit 0.
+for check in 'DATABASE_NAME\n=./data/active.sqlite|names DATABASE_NAME on a line without an =' \
+ 'DATABASE_NAME=./data/openwa.sqlite\nexport DATABASE_NAME\n\n=./data/active.sqlite|names DATABASE_NAME on a line without an =' \
+ 'DATABASE_NAME=\n\n"./data/active.sqlite"|leaves DATABASE_NAME empty on its line' \
+ '# note\342\200\250LOG_LEVEL:\nDATABASE_NAME=./data/active.sqlite|just before a line setting DATABASE_NAME'; do
+ # shellcheck disable=SC2059 # the line's escapes are the point
+ printf "${check%%|*}\n" >"$AGB/.env"
+ set +e
+ OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+ RC_AG=$?
+ set -e
+ if [ "$RC_AG" -eq 0 ] || [ -n "$(ls "$AGB/out" 2>/dev/null)" ] || ! grep -q "${check#*|}" <<<"$OUT_AG"; then
+ fail "(ag) backup did not stop on ${check%%|*} (rc $RC_AG): $OUT_AG"
+ fi
+done
+# dotenv can take the backslash and quote that end a quoted value for an escaped quote, and read on to a
+# quote on a later line.
+printf '%s\n' "DATABASE_NAME='./data/active.sqlite\\'" "'" >"$AGB/.env"
+set +e
+OUT_AG="$(cd "$AGB" && DATA_DIR=./data && . "$REPO_ROOT/scripts/lib-env.sh" && openwa_resolve DATABASE_NAME DEFAULT 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -eq 0 ] || ! grep -q 'ending in a backslash' <<<"$OUT_AG"; then
+ fail "(ag) a quoted DATABASE_NAME ending in a backslash did not fail the lookup (rc $RC_AG): $OUT_AG"
+fi
+# An empty `KEY=` followed by blank lines and an unquoted line stays empty for dotenv too.
+printf 'DATABASE_NAME=\n\n# data store\nLOG_LEVEL=info\n' >"$AGB/.env"
+if [ "$(cd "$AGB" && DATA_DIR=./data && . "$REPO_ROOT/scripts/lib-env.sh" && openwa_resolve DATABASE_NAME DEFAULT)" != DEFAULT ]; then
+ fail "(ag) an empty DATABASE_NAME= followed by a comment and another key did not resolve to the default"
+fi
+# A quoted value holding its own quote character is named as such, and the remedy warns that double
+# quotes expand \n and \r. The other quote style, which the error suggests, archives the configured store.
+make_fixture "$AGB/data/o'brien.sqlite" "ag-active"
+printf '%s\n' "DATABASE_NAME='./data/o'brien.sqlite'" >"$AGB/.env"
+set +e
+OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -eq 0 ] || ! grep -q 'containing its own quote character' <<<"$OUT_AG" ||
+ ! grep -q 'turns \\n and \\r into line breaks' <<<"$OUT_AG"; then
+ fail "(ag) backup did not stop on a single-quoted value holding a single quote with that cause named (rc $RC_AG): $OUT_AG"
+fi
+printf '%s\n' "DATABASE_NAME=\"./data/o'brien.sqlite\"" >"$AGB/.env"
+(cd "$AGB" && BACKUP_DIR="$AGB/quote-fix" "$BACKUP" >/dev/null 2>&1) || fail "(ag) backup failed with the value in the other quote style"
+rm -rf "$AGB/x" "$AGB/data/o'brien.sqlite" && mkdir -p "$AGB/x"
+tar -xzf "$(ls "$AGB"/quote-fix/openwa-backup-*.tar.gz)" -C "$AGB/x"
+if [ "$(db_fingerprint "$AGB/x/openwa.sqlite")" != "ag-active" ]; then
+ fail "(ag) the other quote style archived '$(db_fingerprint "$AGB/x/openwa.sqlite")' instead of the configured data store"
+fi
+# The Postgres connection keys are resolved where a failure stops the run, before pg_dump starts.
+mkdir -p "$AGB/bin"
+printf '#!/bin/sh\necho dumped >"%s/pg_dump-ran"\n' "$AGB" >"$AGB/bin/pg_dump"
+chmod +x "$AGB/bin/pg_dump"
+printf 'DATABASE_TYPE=postgres\nDATABASE_HOST="db.internal" # primary\n' >"$AGB/.env"
+set +e
+OUT_AG="$(cd "$AGB" && PATH="$AGB/bin:$PATH" BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -eq 0 ] || [ -e "$AGB/pg_dump-ran" ] || ! grep -q 'sets DATABASE_HOST in a form' <<<"$OUT_AG"; then
+ fail "(ag) backup dumped the default Postgres host past an unparsed DATABASE_HOST line (rc $RC_AG): $OUT_AG"
+fi
+# DATABASE_NAME is the SQLite data store's path or the Postgres database's name, so a line for it the
+# scripts cannot parse stops neither a Postgres backup that dumps DATABASE_URL nor the restore of a
+# PostgreSQL archive, neither of which reads it.
+printf 'DATABASE_TYPE=postgres\nDATABASE_NAME="openwa" # primary\n' >"$AGB/.env"
+set +e
+OUT_AG="$(cd "$AGB" && PATH="$AGB/bin:$PATH" DATABASE_URL=postgres://db.internal/openwa BACKUP_DIR="$AGB/pg" "$BACKUP" 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -ne 0 ] || [ ! -e "$AGB/pg_dump-ran" ] || ! tar -tzf "$(ls "$AGB"/pg/openwa-backup-*.tar.gz)" | grep -qx './database.sql'; then
+ fail "(ag) a Postgres backup through DATABASE_URL stopped on a DATABASE_NAME line it does not use (rc $RC_AG): $OUT_AG"
+fi
+rm -rf "$AGB/pgdst" && mkdir -p "$AGB/pgdst/data"
+printf 'DATABASE_NAME="openwa" # primary\n' >"$AGB/pgdst/.env"
+set +e
+OUT_AG="$(cd "$AGB/pgdst" && "$RESTORE" "$(ls "$AGB"/pg/openwa-backup-*.tar.gz)" --force 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -ne 0 ] || [ ! -f "$AGB/pgdst/data/database.sql" ] || [ "$(db_fingerprint "$AGB/pgdst/data/main.sqlite")" != "ag-main" ]; then
+ fail "(ag) restoring a PostgreSQL archive stopped on a DATABASE_NAME line it does not use (rc $RC_AG): $OUT_AG"
+fi
+# The remedies the error gives work: the key in the environment wins over the unparsed line, and the
+# same quoted value with its comment on its own line parses. Either way the configured store is archived.
+AG_LINE='DATABASE_NAME="./data/active.sqlite" # data store'
+printf '%s\n' "$AG_LINE" >"$AGB/.env"
+(cd "$AGB" && DATABASE_NAME=./data/active.sqlite BACKUP_DIR="$AGB/env-fix" "$BACKUP" >/dev/null 2>&1) ||
+ fail "(ag) backup failed with DATABASE_NAME passed in the environment over an unparsed line"
+printf '# data store\nDATABASE_NAME="./data/active.sqlite"\n' >"$AGB/.env"
+(cd "$AGB" && BACKUP_DIR="$AGB/line-fix" "$BACKUP" >/dev/null 2>&1) || fail "(ag) backup failed with the comment on its own line"
+for dir in env-fix line-fix; do
+ rm -rf "$AGB/x" && mkdir -p "$AGB/x"
+ tar -xzf "$(ls "$AGB/$dir"/openwa-backup-*.tar.gz)" -C "$AGB/x"
+ if [ "$(db_fingerprint "$AGB/x/openwa.sqlite")" != "ag-active" ]; then
+ fail "(ag) the $dir backup archived '$(db_fingerprint "$AGB/x/openwa.sqlite")' instead of the configured data store"
+ fi
+done
+# A line that only data/.env.generated holds stops the run as well, and so does one in the archive's
+# copy, which a restore reads in place of the target's.
+rm -f "$AGB/.env"
+printf '%s\n' "$AG_LINE" >"$AGB/data/.env.generated"
+set +e
+OUT_AG="$(cd "$AGB" && BACKUP_DIR="$AGB/out" "$BACKUP" 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -eq 0 ] || [ -n "$(ls "$AGB/out" 2>/dev/null)" ] ||
+ ! grep -q '\.env\.generated sets DATABASE_NAME in a form' <<<"$OUT_AG" ||
+ ! grep -q 'Move a comment to its own' <<<"$OUT_AG"; then
+ fail "(ag) backup did not stop on an unparsed DATABASE_NAME line in data/.env.generated (rc $RC_AG): $OUT_AG"
+fi
+(cd "$AGB" && DATABASE_NAME=./data/active.sqlite BACKUP_DIR="$AGB/gen" "$BACKUP" >/dev/null 2>&1) ||
+ fail "(ag) backup failed with DATABASE_NAME passed in the environment over data/.env.generated"
+rm -f "$AGB/data/.env.generated" "$AGB/dst/.env"
+set +e
+OUT_AG="$(cd "$AGB/dst" && "$RESTORE" "$(ls "$AGB"/gen/openwa-backup-*.tar.gz)" --force 2>&1)"
+RC_AG=$?
+set -e
+if [ "$RC_AG" -eq 0 ] || [ -n "$(ls "$AGB/dst/data")" ] ||
+ ! grep -q '\.env\.generated sets DATABASE_NAME in a form' <<<"$OUT_AG"; then
+ fail "(ag) restore did not stop on an unparsed DATABASE_NAME line in the archive's .env.generated (rc $RC_AG): $OUT_AG"
+fi
+if ! grep -q "come from the archive's .env.generated" <<<"$OUT_AG"; then
+ fail "(ag) restore did not name the archive's .env.generated as the source of its paths: $OUT_AG"
+fi
+(cd "$AGB/dst" && DATABASE_NAME=./data/active.sqlite "$RESTORE" "$(ls "$AGB"/gen/openwa-backup-*.tar.gz)" --force >/dev/null 2>&1) ||
+ fail "(ag) restore failed with DATABASE_NAME passed in the environment over the archive's .env.generated"
+if [ "$(db_fingerprint "$AGB/dst/data/active.sqlite")" != "ag-active" ] || [ -e "$AGB/dst/data/openwa.sqlite" ]; then
+ fail "(ag) restore with DATABASE_NAME in the environment did not land on the configured data store only"
fi
-pass "(ag) dotenv's quoted, commented and empty forms resolve like the app, and an unparsed line stops the lookup"
+pass "(ag) dotenv's quoted, commented and empty forms resolve like the app, and an unparsed line stops both scripts"
echo ""
echo "==> (ah) pg_dump makes the app's TLS check when DATABASE_SSL=true"