Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): restore a state directory in place
Yudhi Armyndharis6cf80ec4679b
diff --git a/scripts/restore.sh b/scripts/restore.shindex 0c823e81b..31a6d819c 100755--- a/scripts/restore.sh+++ b/scripts/restore.sh@@ -168,9 +168,16 @@ replace_tree() {returnfilog "Restoring $label"- rm -rf -- "$target_dir"- mkdir -p "$(dirname "$target_dir")"- cp -pR "$source_dir" "$target_dir"+ if [ -d "$target_dir" ] && [ ! -L "$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"+ else+ rm -rf -- "$target_dir"+ mkdir -p "$(dirname "$target_dir")"+ cp -pR "$source_dir" "$target_dir"+ fi}# The data-dir safety snapshot below cannot cover a target that lives OUTSIDE it (a customdiff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 3187e735c..159953329 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -19,6 +19,8 @@# (m) OPENWA_RESTORE_SNAPSHOT_DIR takes the data-dir snapshot off a read-only parent (skipped as root)# (n) a state dir outside the data dir is snapshotted before any database is written, and under# OPENWA_RESTORE_SNAPSHOT_DIR when that is set (skipped as root)+# (o) such a state dir under a read-only parent, a mount point in the container, is restored in+# place (skipped as root)## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -592,8 +594,30 @@ if [ "$(id -u)" -ne 0 ]; thenfail "(n) the archived sessions were not restored"fipass "(n) external state snapshotted before the databases are written, under OPENWA_RESTORE_SNAPSHOT_DIR"++ echo ""+ echo "==> (o) a state dir whose parent is read-only is restored in place"+ # A volume mounted at /sessions can be emptied but not removed or re-created: its parent is the+ # read-only root. The unwritable parent stands in for that here.+ printf 'oscar-stale\n' >"$N/ro/sessions/stale"+ chmod a-w "$N/ro"+ restore_n "$N/ro/sessions" "$N/snapshots-o"+ chmod u+w "$N/ro"+ if [ "$RC" -ne 0 ]; then+ fail "(o) restore into a state dir under a read-only parent failed: $OUT"+ fi+ if [ "$(cat "$N/ro/sessions/session-s1/marker")" != "november-archive" ]; then+ fail "(o) the archived sessions were not restored into the directory"+ fi+ if [ -e "$N/ro/sessions/stale" ]; then+ fail "(o) a file the archive does not carry survived the restore"+ fi+ if [ "$(cat "$N"/snapshots-o/sessions.pre-restore-*/session-s1/marker 2>/dev/null || true)" != "november-live" ]; then+ fail "(o) the sessions snapshot is not under OPENWA_RESTORE_SNAPSHOT_DIR"+ fi+ pass "(o) a state dir under a read-only parent is emptied and refilled in place"else- echo "SKIP: (n) running as root, which ignores the permission bits this case relies on"+ echo "SKIP: (n) and (o) running as root, which ignores the permission bits these cases rely on"fiecho ""