#!/bin/bash set -e echo "🔧 Conditional AzerothCore Database Import" echo "========================================" # Restoration status markers RESTORE_STATUS_DIR="/var/lib/mysql-persistent" RESTORE_SUCCESS_MARKER="$RESTORE_STATUS_DIR/.restore-completed" RESTORE_FAILED_MARKER="$RESTORE_STATUS_DIR/.restore-failed" echo "🔍 Checking restoration status..." # Check if backup was successfully restored if [ -f "$RESTORE_SUCCESS_MARKER" ]; then echo "✅ Backup restoration completed successfully" echo "📄 Restoration details:" cat "$RESTORE_SUCCESS_MARKER" echo "" echo "đŸšĢ Skipping database import - data already restored from backup" echo "💡 This prevents overwriting restored data with fresh schema" exit 0 fi # Check if restoration failed (fresh databases created) if [ -f "$RESTORE_FAILED_MARKER" ]; then echo "â„šī¸ No backup was restored - fresh databases detected" echo "📄 Database creation details:" cat "$RESTORE_FAILED_MARKER" echo "" echo "â–ļī¸ Proceeding with database import to populate fresh databases" else echo "âš ī¸ No restoration status found - assuming fresh installation" echo "â–ļī¸ Proceeding with database import" fi echo "" echo "🔧 Starting database import process..." # First attempt backup restoration echo "🔍 Checking for backups to restore..." BACKUP_DIRS="/backups" # Function to find and validate the most recent backup find_latest_backup() { # Priority 1: Legacy single backup file if [ -f "/var/lib/mysql-persistent/backup.sql" ]; then if head -10 "/var/lib/mysql-persistent/backup.sql" | grep -q "CREATE DATABASE\|INSERT INTO\|CREATE TABLE"; then echo "/var/lib/mysql-persistent/backup.sql" return 0 fi fi # Priority 2: Modern timestamped backups if [ -d "$BACKUP_DIRS" ] && [ "$(ls -A $BACKUP_DIRS)" ]; then # Try daily backups first if [ -d "$BACKUP_DIRS/daily" ] && [ "$(ls -A $BACKUP_DIRS/daily)" ]; then local latest_daily=$(ls -1t $BACKUP_DIRS/daily | head -n 1) if [ -n "$latest_daily" ] && [ -d "$BACKUP_DIRS/daily/$latest_daily" ]; then # Validate backup has actual data if ls "$BACKUP_DIRS/daily/$latest_daily"/*.sql.gz >/dev/null 2>&1; then for backup_file in "$BACKUP_DIRS/daily/$latest_daily"/*.sql.gz; do if [ -f "$backup_file" ] && [ -s "$backup_file" ]; then if zcat "$backup_file" | head -20 | grep -q "CREATE DATABASE\|INSERT INTO\|CREATE TABLE"; then echo "$BACKUP_DIRS/daily/$latest_daily" return 0 fi fi done fi fi fi fi return 1 } # Function to restore from timestamped backup directory restore_from_directory() { local backup_dir="$1" echo "🔄 Restoring from backup directory: $backup_dir" local restore_success=true # Restore each database backup for backup_file in "$backup_dir"/*.sql.gz; do if [ -f "$backup_file" ]; then local db_name=$(basename "$backup_file" .sql.gz) echo "đŸ“Ĩ Restoring database: $db_name" if zcat "$backup_file" | mysql -h ${CONTAINER_MYSQL} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD}; then echo "✅ Successfully restored $db_name" else echo "❌ Failed to restore $db_name" restore_success=false fi fi done if [ "$restore_success" = true ]; then return 0 else return 1 fi } # Attempt backup restoration backup_path=$(find_latest_backup) if [ $? -eq 0 ] && [ -n "$backup_path" ]; then echo "đŸ“Ļ Found backup directory: $(basename $backup_path)" if restore_from_directory "$backup_path"; then echo "✅ Database restoration completed successfully!" echo "$(date): Backup successfully restored from $backup_path" > "$RESTORE_SUCCESS_MARKER" echo "đŸšĢ Skipping schema import - data already restored from backup" exit 0 else echo "❌ Backup restoration failed - proceeding with fresh setup" echo "$(date): Backup restoration failed - proceeding with fresh setup" > "$RESTORE_FAILED_MARKER" fi else echo "â„šī¸ No valid backups found - proceeding with fresh setup" echo "$(date): No backup found - fresh setup needed" > "$RESTORE_FAILED_MARKER" fi # Create fresh databases if restoration didn't happen echo "đŸ—„ī¸ Creating fresh AzerothCore databases..." mysql -h ${CONTAINER_MYSQL} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e " CREATE DATABASE IF NOT EXISTS ${DB_AUTH_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE DATABASE IF NOT EXISTS ${DB_WORLD_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; CREATE DATABASE IF NOT EXISTS ${DB_CHARACTERS_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; SHOW DATABASES;" || { echo "❌ Failed to create databases" exit 1 } echo "✅ Fresh databases created - proceeding with schema import" # Wait for databases to be ready (they should exist now) echo "âŗ Verifying databases are accessible..." for i in $(seq 1 10); do if mysql -h ${CONTAINER_MYSQL} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e "USE ${DB_AUTH_NAME}; USE ${DB_WORLD_NAME}; USE ${DB_CHARACTERS_NAME};" >/dev/null 2>&1; then echo "✅ All databases accessible" break fi echo "âŗ Waiting for databases... attempt $i/10" sleep 2 done # Verify databases are actually empty before importing echo "🔍 Verifying databases are empty before import..." check_table_count() { local db_name="$1" local count=$(mysql -h ${CONTAINER_MYSQL} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e " SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='$db_name' AND table_type='BASE TABLE';" -s -N 2>/dev/null || echo "0") echo "$count" } auth_tables=$(check_table_count "${DB_AUTH_NAME}") world_tables=$(check_table_count "${DB_WORLD_NAME}") char_tables=$(check_table_count "${DB_CHARACTERS_NAME}") echo "📊 Current table counts:" echo " ${DB_AUTH_NAME}: $auth_tables tables" echo " ${DB_WORLD_NAME}: $world_tables tables" echo " ${DB_CHARACTERS_NAME}: $char_tables tables" # Warn if databases appear to have data if [ "$auth_tables" -gt 5 ] || [ "$world_tables" -gt 50 ] || [ "$char_tables" -gt 5 ]; then echo "âš ī¸ WARNING: Databases appear to contain data!" echo "âš ī¸ Import may overwrite existing data. Consider backing up first." echo "âš ī¸ Continuing in 10 seconds... (Ctrl+C to cancel)" sleep 10 fi echo "📝 Creating dbimport configuration..." mkdir -p /azerothcore/env/dist/etc cat > /azerothcore/env/dist/etc/dbimport.conf < "$RESTORE_STATUS_DIR/.import-completed" # Verify import was successful echo "🔍 Verifying import results..." auth_tables_after=$(check_table_count "${DB_AUTH_NAME}") world_tables_after=$(check_table_count "${DB_WORLD_NAME}") char_tables_after=$(check_table_count "${DB_CHARACTERS_NAME}") echo "📊 Post-import table counts:" echo " ${DB_AUTH_NAME}: $auth_tables_after tables" echo " ${DB_WORLD_NAME}: $world_tables_after tables" echo " ${DB_CHARACTERS_NAME}: $char_tables_after tables" if [ "$auth_tables_after" -gt 0 ] && [ "$world_tables_after" -gt 0 ]; then echo "✅ Import verification successful - databases populated" else echo "âš ī¸ Import verification failed - databases may be empty" fi else echo "❌ Database import failed!" echo "$(date): Database import failed" > "$RESTORE_STATUS_DIR/.import-failed" exit 1 fi echo "🎉 Database import process complete!"