From b33362cb7ace8d41d696dbfce520a3a79fce76ea Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Tue, 5 Jan 2021 16:31:03 -0800 Subject: [PATCH] backups: Trigger schedules every hour - This will lead to backups only once a day or so. - In case of errors, backups are tried every hour. - Cleanups are also triggered but cleanups happen only after a successful backup. Tests performed: - In development mode, a new backup is taken after 3 minutes of enabling a repository's schedule. - Error in one repository does not effect the backup of other repositories. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/backups/__init__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/plinth/modules/backups/__init__.py b/plinth/modules/backups/__init__.py index b0e3cd37c..b7a098f0c 100644 --- a/plinth/modules/backups/__init__.py +++ b/plinth/modules/backups/__init__.py @@ -4,6 +4,7 @@ FreedomBox app to manage backup archives. """ import json +import logging import os import pathlib import re @@ -14,10 +15,12 @@ from django.utils.translation import ugettext_lazy as _ from plinth import actions from plinth import app as app_module -from plinth import cfg, menu +from plinth import cfg, glib, menu from . import api +logger = logging.getLogger(__name__) + version = 2 is_essential = True @@ -56,6 +59,11 @@ class BackupsApp(app_module.App): 'backups:index', parent_url_name='system') self.add(menu_item) + # Check every hour (every 3 minutes in debug mode) to perform scheduled + # backups. + interval = 180 if cfg.develop else 3600 + glib.schedule(interval, backup_by_schedule) + def setup(helper, old_version=None): """Install and configure the module.""" @@ -98,6 +106,17 @@ def _backup_handler(packet, encryption_passphrase=None): actions.superuser_run('backups', arguments, input=input_data.encode()) +def backup_by_schedule(data): + """Check if backups need to be taken and run the operation.""" + from . import repository as repository_module + for repository in repository_module.get_repositories(): + try: + repository.schedule.run_schedule() + except Exception as exception: + logger.exception('Error running scheduled backup: %s', exception) + # XXX: Create a notification + + def get_exported_archive_apps(path): """Get list of apps included in exported archive file.""" arguments = ['get-exported-archive-apps', '--path', path]