#!/bin/bash # Database restore script for spotlight.cam # Usage: ./scripts/restore-db.sh [dev|prod] set -e # Check if backup file is provided if [ -z "$1" ]; then echo "❌ Error: Backup file not specified" echo "Usage: ./scripts/restore-db.sh [dev|prod]" echo "Example: ./scripts/restore-db.sh ./backups/backup_dev_20251120_120000.sql dev" exit 1 fi BACKUP_FILE=$1 ENV=${2:-dev} # Check if backup file exists if [ ! -f "$BACKUP_FILE" ]; then echo "❌ Error: Backup file not found: $BACKUP_FILE" exit 1 fi # Set container name based on environment if [ "$ENV" = "prod" ]; then DB_CONTAINER="slc-db-prod" DB_NAME="spotlightcam" else DB_CONTAINER="slc-db" DB_NAME="spotlightcam" fi echo "⚠️ WARNING: This will REPLACE the current database!" echo "📦 Environment: $ENV" echo "🗄️ Container: $DB_CONTAINER" echo "💾 Backup file: $BACKUP_FILE" echo "" read -p "Are you sure you want to continue? (yes/no): " -r echo if [[ ! $REPLY =~ ^[Yy][Ee][Ss]$ ]]; then echo "❌ Restore cancelled" exit 0 fi # Check if container is running if ! docker ps --format '{{.Names}}' | grep -q "^${DB_CONTAINER}$"; then echo "❌ Error: Container $DB_CONTAINER is not running" exit 1 fi echo "🔄 Starting database restore..." # Restore backup cat "$BACKUP_FILE" | docker exec -i "$DB_CONTAINER" psql -U spotlightcam "$DB_NAME" # Check if restore was successful if [ $? -eq 0 ]; then echo "✅ Restore completed successfully!" else echo "❌ Restore failed!" exit 1 fi echo "✨ Done!"