From 7a46f0d810e0e2f6d7af34b5ae32f30bd1f9def6 Mon Sep 17 00:00:00 2001 From: timeshifter Date: Thu, 25 Nov 2021 15:42:59 +0100 Subject: [PATCH] add: class Dataset --- zfs-backup.py | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/zfs-backup.py b/zfs-backup.py index 0503f7f..1355376 100755 --- a/zfs-backup.py +++ b/zfs-backup.py @@ -1,6 +1,8 @@ #!/usr/bin/python # -*- coding: utf-8 -*- +from __future__ import annotations + import os from subprocess import Popen, PIPE, check_output import config @@ -35,19 +37,27 @@ def find_backup_pool(): raise IOError("Konnte keine Backup-Platte finden!") -def get_datasets_to_backup(): - datasets_str = ( - Command([config.ZFS, "list", "-H", "-d", "1", "-o", "name", config.local_pool]) - .stdout.strip() - .decode() - ) - # we omit the first return value, which is the pool, not the dataset - datasets_list = datasets_str.split("\n")[1:] - to_backup = [d for d in datasets_list if d not in config.do_not_backup] - print("Folgende Datasets und alle darunter befindlichen Kinder werden gesichert:") - for tb in to_backup: - print(" " + tb) - return to_backup +class Dataset: + def __init__(self, name: str): + self.name = name + + def __repr__(self): + return self.name + + @classmethod + def from_local_pool(cls) -> list[Dataset]: + datasets_str = ( + Command([config.ZFS, "list", "-H", "-d", "1", "-o", "name", config.local_pool]) + .stdout.strip() + .decode() + ) + # we omit the first return value, which is the pool, not the dataset + datasets_list = datasets_str.split("\n")[1:] + to_backup = [Dataset(d) for d in datasets_list if d not in config.do_not_backup] + print("Folgende Datasets und alle darunter befindlichen Kinder werden gesichert:") + for item in to_backup: + print(" " + str(item)) + return to_backup def get_snapshots(pool): @@ -199,7 +209,7 @@ def zfs_destroy(full_name, recursive=False): def main(): verify_running_as_root() - datasets = get_datasets_to_backup() + datasets = Dataset.from_local_pool() backup_pool = find_backup_pool() import_pool(backup_pool) try: