#!/usr/bin/env bash set -euo pipefail : "${AIGW_RESTORE_DATABASE_URL:?AIGW_RESTORE_DATABASE_URL must point to an isolated drill database}" backup="${1:?usage: restore-drill.sh BACKUP.dump}" [[ -f "$backup" && -f "$backup.sha256" ]] || { printf 'backup or checksum is missing\n' >&2; exit 2; } sha256sum -c "$backup.sha256" case "$AIGW_RESTORE_DATABASE_URL" in *localhost*|*127.0.0.1*|*restore*|*drill*) ;; *) printf 'refusing restore: target URL must visibly identify localhost, restore, or drill\n' >&2; exit 2 ;; esac pg_restore --dbname="$AIGW_RESTORE_DATABASE_URL" --clean --if-exists --no-owner "$backup" psql "$AIGW_RESTORE_DATABASE_URL" -v ON_ERROR_STOP=1 -c "SELECT count(*) AS tenants FROM tenants; SELECT count(*) AS usage_events FROM usage_events; SELECT count(*) AS ledger_entries FROM billing_ledger;" printf 'restore drill completed for %s\n' "$backup"