Files
basil/scripts/restore-standalone-postgres.sh
Paul R Kartchner 883b7820ed
All checks were successful
Basil CI/CD Pipeline / Shared Package Tests (push) Successful in 1m38s
Basil CI/CD Pipeline / Security Scanning (push) Successful in 1m55s
Basil CI/CD Pipeline / Web Tests (push) Successful in 2m9s
Basil CI/CD Pipeline / Build All Packages (push) Successful in 1m31s
Basil CI/CD Pipeline / Code Linting (push) Successful in 1m57s
Basil CI/CD Pipeline / API Tests (push) Successful in 2m34s
Basil CI/CD Pipeline / E2E Tests (push) Has been skipped
Basil CI/CD Pipeline / Build & Push Docker Images (push) Successful in 5m5s
Basil CI/CD Pipeline / Trigger Deployment (push) Successful in 12s
docs: add comprehensive database migration and backup documentation
Added complete guide for migrating from containerized PostgreSQL to standalone
server with production-grade backup strategies.

New files:
- docs/DATABASE-MIGRATION-GUIDE.md - Complete migration guide with step-by-step
  instructions, troubleshooting, and rollback procedures
- scripts/backup-standalone-postgres.sh - Automated backup script with daily,
  weekly, and monthly retention policies
- scripts/restore-standalone-postgres.sh - Safe restore script with verification
  and pre-restore safety backup

Features:
- Hybrid backup strategy (PostgreSQL native + Basil API)
- Automated retention policy (30/90/365 days)
- Integrity verification
- Safety backups before restore
- Complete troubleshooting guide
- Rollback procedures

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-20 15:29:35 -07:00

89 lines
2.5 KiB
Bash
Executable File

#!/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 <<EOF
DROP DATABASE IF EXISTS $DB_NAME;
CREATE DATABASE $DB_NAME;
GRANT ALL PRIVILEGES ON DATABASE $DB_NAME TO $DB_USER;
EOF
# Restore from backup
echo "Restoring from backup..."
gunzip -c "$BACKUP_FILE" | psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME"
# Verify restore
echo "Verifying restore..."
RECIPE_COUNT=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME" -t -c "SELECT COUNT(*) FROM \"Recipe\";")
COOKBOOK_COUNT=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" "$DB_NAME" -t -c "SELECT COUNT(*) FROM \"Cookbook\";")
echo ""
echo "===== Restore Complete ====="
echo "Recipes: $RECIPE_COUNT"
echo "Cookbooks: $COOKBOOK_COUNT"
echo "Pre-restore backup saved at: $PRE_RESTORE_BACKUP"
echo ""
echo "If something went wrong, you can restore from the safety backup:"
echo " gunzip -c $PRE_RESTORE_BACKUP | psql -h $DB_HOST -U $DB_USER $DB_NAME"