Last active
January 4, 2024 20:09
-
-
Save ser/4857a2ea1e5d48a7b8d984a853bae8b1 to your computer and use it in GitHub Desktop.
de-partial-zrepl (work in progress but mostly works)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!/usr/bin/env python3 | |
import argparse | |
import re | |
import subprocess | |
import sys | |
#import time | |
from rich import print | |
from rich.progress import Progress | |
from rich.prompt import Confirm | |
parser = argparse.ArgumentParser(description='De-zrepl particular datasets only') | |
parser.add_argument("-d", "--dataset", help="zfs dataset", required=True) | |
parser.add_argument("-r", "--recursive", help="recursive", action="store_true") | |
parser.add_argument("-a", "--annihilate", help="destroy dataset after de-zrepl-ing", action="store_true") | |
args = parser.parse_args() | |
output = subprocess.run(["zrepl", "zfs-abstraction", "list"], capture_output=True, text=True) | |
full_list = output.stdout.splitlines() | |
#print(full_list) | |
regexp = f'^{args.dataset}{"$" if not args.recursive else ""}' | |
print("Dataset(s) fullfilling your criteria:") | |
datasets = [] | |
for zrepl_hold in full_list: | |
hold_dataset = zrepl_hold.split(" ")[-1] | |
hold_name = zrepl_hold.split(" ")[1] | |
hold_name = hold_name.replace('"', '') | |
if re.match(regexp, hold_dataset): | |
datasets.append((hold_dataset, hold_name)) | |
print(hold_dataset) | |
de_zrepl_indeed = Confirm.ask("Do you want to de-zrepl them?") | |
if de_zrepl_indeed: | |
with Progress() as progress: | |
do_dezrepl_task = progress.add_task("[red]De-zrepl-ing...", total=len(datasets)) | |
while not progress.finished: | |
for dataset in datasets: | |
commandline = ['zfs', 'release', dataset[1], dataset[0]] | |
output = subprocess.run(commandline, capture_output=True, text=True) | |
if output.returncode == 0: | |
progress.update(do_dezrepl_task, advance=1) | |
#print(commandline) | |
else: | |
sys.exit(f'Aborting! Problems with command {commandline}: {output.stderr}') | |
#time.sleep(1) | |
print("Dataset(s) de-zrepl-ed!") | |
else: | |
print("Aborted de-zrepl-ing on your request") | |
if args.annihilate: | |
zfs_destroy_indeed = Confirm.ask("Please confirm that you additionally really want to destroy them?") | |
if zfs_destroy_indeed: | |
print("Dataset(s) destroyed!") | |
else: | |
print("Dataset(s) NOT destroyed on your request") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment