add: class Pool
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
# Name des lokalen Pools
|
# Name des lokalen Pools
|
||||||
local_pool = "tank"
|
local_pool_to_backup = "tank"
|
||||||
|
|
||||||
# Name der Backup-Platten und UUID der Partition
|
# Name der Backup-Platten und UUID der Partition
|
||||||
backup_pool = {
|
backup_pool = {
|
||||||
|
|||||||
+24
-20
@@ -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")
|
print("Suche nach externer Festplatte für Backups")
|
||||||
present_uuids = get_output_of_command(["ls", "/dev/disk/by-uuid"])
|
present_uuids = get_output_of_command(["ls", "/dev/disk/by-uuid"])
|
||||||
for key in list(config.backup_pool.keys()):
|
for pool, uuid in backup_pools.items():
|
||||||
uuid = config.backup_pool[key]
|
|
||||||
if uuid in present_uuids:
|
if uuid in present_uuids:
|
||||||
print("Gefundene Platte: " + key)
|
print("Gefundene Platte: " + pool)
|
||||||
return key
|
return cls(pool)
|
||||||
else:
|
else:
|
||||||
raise IOError("Konnte keine Backup-Platte finden!")
|
raise IOError("Konnte keine Backup-Platte finden!")
|
||||||
|
|
||||||
@@ -39,7 +54,7 @@ class Dataset:
|
|||||||
@classmethod
|
@classmethod
|
||||||
def from_local_pool(cls) -> list[Dataset]:
|
def from_local_pool(cls) -> list[Dataset]:
|
||||||
datasets_str = get_output_of_command(
|
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
|
# we omit the first return value, which is the pool, not the dataset
|
||||||
datasets_list = datasets_str.split("\n")[1:]
|
datasets_list = datasets_str.split("\n")[1:]
|
||||||
@@ -109,7 +124,7 @@ def import_pool(pool):
|
|||||||
|
|
||||||
|
|
||||||
def present_locally(snapshot):
|
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:
|
for each_snapshot in local_snapshots:
|
||||||
if snapshot in each_snapshot:
|
if snapshot in each_snapshot:
|
||||||
return True
|
return True
|
||||||
@@ -195,7 +210,7 @@ def get_output_of_command(cmdline: str | list[str]):
|
|||||||
|
|
||||||
|
|
||||||
def zfs_destroy(full_name, recursive=False):
|
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!"
|
msg = "Wollte in falschem Pool löschen! Dies sollte niemals passieren! Abbruch!"
|
||||||
raise Exception(msg)
|
raise Exception(msg)
|
||||||
else:
|
else:
|
||||||
@@ -206,19 +221,8 @@ def zfs_destroy(full_name, recursive=False):
|
|||||||
get_output_of_command(cmdline)
|
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):
|
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:
|
if not last_local_snapshot:
|
||||||
print("Es scheint keine Snapshots im Speicherpool zu geben")
|
print("Es scheint keine Snapshots im Speicherpool zu geben")
|
||||||
print("Breche ab.")
|
print("Breche ab.")
|
||||||
|
|||||||
Reference in New Issue
Block a user