Add compatibility layer for Docker and Gunicorn deployments
- Auto-detect mysqldump vs mariadb-dump command - Conditional SSL flag based on Docker environment detection - Works in both Docker containers and standard systemd deployments - No breaking changes to existing functionality
This commit is contained in:
@@ -23,6 +23,35 @@ class DatabaseBackupManager:
|
||||
self.config = self._load_database_config()
|
||||
self.backup_path = self._get_backup_path()
|
||||
self._ensure_backup_directory()
|
||||
self.dump_command = self._detect_dump_command()
|
||||
|
||||
def _detect_dump_command(self):
|
||||
"""Detect which mysqldump command is available (mariadb-dump or mysqldump)"""
|
||||
try:
|
||||
# Try mariadb-dump first (newer MariaDB versions)
|
||||
result = subprocess.run(['which', 'mariadb-dump'],
|
||||
capture_output=True, text=True)
|
||||
if result.returncode == 0:
|
||||
return 'mariadb-dump'
|
||||
|
||||
# Fall back to mysqldump
|
||||
result = subprocess.run(['which', 'mysqldump'],
|
||||
capture_output=True, text=True)
|
||||
if result.returncode == 0:
|
||||
return 'mysqldump'
|
||||
|
||||
# Default to mariadb-dump (will error if not available)
|
||||
return 'mariadb-dump'
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not detect dump command: {e}")
|
||||
return 'mysqldump' # Default fallback
|
||||
|
||||
def _get_ssl_args(self):
|
||||
"""Get SSL arguments based on environment (Docker needs --skip-ssl)"""
|
||||
# Check if running in Docker container
|
||||
if os.path.exists('/.dockerenv') or os.environ.get('DOCKER_CONTAINER'):
|
||||
return ['--skip-ssl']
|
||||
return []
|
||||
|
||||
def _load_database_config(self):
|
||||
"""Load database configuration from external_server.conf"""
|
||||
@@ -104,12 +133,18 @@ class DatabaseBackupManager:
|
||||
# Build mysqldump command
|
||||
# Note: --skip-lock-tables and --force help with views that have permission issues
|
||||
cmd = [
|
||||
'mariadb-dump',
|
||||
self.dump_command,
|
||||
f"--host={self.config['host']}",
|
||||
f"--port={self.config['port']}",
|
||||
f"--user={self.config['user']}",
|
||||
f"--password={self.config['password']}",
|
||||
'--skip-ssl',
|
||||
]
|
||||
|
||||
# Add SSL args if needed (Docker environment)
|
||||
cmd.extend(self._get_ssl_args())
|
||||
|
||||
# Add backup options
|
||||
cmd.extend([
|
||||
'--single-transaction',
|
||||
'--skip-lock-tables',
|
||||
'--force',
|
||||
@@ -119,7 +154,7 @@ class DatabaseBackupManager:
|
||||
'--add-drop-database',
|
||||
'--databases',
|
||||
self.config['database']
|
||||
]
|
||||
])
|
||||
|
||||
# Execute mysqldump and save to file
|
||||
with open(backup_file, 'w') as f:
|
||||
@@ -316,11 +351,18 @@ class DatabaseBackupManager:
|
||||
# --complete-insert: Include column names in INSERT (more reliable)
|
||||
# --extended-insert: Use multi-row INSERT for efficiency
|
||||
cmd = [
|
||||
'mariadb-dump',
|
||||
self.dump_command,
|
||||
f"--host={self.config['host']}",
|
||||
f"--port={self.config['port']}",
|
||||
f"--user={self.config['user']}",
|
||||
f"--password={self.config['password']}",
|
||||
]
|
||||
|
||||
# Add SSL args if needed (Docker environment)
|
||||
cmd.extend(self._get_ssl_args())
|
||||
|
||||
# Add data-only backup options
|
||||
cmd.extend([
|
||||
'--no-create-info', # Skip table structure
|
||||
'--skip-triggers', # Skip triggers
|
||||
'--no-create-db', # Skip database creation
|
||||
@@ -329,7 +371,7 @@ class DatabaseBackupManager:
|
||||
'--single-transaction',
|
||||
'--skip-lock-tables',
|
||||
self.config['database']
|
||||
]
|
||||
])
|
||||
|
||||
# Execute mysqldump and save to file
|
||||
with open(backup_file, 'w') as f:
|
||||
|
||||
Reference in New Issue
Block a user