From 45af6e8b5e7b2e76f3d91fec34af25189b5aef57 Mon Sep 17 00:00:00 2001 From: Moritz Date: Tue, 17 Sep 2024 17:36:53 +0200 Subject: [PATCH] parse_backup_labels: read restore/backup hooks, filter by container --- backupbot.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/backupbot.py b/backupbot.py index 7725fef..a3fb89b 100755 --- a/backupbot.py +++ b/backupbot.py @@ -138,7 +138,7 @@ def restore(snapshot, target, noninteractive): logger.debug(result) -def parse_backup_labels(): +def parse_backup_labels(hook_type='backup', selected_container=[]): client = docker.from_env() container_by_service = { c.labels.get('com.docker.swarm.service.name'): c for c in client.containers.list()} @@ -148,7 +148,13 @@ def parse_backup_labels(): specs = s.attrs['Spec'] labels = specs['Labels'] stack_name = labels['com.docker.stack.namespace'] + container_name = s.name.removeprefix(f"{stack_name}_") settings = app_settings[stack_name] = app_settings.get(stack_name) or {} + if (backup := labels.get('backupbot.backup')) and bool(backup): + settings['enabled'] = True + if selected_container and container_name not in selected_container: + logger.debug(f"Skipping {s.name} because it's not a selected container") + continue if mounts:= specs['TaskTemplate']['ContainerSpec'].get('Mounts'): volumes = parse_volumes(stack_name, mounts) volumes.update(settings.get('volumes') or {}) @@ -156,14 +162,12 @@ def parse_backup_labels(): excluded_volumes, included_volume_paths = parse_excludes_includes(labels) settings['excluded_volumes'] = excluded_volumes.union(settings.get('excluded_volumes') or set()) settings['included_volume_paths'] = included_volume_paths.union(settings.get('included_volume_paths') or set()) - if (backup := labels.get('backupbot.backup')) and bool(backup): - settings['enabled'] = True if container := container_by_service.get(s.name): - if command := labels.get('backupbot.backup.pre-hook'): + if command := labels.get(f'backupbot.{hook_type}.pre-hook'): if not (pre_hooks:= settings.get('pre_hooks')): pre_hooks = settings['pre_hooks'] = {} pre_hooks[container] = command - if command := labels.get('backupbot.backup.post-hook'): + if command := labels.get(f'backupbot.{hook_type}.post-hook'): if not (post_hooks:= settings.get('post_hooks')): post_hooks = settings['post_hooks'] = {} post_hooks[container] = command