mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
nextcloud: When backup fails, unset the maintenance mode
- Put dump operation in a try/finally block. - Create context manager to simplify. Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
0f663f8381
commit
7ab7d949af
@ -1,6 +1,7 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-or-later
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
"""Configure Nextcloud."""
|
"""Configure Nextcloud."""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
import secrets
|
import secrets
|
||||||
@ -316,20 +317,30 @@ def _set_maintenance_mode(on: bool):
|
|||||||
_run_occ('maintenance:mode', '--on' if on else '--off')
|
_run_occ('maintenance:mode', '--on' if on else '--off')
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def _maintenance_mode():
|
||||||
|
"""Context to set maintenance mode temporarily."""
|
||||||
|
try:
|
||||||
|
_set_maintenance_mode(True)
|
||||||
|
yield
|
||||||
|
finally:
|
||||||
|
_set_maintenance_mode(False)
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
def dump_database():
|
def dump_database():
|
||||||
"""Dump database to file."""
|
"""Dump database to file."""
|
||||||
_set_maintenance_mode(True)
|
|
||||||
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 _maintenance_mode():
|
||||||
subprocess.run([
|
with action_utils.service_ensure_running('mysql'):
|
||||||
'mysqldump', '--add-drop-database', '--add-drop-table',
|
with DB_BACKUP_FILE.open('w', encoding='utf-8') as file_handle:
|
||||||
'--add-drop-trigger', '--single-transaction',
|
subprocess.run([
|
||||||
'--default-character-set=utf8mb4', '--user', 'root',
|
'mysqldump', '--add-drop-database', '--add-drop-table',
|
||||||
'--databases', DB_NAME
|
'--add-drop-trigger', '--single-transaction',
|
||||||
], stdout=file_handle, check=True)
|
'--default-character-set=utf8mb4', '--user', 'root',
|
||||||
_set_maintenance_mode(False)
|
'--databases', DB_NAME
|
||||||
|
], stdout=file_handle, check=True)
|
||||||
|
|
||||||
|
|
||||||
@privileged
|
@privileged
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user