From 5731d5443c6d3a5552506d63c73c5ea1d0a0dff6 Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Thu, 18 Jan 2024 15:28:59 +0000 Subject: [PATCH] Allow backup script to be used with a simple path rather than strategy --- pydatalab/tasks.py | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/pydatalab/tasks.py b/pydatalab/tasks.py index 38f24ce07..89204166c 100644 --- a/pydatalab/tasks.py +++ b/pydatalab/tasks.py @@ -417,10 +417,20 @@ def generate_random_startingmaterial_id(): @task -def create_backup(_, strategy_name: str): - """Create a backup given the strategy name in the config. +def create_backup( + _, strategy_name: str | None = None, output_path: pathlib.Path | str | None = None +): + """Create a backup given the strategy name in the config or a local output path. + + If a strategy is not provided, this task will simply write a backup file + to the chosen path with the filename derived from the current time. + No retention policy will be applied, and remote backups are not possible + via this route. + + If a strategy is provided, this task will create a backup file following all + the configured settings, including retention and remote storage. - This task can be added as a cronjob on the server, ideally using + This task could be added as a cronjob on the server, ideally using the frequency specified in the strategy to avoid confusion. Example usage in cron: @@ -434,17 +444,27 @@ def create_backup(_, strategy_name: str): ``` """ - from pydatalab.backups import create_backup + from pydatalab.backups import create_backup, take_snapshot from pydatalab.config import CONFIG - if not CONFIG.BACKUP_STRATEGIES: - raise SystemExit("No backup strategies configured.") + if output_path and strategy_name: + raise SystemExit("Cannot specify both an output path and a strategy name.") + + if output_path is not None: + return take_snapshot(pathlib.Path(output_path)) + + if strategy_name is not None: + if not CONFIG.BACKUP_STRATEGIES: + raise SystemExit("No backup strategies configured and output path not provided.") + + strategy = CONFIG.BACKUP_STRATEGIES.get(strategy_name) + if strategy is None: + raise SystemExit(f"Backup strategy {strategy_name!r} not found in config.") - strategy = CONFIG.BACKUP_STRATEGIES.get(strategy_name) - if strategy is None: - raise SystemExit(f"Backup strategy {strategy_name!r} not found in config.") + else: + raise SystemExit("No backup strategy or output path provided.") - create_backup(strategy) + return create_backup(strategy) admin.add_task(create_backup)