Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): keep an online backup when engine files vanish mid-copy

Yudhi Armyndharis401c90cc474e
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 58d6cc613..64aa3f6d4 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -145,6 +145,26 @@ EOF
log "WARN: engine auth state may have been written during the copy; a restore may need re-pairing: $1"
}
+# copy_live_tree <src> <dest> [label] - cp -pRH, except that a file the app deleted or renamed while
+# cp walked the tree (Chromium cache and LevelDB churn, the Baileys store's temp files and consumed
+# keys, media retention and plugin storage writes) is a torn copy, not a failed backup. With a label
+# the copy is engine auth state and goes in ENGINE-STATE-NOTE; without one it is only logged. Every
+# other cp error stays fatal.
+copy_live_tree() {
+ local err
+ if ! err="$(cp -pRH "$1" "$2" 2>&1)"; then
+ if printf '%s\n' "$err" | grep -qv 'No such file or directory'; then
+ printf '%s\n' "$err" >&2
+ exit 1
+ fi
+ if [ -n "${3:-}" ]; then
+ record_engine_state_note "$3 (files changed during the copy)"
+ else
+ log "WARN: files under $1 changed during the copy"
+ fi
+ fi
+}
+
# Online SQLite backup (consistent without stopping the app) when sqlite3 is present. A missing
# source database is FATAL: an archive without the configured databases is not a backup, and a
# silent skip is how an empty archive gets reported as "Backup complete".
@@ -202,12 +222,13 @@ fi
# linked back is archived by its content. Without it the archive held only the link, with no data.
if [ -d "$SESSIONS_DIR" ]; then
log "Backing up whatsapp-web.js sessions"
- cp -pRH "$SESSIONS_DIR" "$STAGE/sessions"
+ copy_live_tree "$SESSIONS_DIR" "$STAGE/sessions" sessions/
# Chromium holds a SingletonLock (a symlink, so not `-e`) in every profile it has open. A browser
# killed outright (force-kill, a stop that timed out) leaves its lock behind until the next launch
# or container start clears it, so a lock here means the profile is open or was last hard-killed.
- OPEN_PROFILES="$(find -H "$SESSIONS_DIR" -mindepth 2 -maxdepth 2 -name SingletonLock -exec dirname {} \; |
- sed 's|.*/||' | sort | tr '\n' ' ')"
+ OPEN_PROFILES="$({
+ find -H "$SESSIONS_DIR" -mindepth 2 -maxdepth 2 -name SingletonLock -exec dirname {} \; 2>/dev/null || true
+ } | sed 's|.*/||' | sort | tr '\n' ' ')"
if [ -n "$OPEN_PROFILES" ]; then
record_engine_state_note "sessions/ (whatsapp-web.js profiles holding a Chromium SingletonLock, open or left by a killed browser: ${OPEN_PROFILES% })"
fi
@@ -217,7 +238,7 @@ fi
if [ -d "$BAILEYS_DIR" ]; then
log "Backing up Baileys authentication state"
- cp -pRH "$BAILEYS_DIR" "$STAGE/baileys"
+ copy_live_tree "$BAILEYS_DIR" "$STAGE/baileys" baileys/
# Baileys rewrites creds.json and its key files during normal traffic and leaves no sign of being
# live, so any session's state counts, stopped or not.
if [ -n "$(find -H "$BAILEYS_DIR" -mindepth 1 -maxdepth 1 -type d -print -quit)" ]; then
@@ -229,14 +250,14 @@ fi
if [ -d "$MEDIA_DIR" ]; then
log "Backing up local media"
- cp -pRH "$MEDIA_DIR" "$STAGE/media"
+ copy_live_tree "$MEDIA_DIR" "$STAGE/media"
else
log "WARN: $MEDIA_DIR not found; skipping local media"
fi
if [ -d "$PLUGIN_PACKAGES_DIR" ]; then
log "Backing up installed plugin packages"
- cp -pRH "$PLUGIN_PACKAGES_DIR" "$STAGE/plugin-packages"
+ copy_live_tree "$PLUGIN_PACKAGES_DIR" "$STAGE/plugin-packages"
fi
# With PLUGINS_DIR unset the app also loads packages from ./plugins, its default up to 0.12.1 (see
@@ -249,7 +270,7 @@ fi
if [ -d "$PLUGIN_STATE_DIR" ]; then
log "Backing up plugin registry and persisted state"
- cp -pRH "$PLUGIN_STATE_DIR" "$STAGE/plugin-state"
+ copy_live_tree "$PLUGIN_STATE_DIR" "$STAGE/plugin-state"
fi
if [ -f "$GENERATED_ENV" ]; then
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index 79711c2a0..119f12e94 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -43,6 +43,9 @@
# is noted in the archive and printed by restore, which does not refuse it even with --strict
# (ab) a blank ./.env line keeps data/.env.generated from supplying the key, so the default applies
# (ac) a missing Baileys auth dir is reported when data/.env.generated selects the Baileys engine
+# (ad) a file an engine deletes during the sessions/ or baileys/ copy is noted instead of failing the
+# backup, a file the app deletes during the media or plugin copies is logged, and any other cp
+# error still fails it
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -1264,5 +1267,53 @@ if ! printf '%s' "$OUT_AC" | grep -q 'ENGINE_TYPE=baileys but .* was not found';
fi
pass "(ac) the missing Baileys auth dir is reported when data/.env.generated selects Baileys"
+echo ""
+echo "==> (ad) a file the engine deletes during the state copy notes the copy instead of failing the backup"
+# Chromium and the Baileys auth store delete and rename files while they run, so cp can list a file
+# that is gone when it opens it. Under set -e that aborted the online backup with no archive. The shim
+# cp copies for real, then reports the error a vanished file gives; any other cp error stays fatal.
+AD="$WORK/ad"
+mkdir -p "$AD/data/sessions/session-s1" "$AD/data/baileys/s1" "$AD/data/media" "$AD/data/plugins" "$AD/shim"
+make_fixture "$AD/data/main.sqlite" "delta2-main"
+make_fixture "$AD/data/openwa.sqlite" "delta2-data"
+printf 'profile\n' >"$AD/data/sessions/session-s1/Preferences"
+printf '{}' >"$AD/data/baileys/s1/creds.json"
+printf 'jpeg\n' >"$AD/data/media/status.jpg"
+printf '{}' >"$AD/data/plugins/registry.json"
+cat >"$AD/shim/cp" <<SHIM
+#!/bin/sh
+$(command -v cp) "\$@" || exit
+case "\$3" in
+ */sessions | */baileys | */media | */plugin-*) echo "cp: cannot stat '\$2/gone': \$SHIM_CP_ERROR" >&2; exit 1 ;;
+esac
+SHIM
+chmod +x "$AD/shim/cp"
+set +e
+OUT_AD="$(cd "$AD" && SHIM_CP_ERROR='No such file or directory' PATH="$AD/shim:$PATH" BACKUP_DIR="$AD/out" "$BACKUP" 2>&1)"
+RC_AD=$?
+set -e
+if [ "$RC_AD" -ne 0 ]; then
+ fail "(ad) a file that vanished during the engine state copy failed the backup: $OUT_AD"
+fi
+NOTE_AD="$(tar -xOzf "$(ls "$AD"/out/openwa-backup-*.tar.gz)" ./ENGINE-STATE-NOTE 2>/dev/null || true)"
+if ! printf '%s\n' "$NOTE_AD" | grep -q '^sessions/ (files changed during the copy)' ||
+ ! printf '%s\n' "$NOTE_AD" | grep -q '^baileys/ (files changed during the copy)'; then
+ fail "(ad) the archive does not note the state copies that changed underneath: $NOTE_AD"
+fi
+for tree in media plugins; do
+ if ! printf '%s\n' "$OUT_AD" | grep -q "WARN: files under .*/$tree changed during the copy"; then
+ fail "(ad) the backup did not log the $tree copy that changed underneath: $OUT_AD"
+ fi
+done
+set +e
+OUT_AD="$(cd "$AD" && SHIM_CP_ERROR='Permission denied' PATH="$AD/shim:$PATH" BACKUP_DIR="$AD/out2" "$BACKUP" 2>&1)"
+RC_AD=$?
+set -e
+if [ "$RC_AD" -eq 0 ] || ! printf '%s' "$OUT_AD" | grep -q 'Permission denied' ||
+ [ -n "$(ls "$AD"/out2/openwa-backup-*.tar.gz 2>/dev/null)" ]; then
+ fail "(ad) a cp error other than a vanished file did not fail the backup: $OUT_AD"
+fi
+pass "(ad) a vanished engine file is noted, a vanished media or plugin file logged, other cp errors fatal"
+
echo ""
echo "All smoke tests passed!"