From 4ec3abc5a5e0c0f5a5790d9713201023b26d3bc7 Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Sat, 19 Feb 2022 15:24:38 +0100 Subject: vidhar: ... --- modules/zfssnap/zfssnap.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/modules/zfssnap/zfssnap.py b/modules/zfssnap/zfssnap.py index 7daa2935..57b0e95f 100644 --- a/modules/zfssnap/zfssnap.py +++ b/modules/zfssnap/zfssnap.py @@ -126,13 +126,13 @@ def prune(config, dry_run): 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} - to_delete = all_snaps - keep - if to_delete: - logger.info(f'Will prune: %s', ', '.join(map(lambda snap: f'‘{snap}’', to_delete))) + to_destroy = all_snaps - keep + if to_destroy: + logger.info(f'Will prune: %s', ', '.join(map(lambda snap: f'‘{snap}’', to_destroy))) else: logger.info('Nothing to prune') - for snap in to_delete: + for snap in to_destroy: args = ['zfs', 'destroy'] if dry_run: args += ['-n'] @@ -140,9 +140,10 @@ def prune(config, dry_run): _log_cmd(*args) subprocess.run(args, check=True) -def rename(snapshots, check=False): +def rename(snapshots, destroy=False): args = ['zfs', 'get', '-H', '-p', '-o', 'name,value', 'creation', *snapshots] _log_cmd(*args) + renamed_to = set() 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) @@ -155,11 +156,23 @@ def rename(snapshots, check=False): if new_name == name: logger.debug(f'Not renaming ‘{name}’ since name is already correct') continue - logger.info(f'Renaming ‘{name}’ to ‘{new_name}’') + if new_name in renamed_to: + if destroy: + logger.warning(f'Destroying ‘{name}’ since ‘{new_name}’ was already renamed to') + args = ['zfs', 'destroy', name] + _log_cmd(*args) + subprocess.run(args, check=True) + else: + logger.info(f'Skipping ‘{name}’ since ‘{new_name}’ was already renamed to') + + continue + + logger.info(f'Renaming ‘{name}’ to ‘{new_name}’') args = ['zfs', 'rename', name, new_name] _log_cmd(*args) - subprocess.run(args, check=check) + subprocess.run(args, check=True) + renamed_to.add(new_name) def autosnap(): items = _get_items() @@ -207,7 +220,7 @@ def autosnap(): _log_cmd(*args) subprocess.run(args, check=True) - rename(snapshots=all_snap_names, check=True) + rename(snapshots=all_snap_names) do_snapshot(*single) do_snapshot(*recursive, recursive=True) @@ -236,6 +249,7 @@ def main(): parser.set_defaults(cmd=autosnap) rename_parser = subparsers.add_parser('rename') rename_parser.add_argument('snapshots', nargs='+') + rename_parser.add_argument('--destroy', action='store_true', default=False) rename_parser.set_defaults(cmd=rename) prune_parser = subparsers.add_parser('prune') prune_parser.add_argument('--config', '-c', dest='config_files', nargs='*', default=list()) @@ -251,7 +265,7 @@ def main(): logger.setLevel(logging.DEBUG) cmdArgs = {} - for copy in {'snapshots', 'dry_run'}: + for copy in {'snapshots', 'dry_run', 'destroy'}: if copy in vars(args): cmdArgs[copy] = vars(args)[copy] if 'config_files' in vars(args): -- cgit v1.2.3