updated aplications database
This commit is contained in:
@@ -439,16 +439,36 @@ class DatabaseBackupManager:
|
||||
'message': 'Backup file not found'
|
||||
}
|
||||
|
||||
# Use mariadb command to restore (more reliable than Python parsing)
|
||||
# First, read the file and skip problematic first line if needed
|
||||
# Read the SQL file
|
||||
with open(file_path, 'r') as f:
|
||||
sql_content = f.read()
|
||||
|
||||
# Clean up SQL content to avoid DEFINER issues
|
||||
import re
|
||||
|
||||
# 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:])
|
||||
|
||||
# Remove DEFINER clauses that cause permission errors
|
||||
# This regex matches DEFINER=`user`@`host` in various contexts
|
||||
sql_content = re.sub(
|
||||
r'/\*!50017 DEFINER=`[^`]+`@`[^`]+`\*/',
|
||||
'',
|
||||
sql_content
|
||||
)
|
||||
sql_content = re.sub(
|
||||
r'/\*!50013 DEFINER=`[^`]+`@`[^`]+`\*/',
|
||||
'',
|
||||
sql_content
|
||||
)
|
||||
sql_content = re.sub(
|
||||
r'DEFINER=`[^`]+`@`[^`]+`',
|
||||
'',
|
||||
sql_content
|
||||
)
|
||||
|
||||
# Build mariadb restore command
|
||||
cmd = [
|
||||
'mariadb',
|
||||
@@ -520,6 +540,28 @@ class DatabaseBackupManager:
|
||||
'message': 'Backup file not found'
|
||||
}
|
||||
|
||||
# Read SQL content
|
||||
with open(file_path, 'r') as f:
|
||||
sql_content = f.read()
|
||||
|
||||
# Clean up SQL content to avoid DEFINER issues (in case data-only backup has them)
|
||||
import re
|
||||
sql_content = re.sub(
|
||||
r'/\*!50017 DEFINER=`[^`]+`@`[^`]+`\*/',
|
||||
'',
|
||||
sql_content
|
||||
)
|
||||
sql_content = re.sub(
|
||||
r'/\*!50013 DEFINER=`[^`]+`@`[^`]+`\*/',
|
||||
'',
|
||||
sql_content
|
||||
)
|
||||
sql_content = re.sub(
|
||||
r'DEFINER=`[^`]+`@`[^`]+`',
|
||||
'',
|
||||
sql_content
|
||||
)
|
||||
|
||||
# First, disable foreign key checks and truncate all tables
|
||||
# This ensures clean data import without constraint violations
|
||||
try:
|
||||
@@ -557,10 +599,7 @@ class DatabaseBackupManager:
|
||||
print(f"Warning during table truncation: {e}")
|
||||
# Continue anyway - the restore might still work
|
||||
|
||||
# Read and execute SQL file using Python mariadb library
|
||||
with open(file_path, 'r') as f:
|
||||
sql_content = f.read()
|
||||
|
||||
# Execute SQL using Python mariadb library
|
||||
conn = mariadb.connect(
|
||||
user=self.config['user'],
|
||||
password=self.config['password'],
|
||||
@@ -583,8 +622,8 @@ class DatabaseBackupManager:
|
||||
print(f"Warning executing statement: {stmt_error}")
|
||||
|
||||
conn.commit()
|
||||
result_success = True
|
||||
result_returncode = 0
|
||||
cursor.close()
|
||||
conn.close()
|
||||
|
||||
# Re-enable foreign key checks
|
||||
try:
|
||||
@@ -603,16 +642,10 @@ class DatabaseBackupManager:
|
||||
except Exception as e:
|
||||
print(f"Warning: Could not re-enable foreign key checks: {e}")
|
||||
|
||||
if result_success:
|
||||
return {
|
||||
'success': True,
|
||||
'message': f'Data restored successfully from {filename}'
|
||||
}
|
||||
else:
|
||||
return {
|
||||
'success': False,
|
||||
'message': f'Data restore failed'
|
||||
}
|
||||
return {
|
||||
'success': True,
|
||||
'message': f'Data restored successfully from {filename} ({executed} statements executed)'
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(f"Exception during data restore: {e}")
|
||||
|
||||
Reference in New Issue
Block a user