Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): fail backup.sh up front on an unwritable BACKUP_DIR
Yudhi Armyndharis2c013d0aebc4
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.mdindex 3df2fde3c..02aa2c329 100644--- a/docs/11-operational-runbooks.md+++ b/docs/11-operational-runbooks.md@@ -590,6 +590,18 @@ OPENWA_DATA_DIR=/srv/openwa/data \> compose. Run the script where that volume is mounted — e.g. point `OPENWA_DATA_DIR`> at the volume's mountpoint, or run it inside a container with `/app/data` mounted.>+> The shipped compose file and Helm chart mount the container root read-only, so the default+> `./backups` (`/app/backups`) cannot be created there and the script refuses to start. Inside the+> container, write to the data volume and then copy the archive off it, since an archive on the same+> volume as the data does not survive losing that volume:+>+> ```bash+> docker exec -e BACKUP_DIR=/app/data/backups openwa-api ./scripts/backup.sh+> docker cp openwa-api:/app/data/backups/. ./backups/+> # Helm: kubectl exec <pod> -- env BACKUP_DIR=/app/data/backups ./scripts/backup.sh+> # kubectl cp <pod>:/app/data/backups ./backups+> ```+>> The scripts resolve every other path the way the application does: an explicit environment value> first, then `./.env`, then `<data dir>/.env.generated`. Settings made through Dashboard >> Infrastructure therefore apply without being restated on the command line. Two caveats whendiff --git a/scripts/backup.sh b/scripts/backup.shindex b3e834743..06cb621ae 100755--- a/scripts/backup.sh+++ b/scripts/backup.sh@@ -77,6 +77,14 @@ ADMIN_KEY_FILE="$DATA_DIR/.api-key"log() { echo "[backup] $*"; }+# Check the destination before staging anything. The shipped container's root filesystem is+# read-only, so the default ./backups (/app/backups) cannot be created there; failing only at the+# end would first copy every database, session and media file into the memory-backed /tmp.+if ! mkdir -p "$BACKUP_DIR" 2>/dev/null || [ ! -w "$BACKUP_DIR" ]; then+ log "ERROR: BACKUP_DIR=$BACKUP_DIR is not writable; point BACKUP_DIR at a writable, persistent directory (inside the read-only container use BACKUP_DIR=/app/data/backups)"+ exit 1+fi+STAGE="$(mktemp -d)"trap 'rm -rf "$STAGE"' EXIT@@ -188,7 +196,6 @@ if [ -f "$ADMIN_KEY_FILE" ]; thencp -p "$ADMIN_KEY_FILE" "$STAGE/.api-key"fi-mkdir -p "$BACKUP_DIR"ARCHIVE="$BACKUP_DIR/openwa-backup-$TIMESTAMP.tar.gz"tar -czf "$ARCHIVE" -C "$STAGE" .diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex d6d8ae176..f30ba762e 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -471,5 +471,34 @@ elseecho "SKIP: (k) sqlite3 not found on this host, so there is no rc file to load"fi+echo ""+echo "==> (l) an unwritable BACKUP_DIR fails before anything is staged"+# The shipped container mounts its root read-only, so the default ./backups cannot be created. The+# run must stop up front, not after copying every database and media file into /tmp.+if [ "$(id -u)" -ne 0 ]; then+ L="$WORK/l"+ mkdir -p "$L/data" "$L/ro"+ make_fixture "$L/data/main.sqlite" "l-main"+ make_fixture "$L/data/openwa.sqlite" "l-data"+ chmod a-w "$L/ro"+ set +e+ OUT_L="$(cd "$L" && BACKUP_DIR="$L/ro/out" "$BACKUP" 2>&1)"+ RC_L=$?+ set -e+ chmod u+w "$L/ro"+ if [ "$RC_L" -eq 0 ]; then+ fail "(l) backup.sh exited 0 with an unwritable BACKUP_DIR"+ fi+ if ! printf '%s' "$OUT_L" | grep -q 'BACKUP_DIR=.* is not writable'; then+ fail "(l) error message does not name the unwritable BACKUP_DIR"+ fi+ if printf '%s' "$OUT_L" | grep -q 'Backing up'; then+ fail "(l) state was staged before the BACKUP_DIR check"+ fi+ pass "(l) unwritable BACKUP_DIR -> non-zero exit before staging, clear message"+else+ echo "SKIP: (l) running as root, which ignores the permission bits this case relies on"+fi+echo ""echo "All smoke tests passed!"