#!/bin/bash # Simple Database Migration Script set -e # Load database credentials from .env file DB_USER="tilbudgivern_service" DB_PASS="REDACTED_PASSWORD" DB_NAME="tilbudgivern" echo "🔄 Starting database migration from s.alw.dk..." # Test SSH connection echo "Testing SSH connection..." ssh -p 55555 alex@s.alw.dk "echo 'SSH OK'" # Create backup directory mkdir -p /tmp/db_backup cd /tmp/db_backup echo "📥 Exporting database from remote server..." # Export database without triggers/views/routines that cause permission issues ssh -p 55555 alex@s.alw.dk "sudo mysqldump tilbudgivern --single-transaction --skip-triggers --skip-routines --skip-add-locks --no-create-info --no-data --skip-comments" > tilbudgivern_structure.sql ssh -p 55555 alex@s.alw.dk "sudo mysqldump tilbudgivern --single-transaction --no-create-info --skip-triggers --skip-routines --skip-add-locks --ignore-table=tilbudgivern.bygma_current_prices --ignore-table=tilbudgivern.v_bygma_current_prices --ignore-table=tilbudgivern.v_bygma_import_stats --ignore-table=tilbudgivern.v_bygma_price_changes --ignore-table=tilbudgivern.v_enhanced_quotes --ignore-table=tilbudgivern.package_overview" > tilbudgivern_data.sql # Check if export was successful if [ -s tilbudgivern_data.sql ]; then echo "✅ Database exported successfully ($(du -h tilbudgivern_data.sql | cut -f1))" else echo "❌ Database export failed" exit 1 fi echo "📤 Importing to local database..." # First, ensure we have the enhanced schema echo "Setting up enhanced schema..." mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < ../setup_enhanced_features.sql 2>/dev/null || echo "Schema setup completed" # Import data mysql -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < tilbudgivern_data.sql if [ $? -eq 0 ]; then echo "✅ Database migration completed successfully!" # Show table count echo "📊 Database statistics:" mysql -u "$DB_USER" -p"$DB_PASS" -e "USE $DB_NAME; SELECT COUNT(*) as 'Total Tables' FROM information_schema.tables WHERE table_schema = '$DB_NAME';" mysql -u "$DB_USER" -p"$DB_PASS" -e "USE $DB_NAME; SELECT 'customer_projects' as table_name, COUNT(*) as records FROM customer_projects UNION ALL SELECT 'project_quotes', COUNT(*) FROM project_quotes;" 2>/dev/null || echo "Some tables might not exist" else echo "❌ Database import failed" exit 1 fi echo "🎉 Migration complete!"