add: class Pool

This commit is contained in:
timeshifter
2021-11-25 16:03:34 +01:00
parent 3d24d3c5ee
commit da8a841ed6
2 changed files with 30 additions and 26 deletions
+1 -1
View File
@@ -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 = {
+24 -20
View File
@@ -17,14 +17,29 @@ Erstes Backup muss für alle notwendigen Datasets durchgeführt werden mit
"""
def find_backup_pool():
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 key in list(config.backup_pool.keys()):
uuid = config.backup_pool[key]
for pool, uuid in backup_pools.items():
if uuid in present_uuids:
print("Gefundene Platte: " + key)
return key
print("Gefundene Platte: " + pool)
return cls(pool)
else:
raise IOError("Konnte keine Backup-Platte finden!")
@@ -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.")