alphax/scripts/postgres/restore.sh
2026-05-16 14:52:10 +08:00

33 lines
727 B
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
if [[ $# -lt 1 ]]; then
echo "Usage: $0 <backup.dump> [--yes]" >&2
exit 2
fi
BACKUP_FILE="$1"
YES="${2:-}"
if [[ -z "${DATABASE_URL:-}" ]]; then
echo "ERROR: DATABASE_URL is required" >&2
exit 1
fi
if [[ ! -f "$BACKUP_FILE" ]]; then
echo "ERROR: backup file not found: $BACKUP_FILE" >&2
exit 1
fi
if [[ "$YES" != "--yes" ]]; then
echo "This will restore into DATABASE_URL and may overwrite existing objects."
read -r -p "Type RESTORE to continue: " CONFIRM
if [[ "$CONFIRM" != "RESTORE" ]]; then
echo "restore cancelled"
exit 1
fi
fi
pg_restore --clean --if-exists --no-owner --dbname="$DATABASE_URL" "$BACKUP_FILE"
echo "[restore] restored $BACKUP_FILE"