Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(backup): let the online SQLite backup finish while the app writes

Yudhi Armyndharis108972471036
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 1840e928a..8e87afd44 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -179,7 +179,12 @@ backup_sqlite() {
exit 1
fi
if command -v sqlite3 >/dev/null 2>&1; then
- sqlite3 "$src" ".backup '$dest'"
+ # The app writes in rollback-journal mode, and a bare .backup gave up on the first lock it met and
+ # restarted after every outside write, so a busy gateway never got a backup. The read transaction
+ # lets the copy finish in one pass (app writes wait for it), the busy timeout waits out a commit,
+ # and -init /dev/null keeps the operator's sqlite3 rc file out of the run.
+ sqlite3 -init /dev/null -cmd ".timeout 30000" "$src" \
+ "BEGIN" "SELECT count(*) FROM sqlite_master" ".backup '$dest'" "COMMIT" >/dev/null
else
log "WARN: sqlite3 not found — plain-copying live database $src (the snapshot may be torn)"
cp "$src" "$dest"
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index fafbd36a9..58946119c 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -47,6 +47,7 @@
# backup, a file the app deletes during the media or plugin copies is logged, and any other cp
# 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)
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -1360,5 +1361,45 @@ if [ "$RC_AE" -ne 0 ] || [ -z "$(ls "$AE"/out/openwa-backup-*.tar.gz 2>/dev/null
fi
pass "(ae) the min-content check reads the whole listing"
+echo ""
+if [ "$HAS_SQLITE3" -eq 1 ]; then
+ echo "==> (af) the online SQLite backup waits out a writer holding the database lock"
+ # The app writes several times a second in rollback-journal mode. A bare .backup gave up on the first
+ # lock it met with 'database is locked', so on a busy gateway no online backup completed.
+ AF="$WORK/af"
+ mkdir -p "$AF/data"
+ make_fixture "$AF/data/main.sqlite" "foxtrot2-main"
+ make_fixture "$AF/data/openwa.sqlite" "foxtrot2-data"
+ {
+ echo 'BEGIN EXCLUSIVE;'
+ echo "INSERT INTO sentinel VALUES('foxtrot2-late');"
+ echo ".system touch '$AF/locked'"
+ sleep 2
+ echo 'COMMIT;'
+ } | sqlite3 "$AF/data/main.sqlite" &
+ WRITER_AF=$!
+ for _ in $(seq 1 100); do
+ [ -f "$AF/locked" ] && break
+ sleep 0.1
+ done
+ [ -f "$AF/locked" ] || fail "(af) the writer never took the database lock"
+ set +e
+ OUT_AF="$(cd "$AF" && BACKUP_DIR="$AF/out" "$BACKUP" 2>&1)"
+ RC_AF=$?
+ set -e
+ wait "$WRITER_AF"
+ if [ "$RC_AF" -ne 0 ]; then
+ fail "(af) the backup failed while a writer held the database lock: $OUT_AF"
+ fi
+ mkdir -p "$AF/extract"
+ tar -xzf "$(ls "$AF"/out/openwa-backup-*.tar.gz)" -C "$AF/extract"
+ if [ "$(sqlite3 "$AF/extract/main.sqlite" 'PRAGMA integrity_check;')" != ok ]; then
+ fail "(af) the snapshot taken after the writer let go is not a sound database"
+ fi
+ pass "(af) the online backup waits for the lock instead of failing"
+else
+ echo "SKIP: (af) sqlite3 not found on this host, so there is no database lock to wait on"
+fi
+
echo ""
echo "All smoke tests passed!"