Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(backup): keep a symlinked state directory across backup and restore
Yudhi Armyndharisf0ff3f912978
diff --git a/scripts/backup.sh b/scripts/backup.shindex 5c836b047..b63ca194f 100755--- a/scripts/backup.sh+++ b/scripts/backup.sh@@ -160,33 +160,35 @@ elseREQUIRED_MEMBERS+=("./openwa.sqlite")fi+# The state directories below are copied with -H: a directory an operator moved to another disk and+# linked back is archived by its content. Without it the archive held only the link, with no data.if [ -d "$SESSIONS_DIR" ]; thenlog "Backing up whatsapp-web.js sessions"- cp -pR "$SESSIONS_DIR" "$STAGE/sessions"+ cp -pRH "$SESSIONS_DIR" "$STAGE/sessions"elselog "WARN: $SESSIONS_DIR not found — skipping sessions"fiif [ -d "$BAILEYS_DIR" ]; thenlog "Backing up Baileys authentication state"- cp -pR "$BAILEYS_DIR" "$STAGE/baileys"+ cp -pRH "$BAILEYS_DIR" "$STAGE/baileys"elif [ "${ENGINE_TYPE:-}" = "baileys" ]; thenlog "WARN: ENGINE_TYPE=baileys but $BAILEYS_DIR was not found — restored sessions will require pairing"fiif [ -d "$MEDIA_DIR" ]; thenlog "Backing up local media"- cp -pR "$MEDIA_DIR" "$STAGE/media"+ cp -pRH "$MEDIA_DIR" "$STAGE/media"fiif [ -d "$PLUGIN_PACKAGES_DIR" ]; thenlog "Backing up installed plugin packages"- cp -pR "$PLUGIN_PACKAGES_DIR" "$STAGE/plugin-packages"+ cp -pRH "$PLUGIN_PACKAGES_DIR" "$STAGE/plugin-packages"fiif [ -d "$PLUGIN_STATE_DIR" ]; thenlog "Backing up plugin registry and persisted state"- cp -pR "$PLUGIN_STATE_DIR" "$STAGE/plugin-state"+ cp -pRH "$PLUGIN_STATE_DIR" "$STAGE/plugin-state"fiif [ -f "$GENERATED_ENV" ]; thendiff --git a/scripts/restore.sh b/scripts/restore.shindex 87183cd2a..9d2fcc222 100755--- a/scripts/restore.sh+++ b/scripts/restore.sh@@ -168,11 +168,13 @@ replace_tree() {returnfilog "Restoring $label"- if [ -d "$target_dir" ] && [ ! -L "$target_dir" ]; then+ if [ -d "$target_dir" ]; then# Empty the directory and refill it rather than remove it: it may be a mount point (a volume under- # the container's read-only root), which can be neither removed nor re-created.- find "$target_dir" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +- cp -pR "$source_dir/." "$target_dir"+ # the container's read-only root), which can be neither removed nor re-created. A symlink to a+ # directory is refilled through the link, so an operator's layout on another disk survives. The+ # trailing slash makes find descend into the link's target instead of stopping at the link.+ find "$target_dir/" -mindepth 1 -maxdepth 1 -exec rm -rf -- {} ++ cp -pR "$source_dir/." "$target_dir/"elserm -rf -- "$target_dir"mkdir -p "$(dirname "$target_dir")"@@ -285,6 +287,17 @@ while IFS= read -r entry; dodone < <(tar -tzf "$ARCHIVE")tar -xzf "$ARCHIVE" -C "$STAGE"+# 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.+for member in sessions baileys media plugin-packages plugin-state; do+ if [ -L "$STAGE/$member" ]; then+ log "ERROR: archive member $member/ is a symlink, not a directory: the backup holds no data for it"+ log " re-take the backup with this version of backup.sh, or extract the archive and restore by hand"+ exit 1+ fi+done+# A backup taken without sqlite3 .backup carries this marker: the database snapshots were# plain-copied from a possibly-live app and may be torn. Warn loudly and continue — unless# --strict makes it fatal. Refuse BEFORE touching any existing state.diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 34db3746a..81c8bd64a 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -24,6 +24,8 @@# (p) a symlinked database target or data dir is snapshotted as a copy of what the link points at# (q) a leftover -wal is cleared before a database is restored and kept in its snapshot (skipped# 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## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -713,5 +715,68 @@ elseecho "SKIP: (q) sqlite3 not found on this host, so there is no WAL-mode database to build"fi+echo ""+echo "==> (r) a symlinked state dir is archived by content and refilled in place"+# An operator who keeps media on another disk links ./data/media there. cp -R archived the link and+# not the files, and the restore replaced the link with a real directory on the data disk, leaving+# the linked disk with the old files.+R="$WORK/r"+mkdir -p "$R/src/data" "$R/src-disk/media" "$R/live" "$R/disk/media"+make_fixture "$R/src/data/main.sqlite" "romeo-archive-main"+make_fixture "$R/src/data/openwa.sqlite" "romeo-archive-data"+printf 'romeo-archive\n' >"$R/src-disk/media/a.jpg"+ln -s "$R/src-disk/media" "$R/src/data/media"+(+ cd "$R/src"+ BACKUP_DIR="$R/out" "$BACKUP" >/dev/null+)+ARCHIVE_R="$(ls "$R"/out/openwa-backup-*.tar.gz)"+if ! tar -tzf "$ARCHIVE_R" | grep -qx './media/a.jpg'; then+ fail "(r) backup archived the symlinked media dir as a link instead of its files"+fi+printf 'romeo-live\n' >"$R/disk/media/m0"+ln -s "$R/disk/media" "$R/live/media"+# restore_r <archive>: a forced restore over $R/live. Output lands in OUT, the exit code in RC.+restore_r() {+ set +e+ OUT="$(cd "$R" && MAIN_DATABASE_NAME="$R/live/main.sqlite" DATABASE_NAME="$R/live/openwa.sqlite" \+ OPENWA_DATA_DIR="$R/live" "$RESTORE" "$1" --force 2>&1)"+ RC=$?+ set -e+}+restore_r "$ARCHIVE_R"+if [ "$RC" -ne 0 ]; then+ fail "(r) restore over a symlinked media dir failed: $OUT"+fi+if [ ! -L "$R/live/media" ]; then+ fail "(r) the restore replaced the symlinked media dir with a real directory"+fi+if [ "$(cat "$R/disk/media/a.jpg" 2>/dev/null || true)" != "romeo-archive" ] || [ -e "$R/disk/media/m0" ]; then+ fail "(r) the directory the link points at was not refilled with the archived media"+fi+if [ "$(cat "$R"/live/media.pre-restore-*/m0 2>/dev/null || true)" != "romeo-live" ]; then+ fail "(r) the snapshot does not hold the media the restore replaced"+fi+# An archive written before backup.sh followed such links carries the link itself, which on this host+# may point at the very directory being emptied. It is refused before anything is touched.+mkdir -p "$R/linked"+tar -xzf "$ARCHIVE_R" -C "$R/linked"+rm -rf "${R:?}/linked/media"+ln -s "$R/disk/media" "$R/linked/media"+tar -czf "$R/linked.tar.gz" -C "$R/linked" .+rm -f "$R/live/main.sqlite"+make_fixture "$R/live/main.sqlite" "romeo-live-main"+restore_r "$R/linked.tar.gz"+if [ "$RC" -eq 0 ]; then+ fail "(r) restore accepted an archive whose media member is a symlink"+fi+if ! printf '%s' "$OUT" | grep -q 'symlink'; then+ fail "(r) the refusal does not say the archive member is a symlink"+fi+if [ "$(db_fingerprint "$R/live/main.sqlite")" != "romeo-live-main" ] || [ ! -f "$R/disk/media/a.jpg" ]; then+ fail "(r) the refused restore changed the install"+fi+pass "(r) a symlinked media dir is archived by content, refilled through the link, and a linked member is refused"+echo ""echo "All smoke tests passed!"