Files
coulobre/src/coulobre/offsite_backup.py
T
parkel ecded4f123
Coulobre Panel CI / Checks (Python 3.12) (pull_request) Successful in 1m46s
Coulobre Panel CI / Checks (Python 3.14) (pull_request) Successful in 1m58s
Use local off-site destinations for manual backups and recovery
2026-12-03 21:14:02 +01:00

146 lines
4.4 KiB
Python

import re
from dataclasses import dataclass
from uuid import UUID
OFFSITE_SOCKET = "/run/coulobre-account-offsite.sock"
TRANSFER_TIMEOUT_SECONDS = 30 * 60
MAX_DESTINATION_LENGTH = 48
DESTINATION_ID = re.compile(r"[a-z][a-z0-9-]{0,47}")
HEX_DIGEST = re.compile(r"[0-9a-f]{64}")
def validate_destination(destination):
if not isinstance(destination, str) or not DESTINATION_ID.fullmatch(
destination
):
raise ValueError("Choose a configured off-site backup destination.")
def destination_labels(value):
if not isinstance(value, dict) or len(value) > 20:
raise ValueError("Configure an object with up to 20 destinations.")
for destination, label in value.items():
validate_destination(destination)
if (
not isinstance(label, str)
or not label.strip()
or not label.isprintable()
or len(label) > 80
):
raise ValueError(
"Each destination needs a label of up to 80 characters."
)
return value
@dataclass(frozen=True)
class OffsiteBackupRequest:
account_id: int
backup_id: UUID
destination: str
def __post_init__(self):
if (
type(self.account_id) is not int
or not 1 <= self.account_id < 2**63
):
raise ValueError("Expected a valid backup account ID.")
if not isinstance(self.backup_id, UUID):
raise ValueError("Expected a backup UUID.")
validate_destination(self.destination)
@classmethod
def from_manifest(cls, data):
if (
not isinstance(data, dict)
or data.keys()
!= {"version", "account_id", "backup_id", "destination"}
or type(data["version"]) is not int
or data["version"] != 1
or not isinstance(data["backup_id"], str)
):
raise ValueError("Expected a version 1 off-site backup request.")
backup_id = UUID(data["backup_id"])
if str(backup_id) != data["backup_id"]:
raise ValueError("Expected a canonical backup ID.")
return cls(data["account_id"], backup_id, data["destination"])
def manifest(self):
return {
"version": 1,
"account_id": self.account_id,
"backup_id": str(self.backup_id),
"destination": self.destination,
}
@property
def tag(self):
return f"coulobre-{self.backup_id}"
@property
def archive_name(self):
return f"account-{self.account_id}-{self.backup_id}.zip"
@dataclass(frozen=True)
class OffsiteRecoveryRequest:
source: OffsiteBackupRequest
recovered_id: UUID
snapshot: str
sha256: str
size: int | None
def __post_init__(self):
if (
not isinstance(self.recovered_id, UUID)
or self.recovered_id == self.source.backup_id
or not all(
isinstance(value, str) and HEX_DIGEST.fullmatch(value)
for value in (self.snapshot, self.sha256)
)
or (
self.size is not None
and (type(self.size) is not int or not 1 <= self.size < 2**63)
)
):
raise ValueError("Expected the original off-site verification.")
@classmethod
def from_manifest(cls, data):
if (
not isinstance(data, dict)
or data.keys()
!= {
"version",
"source",
"recovered_id",
"snapshot",
"sha256",
"size",
}
or type(data["version"]) is not int
or data["version"] != 1
or not isinstance(data["recovered_id"], str)
):
raise ValueError("Expected a version 1 off-site recovery request.")
recovery_id = UUID(data["recovered_id"])
if str(recovery_id) != data["recovered_id"]:
raise ValueError("Expected a canonical recovery ID.")
return cls(
OffsiteBackupRequest.from_manifest(data["source"]),
recovery_id,
data["snapshot"],
data["sha256"],
data["size"],
)
def manifest(self):
return {
"version": 1,
"source": self.source.manifest(),
"recovered_id": str(self.recovered_id),
"snapshot": self.snapshot,
"sha256": self.sha256,
"size": self.size,
}