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):
|
def restore_backup(self, filename):
|
||||||
"""
|
"""
|
||||||
Restore database from a backup file
|
Restore database from a backup file using mariadb command
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename (str): Name of the backup file to restore
|
filename (str): Name of the backup file to restore
|
||||||
@@ -439,43 +439,51 @@ class DatabaseBackupManager:
|
|||||||
'message': 'Backup file not found'
|
'message': 'Backup file not found'
|
||||||
}
|
}
|
||||||
|
|
||||||
# Read SQL file and execute using Python mariadb library
|
# Use mariadb command to restore (more reliable than Python parsing)
|
||||||
import mariadb
|
# First, read the file and skip problematic first line if needed
|
||||||
|
|
||||||
with open(file_path, 'r') as f:
|
with open(file_path, 'r') as f:
|
||||||
sql_content = f.read()
|
sql_content = f.read()
|
||||||
|
|
||||||
# Connect to database
|
# Remove problematic MariaDB sandbox mode comment from first line
|
||||||
conn = mariadb.connect(
|
lines = sql_content.split('\n')
|
||||||
user=self.config['user'],
|
if lines and lines[0].startswith('/*M!999999'):
|
||||||
password=self.config['password'],
|
sql_content = '\n'.join(lines[1:])
|
||||||
host=self.config['host'],
|
|
||||||
port=int(self.config['port']),
|
# Build mariadb restore command
|
||||||
database=self.config['database']
|
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()
|
if result.returncode == 0:
|
||||||
|
return {
|
||||||
# Split SQL into statements and execute
|
'success': True,
|
||||||
statements = sql_content.split(';')
|
'message': f'Database restored successfully from {filename}'
|
||||||
executed = 0
|
}
|
||||||
|
else:
|
||||||
for statement in statements:
|
error_msg = result.stderr
|
||||||
statement = statement.strip()
|
print(f"Restore error: {error_msg}")
|
||||||
if statement:
|
return {
|
||||||
try:
|
'success': False,
|
||||||
cursor.execute(statement)
|
'message': f'Restore failed: {error_msg}'
|
||||||
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)'
|
|
||||||
}
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Exception during restore: {e}")
|
print(f"Exception during restore: {e}")
|
||||||
|
|||||||
@@ -680,14 +680,14 @@ def verify_database_setup():
|
|||||||
cursor.close()
|
cursor.close()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
# Check SQLite database
|
# SQLite check disabled - using MariaDB only
|
||||||
instance_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance'))
|
# instance_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../instance'))
|
||||||
sqlite_path = os.path.join(instance_folder, 'users.db')
|
# sqlite_path = os.path.join(instance_folder, 'users.db')
|
||||||
|
#
|
||||||
if os.path.exists(sqlite_path):
|
# if os.path.exists(sqlite_path):
|
||||||
print_success("SQLite database 'users.db' exists")
|
# print_success("SQLite database 'users.db' exists")
|
||||||
else:
|
# else:
|
||||||
print_error("SQLite database 'users.db' missing")
|
# print_error("SQLite database 'users.db' missing")
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -707,7 +707,7 @@ def main():
|
|||||||
create_warehouse_locations_table,
|
create_warehouse_locations_table,
|
||||||
create_permissions_tables,
|
create_permissions_tables,
|
||||||
create_users_table_mariadb,
|
create_users_table_mariadb,
|
||||||
create_sqlite_tables,
|
# create_sqlite_tables, # Disabled - using MariaDB only
|
||||||
create_database_triggers,
|
create_database_triggers,
|
||||||
populate_permissions_data,
|
populate_permissions_data,
|
||||||
update_external_config,
|
update_external_config,
|
||||||
@@ -731,7 +731,7 @@ def main():
|
|||||||
print(f"📅 Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
print(f"📅 Completed at: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
||||||
print("\n📋 Setup Summary:")
|
print("\n📋 Setup Summary:")
|
||||||
print(" • MariaDB tables created with triggers")
|
print(" • MariaDB tables created with triggers")
|
||||||
print(" • SQLite user database initialized")
|
print(" • MariaDB users table initialized")
|
||||||
print(" • Permissions system fully configured")
|
print(" • Permissions system fully configured")
|
||||||
print(" • Default superadmin user created (username: superadmin, password: superadmin123)")
|
print(" • Default superadmin user created (username: superadmin, password: superadmin123)")
|
||||||
print(" • Configuration files updated")
|
print(" • Configuration files updated")
|
||||||
|
|||||||
@@ -169,8 +169,13 @@ def get_db_connection():
|
|||||||
settings = {}
|
settings = {}
|
||||||
with open(settings_file, 'r') as f:
|
with open(settings_file, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
key, value = line.strip().split('=', 1)
|
line = line.strip()
|
||||||
settings[key] = value
|
# Skip empty lines and comments
|
||||||
|
if not line or line.startswith('#'):
|
||||||
|
continue
|
||||||
|
if '=' in line:
|
||||||
|
key, value = line.split('=', 1)
|
||||||
|
settings[key] = value
|
||||||
|
|
||||||
# Create a database connection
|
# Create a database connection
|
||||||
return mariadb.connect(
|
return mariadb.connect(
|
||||||
|
|||||||
@@ -214,8 +214,13 @@ def settings_handler():
|
|||||||
if os.path.exists(settings_file):
|
if os.path.exists(settings_file):
|
||||||
with open(settings_file, 'r') as f:
|
with open(settings_file, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
key, value = line.strip().split('=', 1)
|
line = line.strip()
|
||||||
external_settings[key] = value
|
# Skip empty lines and comments
|
||||||
|
if not line or line.startswith('#'):
|
||||||
|
continue
|
||||||
|
if '=' in line:
|
||||||
|
key, value = line.split('=', 1)
|
||||||
|
external_settings[key] = value
|
||||||
|
|
||||||
return render_template('settings.html', users=users, external_settings=external_settings, current_user={'role': session.get('role', '')})
|
return render_template('settings.html', users=users, external_settings=external_settings, current_user={'role': session.get('role', '')})
|
||||||
|
|
||||||
@@ -230,8 +235,13 @@ def get_external_db_connection():
|
|||||||
settings = {}
|
settings = {}
|
||||||
with open(settings_file, 'r') as f:
|
with open(settings_file, 'r') as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
key, value = line.strip().split('=', 1)
|
line = line.strip()
|
||||||
settings[key] = value
|
# Skip empty lines and comments
|
||||||
|
if not line or line.startswith('#'):
|
||||||
|
continue
|
||||||
|
if '=' in line:
|
||||||
|
key, value = line.split('=', 1)
|
||||||
|
settings[key] = value
|
||||||
|
|
||||||
# Create a database connection
|
# Create a database connection
|
||||||
return mariadb.connect(
|
return mariadb.connect(
|
||||||
|
|||||||
Reference in New Issue
Block a user