This commit is contained in:
1 parent
415bb3906a
commit
94f1021fd0
5 files changed
+36
-1
No files matched your search
@@ -6,3 +6,5 @@ CRON=0 3 * * *
|
|||||||
# Optional
|
# Optional
|
||||||
TZ=Europe/Brussels
|
TZ=Europe/Brussels
|
||||||
ON_STARTUP=true
|
ON_STARTUP=true
|
||||||
|
# Comma/space separated tables or pg_dump patterns to skip, e.g. public.logs,schema.audit_*
|
||||||
|
EXCLUDE_TABLES=
|
||||||
@@ -28,5 +28,6 @@ ENV SOURCE_DB_URL=""
|
|||||||
ENV DEST_DB_URL=""
|
ENV DEST_DB_URL=""
|
||||||
ENV CRON=""
|
ENV CRON=""
|
||||||
ENV ON_STARTUP=true
|
ENV ON_STARTUP=true
|
||||||
|
ENV EXCLUDE_TABLES=""
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/local/bin/entrypoint.py"]
|
ENTRYPOINT ["/usr/local/bin/entrypoint.py"]
|
||||||
@@ -21,9 +21,23 @@ De source-database wordt **nooit** aangepast: alleen `pg_dump` (reads/SELECT), m
|
|||||||
|----------|---------|-------------|
|
|----------|---------|-------------|
|
||||||
| `TZ` | `Europe/Brussels` | Tijdzone |
|
| `TZ` | `Europe/Brussels` | Tijdzone |
|
||||||
| `ON_STARTUP` | `true` | Direct syncen bij opstart (`true`/`false`) |
|
| `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`
|
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
|
## Gedrag
|
||||||
|
|
||||||
1. Dump van source via `pg_dump` (custom format, read-only).
|
1. Dump van source via `pg_dump` (custom format, read-only).
|
||||||
|
|||||||
@@ -98,11 +98,13 @@ def main() -> None:
|
|||||||
expr = resolve_cron_expr()
|
expr = resolve_cron_expr()
|
||||||
tz = zone()
|
tz = zone()
|
||||||
on_startup = run_on_startup()
|
on_startup = run_on_startup()
|
||||||
|
exclude_tables = os.environ.get("EXCLUDE_TABLES", "").strip()
|
||||||
|
|
||||||
log(
|
log(
|
||||||
"Starting database-syncer "
|
"Starting database-syncer "
|
||||||
f"(TZ={tz.key}, CRON='{expr}', "
|
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:
|
if on_startup:
|
||||||
|
|||||||
@@ -20,10 +20,25 @@ if [ -z "$DEST_DB_URL" ]; then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
DUMP_FILE="${DUMP_FILE:-/tmp/db-sync.dump}"
|
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.
|
# Extra safety: force read-only transactions on the source connection.
|
||||||
export PGOPTIONS_SOURCE="${PGOPTIONS_SOURCE:--c default_transaction_read_only=on}"
|
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)"
|
log "Starting sync (source -> dest)"
|
||||||
|
if [ "${#exclude_args[@]}" -gt 0 ]; then
|
||||||
|
log "Excluding tables: $EXCLUDE_TABLES"
|
||||||
|
fi
|
||||||
log "Dumping source (read-only)..."
|
log "Dumping source (read-only)..."
|
||||||
|
|
||||||
# pg_dump only reads from source. --clean/--if-exists apply DROP statements to DEST on restore.
|
# 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-owner \
|
||||||
--no-acl \
|
--no-acl \
|
||||||
--verbose \
|
--verbose \
|
||||||
|
"${exclude_args[@]}" \
|
||||||
--file="$DUMP_FILE" \
|
--file="$DUMP_FILE" \
|
||||||
"$SOURCE_DB_URL"
|
"$SOURCE_DB_URL"
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user