From ea31ce7efbebfab8323a625f6ab72688ff2b0315 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 19 Feb 2022 18:26:34 +0100 Subject: zfssnap: ... --- modules/zfssnap/zfssnap.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'modules/zfssnap/zfssnap.py') diff --git a/modules/zfssnap/zfssnap.py b/modules/zfssnap/zfssnap.py index 83d30a6b..4e7188f9 100644 --- a/modules/zfssnap/zfssnap.py +++ b/modules/zfssnap/zfssnap.py @@ -16,7 +16,7 @@ import logging import shlex -from collections import defaultdict, OrderedDict, deque +from collections import defaultdict, OrderedDict, deque, namedtuple import configparser from xdg import BaseDirectory @@ -56,6 +56,7 @@ def _get_items(): def prune(config, dry_run, keep_newest): items = defaultdict(list) + Snap = namedtuple(Snap, ['name', 'creation']) args = ['zfs', 'get', '-H', '-p', '-o', 'name,value', '-t', 'snapshot', 'creation'] _log_cmd(*args) with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: @@ -70,7 +71,7 @@ def prune(config, dry_run, keep_newest): if expected_name != name: # logger.debug(f'Skipping ‘{name}’ since it does not conform to naming scheme') continue - items[base_name].append({'name': name, 'creation': creation}) + items[base_name].append(Snap(name=name, creation=creation)) keep = set() kept_count = defaultdict(lambda: defaultdict(lambda: 0)) @@ -84,17 +85,17 @@ def prune(config, dry_run, keep_newest): within = config.gettimedelta('KEEP', 'within') if within > timedelta(seconds=0): for base, snaps in items.items(): - time_ref = max(snaps, key=lambda snap: snap['creation'], default=None) + time_ref = max(snaps, key=lambda snap: snap.creation, default=None) if not time_ref: logger.warn(f'Nothing to keep for ‘{base}’') continue logger.info(f'Using ‘{time_ref["name"]}’ as time reference for ‘{base}’') - within_cutoff = time_ref['creation'] - within + within_cutoff = time_ref.creation - within for snap in snaps: - if snap['creation'] >= within_cutoff: - keep_because(base, snap['name'], 'within') + if snap.creation >= within_cutoff: + keep_because(base, snap.name, 'within') else: logger.warn('Skipping rule ‘within’ since retention period is zero') @@ -117,8 +118,8 @@ def prune(config, dry_run, keep_newest): for base, snaps in items.items(): periods = OrderedDict() - for snap in sorted(snaps, key=lambda snap: snap['creation'], reverse=keep_newest): - period = pattern(snap['creation']) + for snap in sorted(snaps, key=lambda snap: snap.creation, reverse=keep_newest): + period = pattern(snap.creation) if period not in periods: periods[period] = deque() periods[period].append(snap) @@ -130,14 +131,14 @@ def prune(config, dry_run, keep_newest): break for snap in period_snaps: - keep_because(base, snap['name'], rule, period=period) + keep_because(base, snap.name, rule, period=period) to_keep -= 1 break if to_keep > 0: logger.debug(f'Missing {to_keep} to fulfill {rule}={desired_count} for ‘{base}’') - all_snaps = {snap['name'] for _, snaps in items.items() for snap in snaps} + all_snaps = {snap.name for _, snaps in items.items() for snap in snaps} to_destroy = all_snaps - keep if not to_destroy: logger.info('Nothing to prune') -- cgit v1.2.3