agenthub/scripts/restore.sh
Paperclip FoundingEngineer bdd5d92ba7 Initial AgentHub codebase for Coolify deployment
Complete implementation ready for Coolify:
- Node.js 22 + Fastify + socket.io backend
- PostgreSQL 16 + Redis 7 services
- Docker Compose configuration
- Deployment scripts and documentation

Co-Authored-By: Paperclip <noreply@paperclip.ing>
2026-05-01 21:25:57 +00:00

114 lines
3.2 KiB
Bash
Executable file

#!/usr/bin/env bash
set -euo pipefail
# AgentHub Postgres Restore Script
# Restores a pg_dump backup file to a Postgres database
#
# Usage:
# ./restore.sh <backup-file> [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 <backup-file> [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 <<SQL
DROP DATABASE IF EXISTS ${TARGET_DB};
CREATE DATABASE ${TARGET_DB} OWNER ${PGUSER};
SQL
# Restore from backup
echo "[$(date -Iseconds)] Restoring from backup file"
pg_restore -h "${PGHOST}" -p "${PGPORT}" -U "${PGUSER}" -d "${TARGET_DB}" \
--verbose \
--no-owner \
--no-acl \
"${BACKUP_FILE}"
echo "[$(date -Iseconds)] Restore completed"
# Verify restoration
echo ""
echo "[$(date -Iseconds)] Verifying restore..."
TABLE_COUNT=$(psql -h "${PGHOST}" -p "${PGPORT}" -U "${PGUSER}" -d "${TARGET_DB}" -t -c "
SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';
")
echo "Tables restored: ${TABLE_COUNT}"
if [[ "${TABLE_COUNT}" -lt 1 ]]; then
echo "WARNING: No tables found in restored database"
exit 1
fi
echo ""
echo "========================================="
echo "Restore successful!"
echo "========================================="
echo "Database '${TARGET_DB}' has been restored from:"
echo " ${BACKUP_FILE}"
echo ""
echo "Next steps:"
echo " - Verify data integrity"
echo " - Run application smoke tests"
echo " - If restoring to production, update connection strings"