diff --git a/backupbot.py b/backupbot.py index 22362d4..2a7476a 100755 --- a/backupbot.py +++ b/backupbot.py @@ -116,13 +116,14 @@ def parse_backup_labels(): specs = s.attrs['Spec'] labels = specs['Labels'] stack_name = labels['com.docker.stack.namespace'] - app_settings[stack_name] = settings = app_settings.get(stack_name) or {} + settings = app_settings[stack_name] = app_settings.get(stack_name) or {} if mounts:= specs['TaskTemplate']['ContainerSpec'].get('Mounts'): volumes = parse_volumes(stack_name, mounts) - excluded_volumes, included_paths = parse_excludes_includes(volumes, labels) + volumes.update(settings.get('volumes') or {}) + settings['volumes'] = volumes + excluded_volumes, included_volume_paths = parse_excludes_includes(labels) settings['excluded_volumes'] = excluded_volumes.union(settings.get('excluded_volumes') or set()) - settings['included_paths'] = included_paths.union(settings.get('included_paths') or set()) - settings['volume_paths'] = set(volumes.values()).union(settings.get('volume_paths') 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): @@ -174,25 +175,18 @@ def parse_volumes(stack_name, mounts): return volumes -def parse_excludes_includes(volumes, labels): - excluded_volumes_paths = set() - included_paths = set() +def parse_excludes_includes(labels): + excluded_volumes = set() + included_volume_paths = set() for label, value in labels.items(): if label.startswith('backupbot.backup.volumes.'): - volume_name = label.removeprefix('backupbot.backup.volumes.') - if not (volume_path:= volumes.get(volume_name)): - logger.error(f'Can not find volume with the name {volume_name}') - elif label.endswith('path'): - relative_paths = value.split(',') - for p in relative_paths: - absolute_path = Path(f"{volume_path}/{p}") - #TODO: if absolute_path.exists(): - included_paths.add(absolute_path) - excluded_volumes_paths.add(volume_path) + volume_name = label.removeprefix('backupbot.backup.volumes.').removesuffix('.path') + if label.endswith('path'): + relative_paths = tuple(value.split(',')) + included_volume_paths.add((volume_name, relative_paths)) elif bool(value): - excluded_volumes_paths.add(volume_path) - return excluded_volumes_paths, included_paths - + excluded_volumes.add(volume_name) + return excluded_volumes, included_volume_paths def get_backup_cmds(): client = docker.from_env()