Fix config file parsing and improve backup/restore functionality
- Fix external_server.conf parsing to skip comment lines and empty lines - Update routes.py get_db_connection() to handle comments - Update settings.py get_external_db_connection() to handle comments - Improve restore_backup() to use mariadb command instead of Python parsing - Remove SQLite database creation (MariaDB only) - Add environment detection for dump command (mariadb-dump vs mysqldump) - Add conditional SSL flag based on Docker environment - Fix database restore to handle MariaDB sandbox mode comments
This commit is contained in:
@@ -415,7 +415,7 @@ class DatabaseBackupManager:
|
||||
|
||||
def restore_backup(self, filename):
|
||||
"""
|
||||
Restore database from a backup file
|
||||
Restore database from a backup file using mariadb command
|
||||
|
||||
Args:
|
||||
filename (str): Name of the backup file to restore
|
||||
@@ -439,43 +439,51 @@ class DatabaseBackupManager:
|
||||
'message': 'Backup file not found'
|
||||
}
|
||||
|
||||
# Read SQL file and execute using Python mariadb library
|
||||
import mariadb
|
||||
|
||||
# Use mariadb command to restore (more reliable than Python parsing)
|
||||
# First, read the file and skip problematic first line if needed
|
||||
with open(file_path, 'r') as f:
|
||||
sql_content = f.read()
|
||||
|
||||
# Connect to database
|
||||
conn = mariadb.connect(
|
||||
user=self.config['user'],
|
||||
password=self.config['password'],
|
||||
host=self.config['host'],
|
||||
port=int(self.config['port']),
|
||||
database=self.config['database']
|
||||
# Remove problematic MariaDB sandbox mode comment from first line
|
||||
lines = sql_content.split('\n')
|
||||
if lines and lines[0].startswith('/*M!999999'):
|
||||
sql_content = '\n'.join(lines[1:])
|
||||
|
||||
# Build mariadb restore command
|
||||
cmd = [
|
||||
'mariadb',
|
||||
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 database name
|
||||
cmd.append(self.config['database'])
|
||||
|
||||
# Execute restore with stdin
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
input=sql_content,
|
||||
capture_output=True,
|
||||
text=True
|
||||
)
|
||||
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Split SQL into statements and execute
|
||||
statements = sql_content.split(';')
|
||||
executed = 0
|
||||
|
||||
for statement in statements:
|
||||
statement = statement.strip()
|
||||
if statement:
|
||||
try:
|
||||
cursor.execute(statement)
|
||||
executed += 1
|
||||
except Exception as stmt_error:
|
||||
print(f"Warning executing statement: {stmt_error}")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'message': f'Database restored successfully from {filename} ({executed} statements executed)'
|
||||
}
|
||||
if result.returncode == 0:
|
||||
return {
|
||||
'success': True,
|
||||
'message': f'Database restored successfully from {filename}'
|
||||
}
|
||||
else:
|
||||
error_msg = result.stderr
|
||||
print(f"Restore error: {error_msg}")
|
||||
return {
|
||||
'success': False,
|
||||
'message': f'Restore failed: {error_msg}'
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Exception during restore: {e}")
|
||||
|
||||
Reference in New Issue
Block a user