diff --git a/Dockers/database-syncer/.env.example b/Dockers/database-syncer/.env.example index 973f8b7..b3be3e9 100644 --- a/Dockers/database-syncer/.env.example +++ b/Dockers/database-syncer/.env.example @@ -6,3 +6,5 @@ CRON=0 3 * * * # Optional TZ=Europe/Brussels ON_STARTUP=true +# Comma/space separated tables or pg_dump patterns to skip, e.g. public.logs,schema.audit_* +EXCLUDE_TABLES= diff --git a/Dockers/database-syncer/Dockerfile b/Dockers/database-syncer/Dockerfile index c9ae590..6c44c93 100644 --- a/Dockers/database-syncer/Dockerfile +++ b/Dockers/database-syncer/Dockerfile @@ -28,5 +28,6 @@ ENV SOURCE_DB_URL="" ENV DEST_DB_URL="" ENV CRON="" ENV ON_STARTUP=true +ENV EXCLUDE_TABLES="" ENTRYPOINT ["/usr/local/bin/entrypoint.py"] diff --git a/Dockers/database-syncer/README.md b/Dockers/database-syncer/README.md index 8fb7426..b76e524 100644 --- a/Dockers/database-syncer/README.md +++ b/Dockers/database-syncer/README.md @@ -21,9 +21,23 @@ De source-database wordt **nooit** aangepast: alleen `pg_dump` (reads/SELECT), m |----------|---------|-------------| | `TZ` | `Europe/Brussels` | Tijdzone | | `ON_STARTUP` | `true` | Direct syncen bij opstart (`true`/`false`) | +| `EXCLUDE_TABLES` | *(leeg)* | Komma- en/of spatiegescheiden lijst van tabellen die niet gesynchroniseerd worden | URL-formaat: `postgresql://user:password@host:5432/dbname` +## Tabellen uitsluiten + +Met `EXCLUDE_TABLES` sluit je tabellen uit van de sync. De waarde is een +komma- en/of spatiegescheiden lijst en wordt als `pg_dump --exclude-table` +patroon gebruikt, dus schema-kwalificatie en wildcards werken: + +```bash +-e 'EXCLUDE_TABLES=public.logs,schema.audit_*' +``` + +Uitgesloten tabellen worden noch aangemaakt noch gevuld op de destination; +bestaande data in die tabellen op de destination blijft ongemoeid. + ## Gedrag 1. Dump van source via `pg_dump` (custom format, read-only). diff --git a/Dockers/database-syncer/entrypoint.py b/Dockers/database-syncer/entrypoint.py index 2a40982..6cf5ea9 100755 --- a/Dockers/database-syncer/entrypoint.py +++ b/Dockers/database-syncer/entrypoint.py @@ -98,11 +98,13 @@ def main() -> None: expr = resolve_cron_expr() tz = zone() on_startup = run_on_startup() + exclude_tables = os.environ.get("EXCLUDE_TABLES", "").strip() log( "Starting database-syncer " f"(TZ={tz.key}, CRON='{expr}', " - f"SOURCE='{redacted(source)}', DEST='{redacted(dest)}')" + f"SOURCE='{redacted(source)}', DEST='{redacted(dest)}', " + f"EXCLUDE_TABLES='{exclude_tables or '-'}')" ) if on_startup: diff --git a/Dockers/database-syncer/sync.sh b/Dockers/database-syncer/sync.sh index cbd2fda..cc48e8b 100755 --- a/Dockers/database-syncer/sync.sh +++ b/Dockers/database-syncer/sync.sh @@ -20,10 +20,25 @@ if [ -z "$DEST_DB_URL" ]; then fi DUMP_FILE="${DUMP_FILE:-/tmp/db-sync.dump}" +# Comma- and/or space-separated list of tables (or patterns) to skip entirely. +# Accepts pg_dump patterns, e.g. "public.logs, schema.audit_*". +EXCLUDE_TABLES="${EXCLUDE_TABLES:-}" # Extra safety: force read-only transactions on the source connection. export PGOPTIONS_SOURCE="${PGOPTIONS_SOURCE:--c default_transaction_read_only=on}" +exclude_args=() +if [ -n "$EXCLUDE_TABLES" ]; then + # Split on commas and whitespace. + read -r -a exclude_items <<< "$(printf '%s' "$EXCLUDE_TABLES" | tr ',' ' ')" + for item in "${exclude_items[@]}"; do + [ -n "$item" ] && exclude_args+=(--exclude-table="$item") + done +fi + log "Starting sync (source -> dest)" +if [ "${#exclude_args[@]}" -gt 0 ]; then + log "Excluding tables: $EXCLUDE_TABLES" +fi log "Dumping source (read-only)..." # pg_dump only reads from source. --clean/--if-exists apply DROP statements to DEST on restore. @@ -32,6 +47,7 @@ PGOPTIONS="$PGOPTIONS_SOURCE" pg_dump \ --no-owner \ --no-acl \ --verbose \ + "${exclude_args[@]}" \ --file="$DUMP_FILE" \ "$SOURCE_DB_URL"