#!/usr/bin/env bash set -euo pipefail # AgentHub Postgres Restore Script # Restores a pg_dump backup file to a Postgres database # # Usage: # ./restore.sh [target-database] # # Examples: # # Restore to default database (from env or 'agenthub') # ./restore.sh /backups/agenthub_20260430_030000.dump # # # Restore to specific database # ./restore.sh /backups/agenthub_20260430_030000.dump agenthub_restore_test # # Environment variables: # PGHOST, PGPORT, PGUSER, PGPASSWORD - Postgres connection params # SKIP_CONFIRMATION - Set to 'yes' to skip interactive confirmation BACKUP_FILE="${1:-}" TARGET_DB="${2:-${PGDATABASE:-agenthub}}" if [[ -z "${BACKUP_FILE}" ]]; then echo "Usage: $0 [target-database]" echo "" echo "Example:" echo " $0 /backups/agenthub_20260430_030000.dump" exit 1 fi if [[ ! -f "${BACKUP_FILE}" ]]; then echo "ERROR: Backup file not found: ${BACKUP_FILE}" exit 1 fi # Postgres connection from env PGHOST="${PGHOST:-localhost}" PGPORT="${PGPORT:-5432}" PGUSER="${PGUSER:-agenthub}" echo "=========================================" echo "AgentHub Database Restore" echo "=========================================" echo "Backup file: ${BACKUP_FILE}" echo "Target database: ${TARGET_DB}" echo "Host: ${PGHOST}:${PGPORT}" echo "User: ${PGUSER}" echo "=========================================" echo "" # Get backup file info BACKUP_SIZE=$(stat -f%z "${BACKUP_FILE}" 2>/dev/null || stat -c%s "${BACKUP_FILE}") echo "Backup size: $((BACKUP_SIZE / 1024 / 1024)) MB" # Confirm before proceeding if [[ "${SKIP_CONFIRMATION:-}" != "yes" ]]; then echo "" echo "WARNING: This will DROP and recreate the target database." echo "All existing data in '${TARGET_DB}' will be LOST." echo "" read -p "Continue? (yes/no): " CONFIRM if [[ "${CONFIRM}" != "yes" ]]; then echo "Restore cancelled." exit 0 fi fi echo "" echo "[$(date -Iseconds)] Starting restore..." # Drop and recreate database echo "[$(date -Iseconds)] Dropping and recreating database '${TARGET_DB}'" psql -h "${PGHOST}" -p "${PGPORT}" -U "${PGUSER}" -d postgres <