exclude tables in sync
Build and Push Docker Images / build-and-push (push) Successful in 1m48s

This commit is contained in:
Bram committed 2026-11-16 16:26:41 +01:00
1 parent 415bb3906a
commit 94f1021fd0
5 files changed
+36 -1

No files matched your search

+2
View File
@@ -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=
+1
View File
@@ -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"]
+14
View File
@@ -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).
+3 -1
View File
@@ -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:
+16
View File
@@ -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"