Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(backup): follow the app's media fallback from a leftover ./uploads

Yudhi Armyndharisbc50bce59bd8
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 01731b51f..f43a44662 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -67,7 +67,7 @@ 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")"
+MEDIA_DIR="$(openwa_media_dir)"
# 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.
@@ -187,6 +187,8 @@ fi
if [ -d "$MEDIA_DIR" ]; then
log "Backing up local media"
cp -pRH "$MEDIA_DIR" "$STAGE/media"
+else
+ log "WARN: $MEDIA_DIR not found; skipping local media"
fi
if [ -d "$PLUGIN_PACKAGES_DIR" ]; then
diff --git a/scripts/lib-env.sh b/scripts/lib-env.sh
index 0bbae386b..e1e2b8cb2 100644
--- a/scripts/lib-env.sh
+++ b/scripts/lib-env.sh
@@ -63,6 +63,24 @@ openwa_writable() {
[ -d "$p" ] && [ -w "$p" ]
}
+# openwa_media_dir - STORAGE_LOCAL_PATH as the app settles it (src/config/storage-root.ts). v0.2.0 to
+# v0.7.3 persisted ./uploads into .env.generated; where that cannot be created, as under the image's
+# root-owned /app, the app keeps media in ./data/media instead, so the scripts have to look there too.
+openwa_media_dir() {
+ local dir
+ dir="$(openwa_resolve STORAGE_LOCAL_PATH "$DATA_DIR/media")"
+ case "$dir" in
+ ./uploads | uploads)
+ if ! openwa_writable "$dir"; then
+ echo "[config] WARN: STORAGE_LOCAL_PATH=$dir cannot be created here, so the app keeps media in" >&2
+ echo "[config] ./data/media; using that. Remove the leftover line from .env.generated." >&2
+ dir=./data/media
+ fi
+ ;;
+ esac
+ printf '%s' "$dir"
+}
+
# 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"
diff --git a/scripts/restore.sh b/scripts/restore.sh
index 680ff03b8..b6c74ce31 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -300,7 +300,7 @@ 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")"
+MEDIA_DIR="$(openwa_media_dir)"
# 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.
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index 814c5ba05..3b92c9a98 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -31,6 +31,8 @@
# and a `KEY: value` line is reported
# (u) a state or database 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
+# both scripts, and a missing media dir is reported (skipped as root)
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -951,5 +953,60 @@ else
echo "SKIP: (u) running as root, which ignores the permission bits this case relies on"
fi
+echo ""
+echo "==> (v) a leftover STORAGE_LOCAL_PATH=./uploads follows the app's fallback to ./data/media"
+# v0.2.0 to v0.7.3 persisted ./uploads into data/.env.generated. In the image /app is not writable, so
+# the app cannot create it and keeps media in ./data/media instead; the scripts looked in ./uploads,
+# found nothing and left every media file out without a word. The read-only working directory stands
+# in for /app here.
+if [ "$(id -u)" -ne 0 ]; then
+ V="$WORK/v"
+ mkdir -p "$V/app/data/media" "$V/dst/data" "$V/bare/data"
+ make_fixture "$V/app/data/main.sqlite" "victor-main"
+ make_fixture "$V/app/data/openwa.sqlite" "victor-data"
+ printf 'victor-media\n' >"$V/app/data/media/a.jpg"
+ printf 'STORAGE_LOCAL_PATH=./uploads\n' >"$V/app/data/.env.generated"
+ chmod a-w "$V/app"
+ set +e
+ OUT_V="$(cd "$V/app" && BACKUP_DIR="$V/out" "$BACKUP" 2>&1)"
+ RC_V=$?
+ set -e
+ chmod u+w "$V/app"
+ if [ "$RC_V" -ne 0 ]; then
+ fail "(v) backup failed: $OUT_V"
+ fi
+ ARCHIVE_V="$(ls "$V"/out/openwa-backup-*.tar.gz)"
+ if ! tar -tzf "$ARCHIVE_V" | grep -qx './media/a.jpg'; then
+ fail "(v) backup left out the media the app keeps in ./data/media"
+ fi
+ if ! printf '%s' "$OUT_V" | grep -q 'STORAGE_LOCAL_PATH=./uploads'; then
+ fail "(v) the fallback from the leftover ./uploads was not reported"
+ fi
+ chmod a-w "$V/dst"
+ set +e
+ OUT_V="$(cd "$V/dst" && "$RESTORE" "$ARCHIVE_V" 2>&1)"
+ RC_V=$?
+ set -e
+ chmod u+w "$V/dst"
+ if [ "$RC_V" -ne 0 ]; then
+ fail "(v) restore failed: $OUT_V"
+ fi
+ if [ "$(cat "$V/dst/data/media/a.jpg" 2>/dev/null || true)" != "victor-media" ]; then
+ fail "(v) restore did not put the media back where the app reads it"
+ fi
+ # On a host where ./uploads can be created the app uses it, so the scripts keep it too, and a
+ # media dir that is not there is reported rather than skipped in silence.
+ make_fixture "$V/bare/data/main.sqlite" "victor-bare-main"
+ make_fixture "$V/bare/data/openwa.sqlite" "victor-bare-data"
+ printf 'STORAGE_LOCAL_PATH=./uploads\n' >"$V/bare/data/.env.generated"
+ OUT_V="$(cd "$V/bare" && BACKUP_DIR="$V/out-bare" "$BACKUP" 2>&1)"
+ if ! printf '%s' "$OUT_V" | grep -q 'WARN: ./uploads not found'; then
+ fail "(v) a missing media dir was skipped without a warning"
+ fi
+ pass "(v) a leftover ./uploads falls back like the app, and missing media is reported"
+else
+ echo "SKIP: (v) running as root, which ignores the permission bits this case relies on"
+fi
+
echo ""
echo "All smoke tests passed!"