move around init logic

This commit is contained in:
Deckard
2025-10-14 19:16:16 -04:00
parent a5cf153a10
commit 697c7b1c7b
2 changed files with 140 additions and 15 deletions

View File

@@ -48,6 +48,9 @@ services:
ac-backup: ac-backup:
image: ${MYSQL_IMAGE} image: ${MYSQL_IMAGE}
container_name: ${CONTAINER_BACKUP} container_name: ${CONTAINER_BACKUP}
depends_on:
ac-db-import:
condition: service_completed_successfully
environment: environment:
MYSQL_HOST: ${CONTAINER_MYSQL} MYSQL_HOST: ${CONTAINER_MYSQL}
MYSQL_PORT: ${MYSQL_PORT} MYSQL_PORT: ${MYSQL_PORT}
@@ -79,10 +82,13 @@ services:
networks: networks:
- azerothcore - azerothcore
# Step 3: Database initialization (one-time setup) # Step 3: Database initialization (conditional - only runs if import fails)
ac-db-init: ac-db-init:
image: ${MYSQL_IMAGE} image: ${MYSQL_IMAGE}
container_name: ${CONTAINER_DB_INIT} container_name: ${CONTAINER_DB_INIT}
depends_on:
ac-db-import:
condition: service_completed_successfully
volumes: volumes:
- ${STORAGE_PATH}/mysql-data:/var/lib/mysql-persistent - ${STORAGE_PATH}/mysql-data:/var/lib/mysql-persistent
- ${HOST_BACKUP_PATH}:/backups - ${HOST_BACKUP_PATH}:/backups
@@ -104,17 +110,41 @@ services:
- sh - sh
- -c - -c
- | - |
# Install curl for downloading db init script (handle different package managers) # Check if databases were already restored/imported
if [ -f "/var/lib/mysql-persistent/.restore-completed" ]; then
echo "✅ Databases already restored from backup - init not needed"
exit 0
fi
if mysql -h ${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e "
SELECT COUNT(*) FROM information_schema.tables
WHERE table_schema IN ('${DB_AUTH_NAME}', '${DB_WORLD_NAME}', '${DB_CHARACTERS_NAME}');" -s -N 2>/dev/null | grep -q -v '^0$'; then
echo "✅ Databases already populated - init not needed"
exit 0
fi
echo "🔧 Databases need initialization - proceeding with fresh setup"
# Install curl for downloading db init script
microdnf install -y curl || yum install -y curl || (apt-get update && apt-get install -y curl) microdnf install -y curl || yum install -y curl || (apt-get update && apt-get install -y curl)
# Download enhanced db init script from GitHub # Create fresh databases only
echo "📥 Downloading enhanced database initialization script from GitHub..." echo "🗄️ Creating fresh AzerothCore databases..."
curl -fsSL https://raw.githubusercontent.com/uprightbass360/acore-compose/main/scripts/db-init-enhanced.sh -o /tmp/db-init-enhanced.sh mysql -h ${MYSQL_HOST} -u${MYSQL_USER} -p${MYSQL_ROOT_PASSWORD} -e "
chmod +x /tmp/db-init-enhanced.sh CREATE DATABASE IF NOT EXISTS ${DB_AUTH_NAME} DEFAULT CHARACTER SET ${MYSQL_CHARACTER_SET} COLLATE ${MYSQL_COLLATION};
/tmp/db-init-enhanced.sh CREATE DATABASE IF NOT EXISTS ${DB_WORLD_NAME} DEFAULT CHARACTER SET ${MYSQL_CHARACTER_SET} COLLATE ${MYSQL_COLLATION};
CREATE DATABASE IF NOT EXISTS ${DB_CHARACTERS_NAME} DEFAULT CHARACTER SET ${MYSQL_CHARACTER_SET} COLLATE ${MYSQL_COLLATION};
SHOW DATABASES;" || {
echo "❌ Failed to create databases"
exit 1
}
# Set marker for import to proceed
echo "$(date): Fresh databases created - import needed" > /var/lib/mysql-persistent/.restore-failed
echo "✅ Fresh databases created!"
restart: "no" restart: "no"
# Step 4: Database import (one-time setup - run after db-init) # Step 4: Database import (one-time setup - attempts restore first)
ac-db-import: ac-db-import:
image: ${AC_DB_IMPORT_IMAGE} image: ${AC_DB_IMPORT_IMAGE}
container_name: ${CONTAINER_DB_IMPORT} container_name: ${CONTAINER_DB_IMPORT}
@@ -122,8 +152,6 @@ services:
depends_on: depends_on:
ac-mysql: ac-mysql:
condition: service_healthy condition: service_healthy
ac-db-init:
condition: service_completed_successfully
networks: networks:
- azerothcore - azerothcore
volumes: volumes:

View File

@@ -37,15 +37,112 @@ fi
echo "" echo ""
echo "🔧 Starting database import process..." echo "🔧 Starting database import process..."
# Wait for databases to be ready # First attempt backup restoration
echo "⏳ Waiting for databases to be accessible..." echo "🔍 Checking for backups to restore..."
for i in $(seq 1 120); do
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 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" echo "✅ All databases accessible"
break break
fi fi
echo "⏳ Waiting for databases... attempt $i/120" echo "⏳ Waiting for databases... attempt $i/10"
sleep 5 sleep 2
done done
# Verify databases are actually empty before importing # Verify databases are actually empty before importing