From 2f79812b847047f74fd8e98e9ecb8d42c1da645c Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 19 Feb 2022 22:45:22 +0100 Subject: zfssnap: use property for pruning --- modules/zfssnap/zfssnap.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'modules/zfssnap') diff --git a/modules/zfssnap/zfssnap.py b/modules/zfssnap/zfssnap.py index b72174fc..e77f11f3 100644 --- a/modules/zfssnap/zfssnap.py +++ b/modules/zfssnap/zfssnap.py @@ -32,7 +32,7 @@ def _now(): def _snap_name(item, time=_now()): suffix = re.sub(r'\+00:00$', r'Z', time.isoformat(timespec='seconds')) - return f'{item}@auto_{suffix}' + return f'{item}@{suffix}' def _log_cmd(*args): fmt_args = ' '.join(map(shlex.quote, args)) @@ -53,8 +53,18 @@ def _get_items(): return items def prune(config, dry_run, keep_newest): - items = defaultdict(list) + prunable_snapshots = set() + args = ['zfs', 'get', '-H', '-p', '-o', 'name,value', '-t', 'snapshot', '-s', 'local' 'li.yggdrasil:is-auto-snapshot'] + _log_cmd(*args) + with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: + text_stdout = io.TextIOWrapper(proc.stdout) + reader = csv.reader(text_stdout, delimiter='\t', quoting=csv.QUOTE_NONE) + Row = namedtuple('Row', ['name', 'is_auto_snapshot']) + for row in map(Row._make, reader): + if bool(strtobool(row.is_auto_snapshot)): + prunable_snapshots.add(row.name) + items = defaultdict(list) Snap = namedtuple('Snap', ['name', 'creation']) args = ['zfs', 'get', '-H', '-p', '-o', 'name,value', '-t', 'snapshot', 'creation'] _log_cmd(*args) @@ -63,12 +73,10 @@ def prune(config, dry_run, keep_newest): reader = csv.reader(text_stdout, delimiter='\t', quoting=csv.QUOTE_NONE) Row = namedtuple('Row', ['name', 'timestamp']) for row in map(Row._make, reader): - creation = datetime.fromtimestamp(int(row.timestamp), timezone.utc) - base_name, _, _ = row.name.rpartition('@') - expected_name = _snap_name(base_name, time=creation) - if expected_name != row.name: - # logger.debug(f'Skipping ‘{row.name}’ since it does not conform to naming scheme') + if row.name is not in prunable_snapshots: continue + + creation = datetime.fromtimestamp(int(row.timestamp), timezone.utc) items[base_name].append(Snap(name=row.name, creation=creation)) kept_count = defaultdict(lambda: defaultdict(lambda: 0)) -- cgit v1.2.3