wordpress: Fix backup, restore and uninstall when db is not running

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2024-03-08 15:23:15 -08:00 committed by James Valleroy
parent c341a531dd
commit d615ff107e
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -144,6 +144,7 @@ def is_public() -> bool:
def dump_database(): def dump_database():
"""Dump database to file.""" """Dump database to file."""
_db_backup_file.parent.mkdir(parents=True, exist_ok=True) _db_backup_file.parent.mkdir(parents=True, exist_ok=True)
with action_utils.service_ensure_running('mysql'):
with _db_backup_file.open('w', encoding='utf-8') as file_handle: with _db_backup_file.open('w', encoding='utf-8') as file_handle:
subprocess.run([ subprocess.run([
'mysqldump', '--add-drop-database', '--add-drop-table', 'mysqldump', '--add-drop-database', '--add-drop-table',
@ -154,6 +155,7 @@ def dump_database():
@privileged @privileged
def restore_database(): def restore_database():
"""Restore database from file.""" """Restore database from file."""
with action_utils.service_ensure_running('mysql'):
with _db_backup_file.open('r', encoding='utf-8') as file_handle: with _db_backup_file.open('r', encoding='utf-8') as file_handle:
subprocess.run(['mysql', '--user', 'root'], stdin=file_handle, subprocess.run(['mysql', '--user', 'root'], stdin=file_handle,
check=True) check=True)
@ -188,6 +190,7 @@ def uninstall():
def _drop_database(): def _drop_database():
"""Drop the mysql database that was created during install.""" """Drop the mysql database that was created during install."""
with action_utils.service_ensure_running('mysql'):
query = f'''DROP DATABASE {DB_NAME};''' query = f'''DROP DATABASE {DB_NAME};'''
subprocess.run(['mysql', '--user', 'root'], input=query.encode(), subprocess.run(['mysql', '--user', 'root'], input=query.encode(),
check=True) check=True)