#!/bin/bash # # Basil Restore Script for Standalone PostgreSQL # Run manually when you need to restore from backup # # Usage: ./restore-standalone-postgres.sh /path/to/backup.sql.gz set -e # Configuration DB_HOST="localhost" DB_PORT="5432" DB_NAME="basil" DB_USER="basil" # Check arguments if [ $# -eq 0 ]; then echo "Usage: $0 /path/to/backup.sql.gz" echo "" echo "Available backups:" echo "Daily:" ls -lh /var/backups/basil/daily/ 2>/dev/null | tail -5 echo "" echo "Weekly:" ls -lh /var/backups/basil/weekly/ 2>/dev/null | tail -5 exit 1 fi BACKUP_FILE="$1" # Verify backup file exists if [ ! -f "$BACKUP_FILE" ]; then echo "ERROR: Backup file not found: $BACKUP_FILE" exit 1 fi # Verify backup integrity echo "Verifying backup integrity..." if ! gzip -t "$BACKUP_FILE"; then echo "ERROR: Backup file is corrupted!" exit 1 fi # Confirm restore echo "===== WARNING =====" echo "This will DESTROY all current data in database: $DB_NAME" echo "Backup file: $BACKUP_FILE" echo "Database: $DB_USER@$DB_HOST:$DB_PORT/$DB_NAME" echo "" read -p "Are you sure you want to continue? (type 'yes' to confirm): " CONFIRM if [ "$CONFIRM" != "yes" ]; then echo "Restore cancelled." exit 0 fi # Create backup of current database before restore TIMESTAMP=$(date +%Y%m%d_%H%M%S) PRE_RESTORE_BACKUP="/tmp/basil-pre-restore-$TIMESTAMP.sql.gz" echo "Creating safety backup of current database..." pg_dump -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME" | gzip > "$PRE_RESTORE_BACKUP" echo "Safety backup created: $PRE_RESTORE_BACKUP" # Drop and recreate database echo "Dropping existing database..." psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" postgres <