From d615ff107ea2e1bc9eb22694474992a1e67b5dee Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 8 Mar 2024 15:23:15 -0800 Subject: [PATCH] wordpress: Fix backup, restore and uninstall when db is not running Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/wordpress/privileged.py | 27 ++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/plinth/modules/wordpress/privileged.py b/plinth/modules/wordpress/privileged.py index dcb191562..a358a9687 100644 --- a/plinth/modules/wordpress/privileged.py +++ b/plinth/modules/wordpress/privileged.py @@ -144,21 +144,23 @@ def is_public() -> bool: def dump_database(): """Dump database to file.""" _db_backup_file.parent.mkdir(parents=True, exist_ok=True) - with _db_backup_file.open('w', encoding='utf-8') as file_handle: - subprocess.run([ - 'mysqldump', '--add-drop-database', '--add-drop-table', - '--add-drop-trigger', '--user', 'root', '--databases', DB_NAME - ], stdout=file_handle, check=True) + with action_utils.service_ensure_running('mysql'): + with _db_backup_file.open('w', encoding='utf-8') as file_handle: + subprocess.run([ + 'mysqldump', '--add-drop-database', '--add-drop-table', + '--add-drop-trigger', '--user', 'root', '--databases', DB_NAME + ], stdout=file_handle, check=True) @privileged def restore_database(): """Restore database from file.""" - with _db_backup_file.open('r', encoding='utf-8') as file_handle: - subprocess.run(['mysql', '--user', 'root'], stdin=file_handle, - check=True) + with action_utils.service_ensure_running('mysql'): + with _db_backup_file.open('r', encoding='utf-8') as file_handle: + subprocess.run(['mysql', '--user', 'root'], stdin=file_handle, + check=True) - _set_privileges(DB_HOST, DB_NAME, DB_USER, _read_db_password()) + _set_privileges(DB_HOST, DB_NAME, DB_USER, _read_db_password()) def _read_db_password(): @@ -188,6 +190,7 @@ def uninstall(): def _drop_database(): """Drop the mysql database that was created during install.""" - query = f'''DROP DATABASE {DB_NAME};''' - subprocess.run(['mysql', '--user', 'root'], input=query.encode(), - check=True) + with action_utils.service_ensure_running('mysql'): + query = f'''DROP DATABASE {DB_NAME};''' + subprocess.run(['mysql', '--user', 'root'], input=query.encode(), + check=True)