#!/usr/bin/env bash # Restores a dump produced by backup-db.sh into the running "postgres" container. # Destructive: drops and recreates every object in the target database before restoring. # # Usage: scripts/restore-db.sh path/to/dnd_backup_20260101_120000.dump [-y] # -y skip the confirmation prompt (for non-interactive/scripted use) set -euo pipefail cd "$(dirname "$0")/.." skip_confirm=false file="" for arg in "$@"; do if [ "$arg" = "-y" ]; then skip_confirm=true else file="$arg" fi done if [ -z "$file" ]; then echo "Usage: $0 path/to/backup.dump [-y]" >&2 exit 1 fi if [ ! -f "$file" ]; then echo "Error: file not found: $file" >&2 exit 1 fi if [ "$skip_confirm" = false ]; then read -r -p "This will DROP and recreate every table in the target database. Continue? [y/N] " reply case "$reply" in [yY]|[yY][eE][sS]) ;; *) echo "Aborted."; exit 1 ;; esac fi echo "Restoring ${file} ..." docker compose exec -T postgres sh -c \ 'pg_restore -U "$POSTGRES_USER" -d "$POSTGRES_DB" --clean --if-exists --no-owner --no-privileges --single-transaction' \ < "$file" echo "Done."