Skip to content

Commit

Permalink
Allow backup script to be used with a simple path rather than strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Jan 18, 2024
1 parent 3788ae7 commit 5731d54
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions pydatalab/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down

0 comments on commit 5731d54

Please sign in to comment.