From ff434b65f40a9d06c43619e2225e6512c9ab6664 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 7 Apr 2024 11:22:16 -0700 Subject: [PATCH] nextcloud: Allow backup/restore when app is disabled - Also restart the container after restoring the database and its password. This seems to be required (perhaps to flush caches) for a successful database connection. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/nextcloud/__init__.py | 18 +++++++++-- plinth/modules/nextcloud/privileged.py | 42 ++++++++++++++------------ 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/plinth/modules/nextcloud/__init__.py b/plinth/modules/nextcloud/__init__.py index c967483e0..f21e3f1aa 100644 --- a/plinth/modules/nextcloud/__init__.py +++ b/plinth/modules/nextcloud/__init__.py @@ -151,9 +151,23 @@ class NextcloudBackupRestore(BackupRestore): def backup_pre(self, packet): """Save database contents.""" super().backup_pre(packet) - privileged.dump_database() + self.app.get_component('dropin-configs-nextcloud').enable() + mysql = self.app.get_component('shared-daemon-nextcloud-mysql') + redis = self.app.get_component('shared-daemon-nextcloud-redis') + container = self.app.get_component('daemon-nextcloud') + with mysql.ensure_running(): + with redis.ensure_running(): + with container.ensure_running(): + privileged.dump_database() def restore_post(self, packet): """Restore database contents.""" super().restore_post(packet) - privileged.restore_database() + self.app.get_component('dropin-configs-nextcloud').enable() + mysql = self.app.get_component('shared-daemon-nextcloud-mysql') + redis = self.app.get_component('shared-daemon-nextcloud-redis') + container = self.app.get_component('daemon-nextcloud') + with mysql.ensure_running(): + with redis.ensure_running(): + with container.ensure_running(): + privileged.restore_database() diff --git a/plinth/modules/nextcloud/privileged.py b/plinth/modules/nextcloud/privileged.py index ec2d1c54b..2021374bc 100644 --- a/plinth/modules/nextcloud/privileged.py +++ b/plinth/modules/nextcloud/privileged.py @@ -19,6 +19,7 @@ NETWORK_NAME = 'nextcloud-fbx' BRIDGE_IP = '172.16.16.1' CONTAINER_IP = '172.16.16.2' CONTAINER_NAME = 'nextcloud-freedombox' +SERVICE_NAME = 'nextcloud-freedombox' VOLUME_NAME = 'nextcloud-volume-freedombox' IMAGE_NAME = 'docker.io/library/nextcloud:stable-apache' @@ -333,36 +334,39 @@ def dump_database(): DB_BACKUP_FILE.parent.mkdir(parents=True, exist_ok=True) 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) + 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 def restore_database(): """Restore database from file.""" - 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) + with DB_BACKUP_FILE.open('r', encoding='utf-8') as file_handle: + subprocess.run(['mysql', '--user', 'root'], stdin=file_handle, + check=True) - _set_database_privileges(_get_dbpassword()) + subprocess.run(['redis-cli', '-n', + str(REDIS_DB), 'FLUSHDB', 'SYNC'], check=False) + + _set_database_privileges(_get_dbpassword()) + + # After updating the configuration, a restart seems to be required for the + # new DB password be used. + action_utils.service_try_restart(SERVICE_NAME) - subprocess.run(['redis-cli', '-n', REDIS_DB, 'FLUSHDB', 'SYNC'], - check=False) _set_maintenance_mode(False) - # Attempts to update UUIDs of user and group entries. By default, - # the command attempts to update UUIDs that have been invalidated by - # a migration step. + # Attempts to update UUIDs of user and group entries. By default, the + # command attempts to update UUIDs that have been invalidated by a + # migration step. _run_occ('ldap:update-uuid') - # Update the systems data-fingerprint after a backup is restored + # Update the systems data-fingerprint after a backup is restored. _run_occ('maintenance:data-fingerprint')