parse_backup_labels: read restore/backup hooks, filter by container

This commit is contained in:
Moritz 2024-09-17 17:36:53 +02:00
parent f7207cdf36
commit 45af6e8b5e

View File

@ -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