#!/usr/bin/bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck disable=SC1091
source "$SCRIPT_DIR/pd_backup_lib.sh"

stamp="$(timestamp_utc)"
dbs="${POSTGRES_DB_LIST:-}"

if [[ -z "$dbs" ]]; then
  echo "POSTGRES_DB_LIST is empty; nothing to dump." >&2
  exit 1
fi

for db in $dbs; do
  out="$PD_DB_DUMP_ROOT/${db}_${stamp}.sql.gz"
  echo "Dumping Postgres database '$db' to $out"
  docker_cmd exec "$POSTGRES_CONTAINER" pg_dump -U postgres "$db" | gzip -9 > "$out"
done

echo "Postgres dumps complete: $PD_DB_DUMP_ROOT"
