From 1a50f2a4d013d8cf12e85b591e216c93e81f2142 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 24 Feb 2022 13:50:22 +0100 Subject: Revert "zfssnap: no recursive" This reverts commit 4de6acd7756947819d5a0e56197c8372ac62f1b4. --- modules/zfssnap/zfssnap.py | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/modules/zfssnap/zfssnap.py b/modules/zfssnap/zfssnap.py index 360d7f25..6d9bc6de 100644 --- a/modules/zfssnap/zfssnap.py +++ b/modules/zfssnap/zfssnap.py @@ -25,6 +25,8 @@ from functools import cache from math import floor +import asyncio + @cache def _now(): @@ -212,8 +214,33 @@ def rename(snapshots, destroy=False, set_is_auto=False): def autosnap(): items = _get_items() + recursive, single = set(), set() + + for item_name, is_included in items.items(): + if not is_included: + continue + + children = {sub_name for sub_name in items if sub_name.startswith(f'{item_name}/')} + is_recursive = all([items[sub_name] for sub_name in children]) + if is_recursive and children: + recursive.add(item_name) + else: + single.add(item_name) + + for item_name in recursive | single: + is_covered = any([item_name.startswith(f'{super_name}/') for super_name in recursive]) + if is_covered: + try: + recursive.remove(item_name) + except KeyError: + pass + try: + single.remove(item_name) + except KeyError: + pass + all_snap_names = set() - def do_snapshot(*snap_items, recursive=False): + async def do_snapshot(*snap_items, recursive=False): nonlocal items, all_snap_names snap_names = {_snap_name(item) for item in snap_items} if recursive: @@ -230,10 +257,17 @@ def autosnap(): _log_cmd(*args) subprocess.run(args, check=True) - - do_snapshot(*items) - if not items: + tasks = [] + if single: + tasks.append(do_snapshot(*single)) + if recursive: + tasks.append(do_snapshot(*recursive, recursive=True)) + if not tasks: logger.warning('No snapshots to create') + else: + async def run_tasks(): + await asyncio.gather(*tasks) + asyncio.run(run_tasks()) for snap in all_snap_names: logger.info(f'Created ‘{snap}’') if all_snap_names: -- cgit v1.2.3