#!/bin/sh set -e # The dump lives at the db-data volume root: backup-bot-two v2 snapshots paths inside # named volumes (backupbot.backup.volumes.db-data.path), not the container root fs. DUMP=/var/lib/postgresql/data/postgres.dump backup() { pg_dump -U "$POSTGRES_USER" -Fc "$POSTGRES_DB" | gzip > "$DUMP.gz" } backup_cleanup() { rm -f "$DUMP.gz" } restore() { gzip -d "$DUMP.gz" # --if-exists: otherwise DROPs on objects absent from the live db error out and # pg_restore exits 1, killing the chain and leaving the dump behind. pg_restore --clean --if-exists -U "$POSTGRES_USER" --dbname="$POSTGRES_DB" < "$DUMP" rm -f "$DUMP" # pg_restore --clean recreates objects under the live app, so its pooled connections # keep stale type-OID caches ('cache lookup failed for type ...' crash loops, e.g. # Oban). Terminate them so Ecto reconnects fresh. psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = current_database() AND pid <> pg_backend_pid();" } "$@"