Files
AzerothCore-RealmMaster/V1/scripts/backup-hourly.sh
2025-10-17 01:40:50 -04:00

75 lines
2.7 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
# Configuration from environment variables
MYSQL_HOST=${MYSQL_HOST:-ac-mysql}
MYSQL_PORT=${MYSQL_PORT:-3306}
MYSQL_USER=${MYSQL_USER:-root}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-password}
BACKUP_DIR="/backups"
RETENTION_HOURS=${BACKUP_RETENTION_HOURS:-6}
DATE_FORMAT="%Y%m%d_%H%M%S"
# Database names from environment variables - core databases
DATABASES=("${DB_AUTH_NAME:-acore_auth}" "${DB_WORLD_NAME:-acore_world}" "${DB_CHARACTERS_NAME:-acore_characters}")
# Check if acore_playerbots database exists and add it to backup list
echo "Checking for optional acore_playerbots database..."
if mysql -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD -e "USE acore_playerbots;" 2>/dev/null; then
DATABASES+=("acore_playerbots")
echo "✅ acore_playerbots database found - will be included in backup"
else
echo " acore_playerbots database not found - skipping (this is normal for some installations)"
fi
# Create hourly backup directory
HOURLY_DIR="$BACKUP_DIR/hourly"
mkdir -p $HOURLY_DIR
# Generate timestamp
TIMESTAMP=$(date +$DATE_FORMAT)
BACKUP_SUBDIR="$HOURLY_DIR/$TIMESTAMP"
mkdir -p $BACKUP_SUBDIR
echo "[$TIMESTAMP] Starting AzerothCore hourly backup..."
echo "[$TIMESTAMP] Databases to backup: ${DATABASES[@]}"
# Backup each database
for db in "${DATABASES[@]}"; do
echo "[$TIMESTAMP] Backing up database: $db"
mysqldump -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD \
--single-transaction --routines --triggers --events \
--hex-blob --quick --lock-tables=false \
--add-drop-database --databases $db \
| gzip > $BACKUP_SUBDIR/${db}.sql.gz
if [ $? -eq 0 ]; then
SIZE=$(du -h $BACKUP_SUBDIR/${db}.sql.gz | cut -f1)
echo "[$TIMESTAMP] ✅ Successfully backed up $db ($SIZE)"
else
echo "[$TIMESTAMP] ❌ Failed to backup $db"
exit 1
fi
done
# Create backup manifest
cat > $BACKUP_SUBDIR/manifest.json <<EOF
{
"timestamp": "$TIMESTAMP",
"type": "hourly",
"databases": ["${DATABASES[@]}"],
"backup_size": "$(du -sh $BACKUP_SUBDIR | cut -f1)",
"retention_hours": $RETENTION_HOURS,
"mysql_version": "$(mysql -h$MYSQL_HOST -P$MYSQL_PORT -u$MYSQL_USER -p$MYSQL_PASSWORD -e 'SELECT VERSION();' -s -N)"
}
EOF
# Clean up old hourly backups (keep only last N hours)
echo "[$TIMESTAMP] Cleaning up hourly backups older than $RETENTION_HOURS hours..."
find $HOURLY_DIR -type d -name "[0-9]*" -mmin +$((RETENTION_HOURS * 60)) -exec rm -rf {} + 2>/dev/null || true
# Log backup completion
echo "[$TIMESTAMP] ✅ Hourly backup completed successfully"
echo "[$TIMESTAMP] Backup location: $BACKUP_SUBDIR"
echo "[$TIMESTAMP] Current hourly backups:"
ls -la $HOURLY_DIR/ | tail -n +2