mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +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
|
||||
"""Configure Nextcloud."""
|
||||
|
||||
import contextlib
|
||||
import json
|
||||
import pathlib
|
||||
import secrets
|
||||
@ -316,20 +317,30 @@ def _set_maintenance_mode(on: bool):
|
||||
_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
|
||||
def dump_database():
|
||||
"""Dump database to file."""
|
||||
_set_maintenance_mode(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:
|
||||
subprocess.run([
|
||||
'mysqldump', '--add-drop-database', '--add-drop-table',
|
||||
'--add-drop-trigger', '--single-transaction',
|
||||
'--default-character-set=utf8mb4', '--user', 'root',
|
||||
'--databases', DB_NAME
|
||||
], stdout=file_handle, check=True)
|
||||
_set_maintenance_mode(False)
|
||||
|
||||
with _maintenance_mode():
|
||||
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', '--single-transaction',
|
||||
'--default-character-set=utf8mb4', '--user', 'root',
|
||||
'--databases', DB_NAME
|
||||
], stdout=file_handle, check=True)
|
||||
|
||||
|
||||
@privileged
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user