diff --git a/config.py b/config.py index 0920591..9a07c5b 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Name des lokalen Pools -local_pool = "tank" +local_pool_to_backup = "tank" # Name der Backup-Platten und UUID der Partition backup_pool = { diff --git a/zfs-backup.py b/zfs-backup.py index 5e72f29..2219a78 100755 --- a/zfs-backup.py +++ b/zfs-backup.py @@ -17,16 +17,31 @@ Erstes Backup muss für alle notwendigen Datasets durchgeführt werden mit """ -def find_backup_pool(): - print("Suche nach externer Festplatte für Backups") - present_uuids = get_output_of_command(["ls", "/dev/disk/by-uuid"]) - for key in list(config.backup_pool.keys()): - uuid = config.backup_pool[key] - if uuid in present_uuids: - print("Gefundene Platte: " + key) - return key - else: - raise IOError("Konnte keine Backup-Platte finden!") +def main(): + verify_running_as_root() + datasets = Dataset.from_local_pool() + backup_pool = Pool.from_dict(config.backup_pool) + import_pool(backup_pool) + try: + send_datasets_to_backup_pool(backup_pool, datasets) + finally: + export_pool(backup_pool) + + +class Pool: + def __init__(self, name): + self.name = name + + @classmethod + def from_dict(cls, backup_pools: dict[str, str]) -> Pool: + print("Suche nach externer Festplatte für Backups") + present_uuids = get_output_of_command(["ls", "/dev/disk/by-uuid"]) + for pool, uuid in backup_pools.items(): + if uuid in present_uuids: + print("Gefundene Platte: " + pool) + return cls(pool) + else: + raise IOError("Konnte keine Backup-Platte finden!") class Dataset: @@ -39,7 +54,7 @@ class Dataset: @classmethod def from_local_pool(cls) -> list[Dataset]: datasets_str = get_output_of_command( - [config.ZFS, "list", "-H", "-d", "1", "-o", "name", config.local_pool] + [config.ZFS, "list", "-H", "-d", "1", "-o", "name", config.local_pool_to_backup] ) # we omit the first return value, which is the pool, not the dataset datasets_list = datasets_str.split("\n")[1:] @@ -109,7 +124,7 @@ def import_pool(pool): def present_locally(snapshot): - local_snapshots = get_snapshots(config.local_pool) + local_snapshots = get_snapshots(config.local_pool_to_backup) for each_snapshot in local_snapshots: if snapshot in each_snapshot: return True @@ -195,7 +210,7 @@ def get_output_of_command(cmdline: str | list[str]): def zfs_destroy(full_name, recursive=False): - if config.local_pool in full_name: + if config.local_pool_to_backup in full_name: msg = "Wollte in falschem Pool löschen! Dies sollte niemals passieren! Abbruch!" raise Exception(msg) else: @@ -206,19 +221,8 @@ def zfs_destroy(full_name, recursive=False): get_output_of_command(cmdline) -def main(): - verify_running_as_root() - datasets = Dataset.from_local_pool() - backup_pool = find_backup_pool() - import_pool(backup_pool) - try: - send_datasets_to_backup_pool(backup_pool, datasets) - finally: - export_pool(backup_pool) - - def send_datasets_to_backup_pool(backup_pool, datasets): - last_local_snapshot = last_snapshot(config.local_pool) + last_local_snapshot = last_snapshot(config.local_pool_to_backup) if not last_local_snapshot: print("Es scheint keine Snapshots im Speicherpool zu geben") print("Breche ab.")