Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(backup): keep a truncated archive off its final name
Yudhi Armyndharisa967a575eeb8
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 9a74765bf..39512f5ba 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -100,6 +100,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- Resolve `./data/...` paths from `.env.generated` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs.- Stop `backup.sh` and `restore.sh` on an env line they cannot parse instead of falling back to a default path or TLS setting.- Verify the PostgreSQL server certificate in `backup.sh` when `DATABASE_SSL=true`, as the app does.+- Leave no truncated archive in `BACKUP_DIR` when the final tar step of `backup.sh` fails, for example on a full volume.- 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.- Deliver Baileys API edit and revoke events to webhook and WebSocket consumers.diff --git a/scripts/backup.sh b/scripts/backup.shindex 7555effbe..7b2df393f 100755--- a/scripts/backup.sh+++ b/scripts/backup.sh@@ -53,9 +53,11 @@# included, against the certificate, so name the host the certificate carries.## Failure policy: a missing source database is FATAL (no silent empty backup), and the finished-# archive must contain every configured database or it is deleted and the run fails. When the-# sqlite3 CLI is unavailable the databases are plain-copied (possibly torn if the app is live) and-# the archive carries a CONSISTENCY-WARNING marker that restore.sh surfaces.+# archive must contain every configured database or it is deleted and the run fails. The archive+# takes its final name only once complete, so a failed tar leaves no truncated+# openwa-backup-*.tar.gz. When the sqlite3 CLI is unavailable the databases are plain-copied (possibly+# torn if the app is live) and the archive carries a CONSISTENCY-WARNING marker that restore.sh+# surfaces.## Engine authentication state (sessions/, baileys/) is copied, not snapshotted: the engines rewrite# it while they run, so an online backup can hold a copy torn mid-write. When a whatsapp-web.js@@ -121,7 +123,12 @@ fi# memory limit, so an in-container run there points TMPDIR at the data volume (docs/11); staging the# data in the tmpfs gets the running gateway OOM-killed once the data outgrows its headroom.STAGE="$(mktemp -d)"-trap 'rm -rf "$STAGE"' EXIT+# The archive is written under a name no restore, listing or retention glob matches and renamed once the+# min-content check passes: a tar that fails part way (the volume fills) must not leave a truncated+# archive that reads as the newest backup.+ARCHIVE="$BACKUP_DIR/openwa-backup-$TIMESTAMP.tar.gz"+ARCHIVE_PARTIAL="$ARCHIVE.partial"+trap 'rm -rf "$STAGE" "$ARCHIVE_PARTIAL"' EXITCONSISTENCY_WARNING="$STAGE/CONSISTENCY-WARNING"@@ -238,7 +245,7 @@ if [ "$DATABASE_TYPE" = "postgres" ]; thenelseif [ -z "${PGSSLROOTCERT:-}" ] && command -v node >/dev/null 2>&1; thenPG_ROOT_CERTS="$(mktemp)"- trap 'rm -rf "$STAGE" "$PG_ROOT_CERTS"' EXIT+ trap 'rm -rf "$STAGE" "$ARCHIVE_PARTIAL" "$PG_ROOT_CERTS"' EXIT# Before Node 22.15 there is no getCACertificates, and rootCertificates holds only the bundled# roots, so NODE_EXTRA_CA_CERTS is added from its file. An unreadable file is skipped, as Node# itself skips it after the warning it prints at startup.@@ -349,10 +356,9 @@ if [ -f "$ADMIN_KEY_FILE" ]; thencp -p "$ADMIN_KEY_FILE" "$STAGE/.api-key"fi-ARCHIVE="$BACKUP_DIR/openwa-backup-$TIMESTAMP.tar.gz"-tar -czf "$ARCHIVE" -C "$STAGE" .+tar -czf "$ARCHIVE_PARTIAL" -C "$STAGE" .-ARCHIVE_LIST="$(tar -tzf "$ARCHIVE")"+ARCHIVE_LIST="$(tar -tzf "$ARCHIVE_PARTIAL")"# Min-content check on the finished archive: every configured database must be present. If not,# delete the defective archive and fail — leaving it on disk invites a restore into a fresh-empty@@ -367,9 +373,10 @@ for member in "${REQUIRED_MEMBERS[@]}"; dodoneif [ -n "$MISSING_MEMBERS" ]; thenlog "ERROR: archive failed the min-content check — missing required member(s):$MISSING_MEMBERS"- rm -f "$ARCHIVE"+ rm -f "$ARCHIVE_PARTIAL"exit 1fi+mv "$ARCHIVE_PARTIAL" "$ARCHIVE"log "Backup complete: $ARCHIVE"log "SECURITY: this archive can contain database passwords, plugin secrets, and an admin API key; restrict and encrypt it"diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 895e65388..a7fccc485 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -66,6 +66,8 @@# sets nothing and send both to the default paths (skipped as root)# (ak) the archive and a BACKUP_DIR backup.sh creates stay private under a permissive umask, and the# staging copy is removed after a backup that succeeds and one that fails+# (al) a tar that fails part way exits non-zero and leaves no archive in BACKUP_DIR, so a truncated+# file cannot pass for the newest backup## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -118,7 +120,7 @@ db_fingerprint() {# to shadow tar (simulating an incomplete archive) without touching the real scripts.populate_shim() {shim_dir="$1"- tools="env bash sh cp tar gzip mktemp date rm sed mkdir ls cat chmod grep printf uname dirname tr tail"+ tools="env bash sh cp mv tar gzip mktemp date rm sed mkdir ls cat chmod grep printf uname dirname tr tail"if [ "${2:-}" = "with-sqlite3" ]; thentools="$tools sqlite3"fi@@ -2239,5 +2241,40 @@ case "$AK_PROBE" in*) pass "(ak) the archive is 600 and a created BACKUP_DIR is 700 (staging cleanup not checked: this mktemp ignores TMPDIR)" ;;esac+echo ""+echo "==> (al) a tar that fails part way leaves no archive behind"+# A full volume stops tar after part of the archive is written. That file must not stay under the+# openwa-backup-*.tar.gz name, where a listing or a keep-newest rotation reads it as the latest backup.+AL="$WORK/al"+mkdir -p "$AL/src/data" "$AL/shim"+make_fixture "$AL/src/data/main.sqlite" "al-main"+make_fixture "$AL/src/data/openwa.sqlite" "al-data"+if [ "$HAS_SQLITE3" -eq 1 ]; then+ populate_shim "$AL/shim" with-sqlite3+else+ populate_shim "$AL/shim"+fi+# (remove the populate_shim symlink first: writing through it would target the real tar)+rm -f "$AL/shim/tar"+cat >"$AL/shim/tar" <<EOF+#!/usr/bin/env bash+if [ "\$1" = "-czf" ]; then+ printf 'partial' >"\$2"+ echo "tar: Write error" >&2+ exit 2+fi+exec "$REAL_TAR" "\$@"+EOF+chmod +x "$AL/shim/tar"+set +e+(cd "$AL/src" && PATH="$AL/shim" BACKUP_DIR="$AL/out" "$BACKUP" >/dev/null 2>&1)+RC_AL=$?+set -e+[ "$RC_AL" -ne 0 ] || fail "(al) backup exited 0 after its tar failed"+if [ -n "$(ls -A "$AL/out" 2>/dev/null || true)" ]; then+ fail "(al) a truncated archive was left behind: $(ls -A "$AL/out")"+fi+pass "(al) a failed tar exits non-zero and leaves nothing in BACKUP_DIR"+echo ""echo "All smoke tests passed!"