feat(backup): BackupServerDriver, a role base for backup servers (#82)
driver / lint (pull_request) Successful in 15s
driver / standard (pull_request) Successful in 16s
driver / types (pull_request) Successful in 1m6s
driver / test (3.13) (pull_request) Successful in 1m7s
driver / test (3.12) (pull_request) Successful in 1m15s
driver / lowest (pull_request) Successful in 30s
driver / build (pull_request) Successful in 29s
driver / security (pull_request) Successful in 58s
CI / driver (pull_request) Successful in 2m22s
driver / publish (pull_request) Skipped

A backup server keeps datastores, one backup group per source with its
snapshots, and the jobs that keep them healthy: garbage collection,
verify, prune, sync and tape. None of that is a pool, a volume or a
replication job, so it is a role of its own instead of being bent into
StorageDriver. A server that manages its own disks lists StorageDriver
second.

The role declares the reads (datastores, groups with their newest
snapshot, a group's snapshots, jobs, remotes) and the actions (garbage
collection, running a job, verifying, protecting and deleting a
snapshot), which start a task and return its id for get_backup_task.
The shapes live in napalm_device_types.backups.

ClusterStorageDict gains an optional backup_server: which server,
datastore and namespace a hypervisor's backup storage writes to, with
the fingerprint it pins. Matching that to a server is the application's.
This commit is contained in:
christianmanivong committed 2026-11-02 20:46:40 +01:00
1 parent a0901e3277
commit 95e3aa1e26
12 files changed
+668 -3

No files matched your search

+31
View File
@@ -22,6 +22,7 @@ driver do X".
Role bases -- what a device *is*:
* :class:`~napalm_device_types.access_point.AccessPointDriver`
* :class:`~napalm_device_types.backup_server.BackupServerDriver`
* :class:`~napalm_device_types.switch.SwitchDriver`
* :class:`~napalm_device_types.firewall.FirewallDriver`
* :class:`~napalm_device_types.hypervisor.HypervisorDriver`
@@ -78,6 +79,22 @@ from napalm_device_types.apt_proxy import (
same_proxy_url,
)
from napalm_device_types.arp import ARP_AGE_UNKNOWN
from napalm_device_types.backup_server import BackupServerDriver
from napalm_device_types.backups import (
BACKUP_JOB_KINDS,
BackupDatastoreDict,
BackupGroupDict,
BackupJobKind,
BackupRemoteDict,
BackupRunState,
BackupServerJobDict,
BackupServerRefDict,
BackupSnapshotDict,
BackupSnapshotRefDict,
BackupTaskDict,
BackupTaskStatusDict,
BackupVerifyState,
)
from napalm_device_types.base import DeviceTypeDriver, FingerprintRule, PortSpec, rules_max_weight
from napalm_device_types.channel import (
ByteStream,
@@ -256,6 +273,19 @@ from napalm_device_types.vm_migration import MigrationPreflightDict
from napalm_device_types.vm_stop_request import VmStopRequestMixin
__all__ = [
"BACKUP_JOB_KINDS",
"BackupDatastoreDict",
"BackupGroupDict",
"BackupJobKind",
"BackupRemoteDict",
"BackupRunState",
"BackupServerJobDict",
"BackupServerRefDict",
"BackupSnapshotDict",
"BackupSnapshotRefDict",
"BackupTaskDict",
"BackupTaskStatusDict",
"BackupVerifyState",
"BackupDict",
"BackupJobDict",
"ClusterDict",
@@ -295,6 +325,7 @@ __all__ = [
"SnmpMemorySpec",
"SnmpValueSpec",
"AccessPointDriver",
"BackupServerDriver",
"ByteStream",
"CommandChannelMixin",
"CommandResult",
+238
View File
@@ -0,0 +1,238 @@
"""
Abstract base class for backup server drivers.
Usage::
from napalm_device_types import BackupServerDriver, StorageDriver
class ProxmoxBackupDriver(BackupServerDriver, StorageDriver):
def get_backup_datastores(self):
...
"""
from typing import TYPE_CHECKING
from napalm_device_types.base import DeviceTypeDriver
if TYPE_CHECKING: # pragma: no cover - for type checkers only
from napalm_device_types.backups import (
BackupDatastoreDict,
BackupGroupDict,
BackupJobKind,
BackupRemoteDict,
BackupServerJobDict,
BackupSnapshotDict,
BackupSnapshotRefDict,
BackupTaskDict,
BackupTaskStatusDict,
)
class BackupServerDriver(DeviceTypeDriver):
"""
Abstract intermediate driver for backup servers (e.g. Proxmox Backup
Server): the device hypervisors and hosts write their backups to.
A backup server keeps its backups in datastores, one group per source (a
VM, a container, a host) and one snapshot per backup run, and keeps them
healthy with scheduled jobs: garbage collection, verify, prune, sync with
other servers and tape. None of that is a storage pool, a volume or a
replication job, so it is not modelled through ``StorageDriver``. A server
that also manages its own disks and pools lists ``StorageDriver`` as a
second role, after this one.
The actions start a task on the server and return at once with its id;
:meth:`get_backup_task` follows it.
"""
#: Stable key netOrk exposes as ``device_class``. The order in which a
#: driver lists its role bases is the ranking; see
#: :func:`napalm_device_types.roles.primary_role_of`.
ROLE: str = "backup_server"
TYPE_LABEL: str = "Backup Server"
if TYPE_CHECKING:
# ------------------------------------------------------------------
# Read
# ------------------------------------------------------------------
def get_backup_datastores(self) -> list[BackupDatastoreDict]:
"""
Returns the server's datastores with their usage.
Example::
[
{
"name": "store1",
"path": "/mnt/datastore/store1",
"backend": "local",
"total": 4000787030016,
"used": 1529008357376,
"available": 2471778672640,
"estimated_full": 1798761600,
"maintenance": None,
"comment": None,
},
]
:raises Exception: when the server could not be read; never an
empty list for a failed read.
"""
...
def get_backup_groups(self, datastore: str = "") -> list[BackupGroupDict]:
"""
Returns the backup groups of every namespace of *datastore*, or of
every datastore when it is empty, each with its newest snapshot
only. :meth:`get_backup_snapshots` lists all of a group's.
Example::
[
{
"datastore": "store1",
"namespace": "",
"type": "vm",
"id": "101",
"owner": "backup@pbs",
"count": 14,
"newest": {
"time": 1793660400,
"size": 34359738368,
"verify": "ok",
"protected": False,
"comment": "web-01",
},
"oldest_time": 1792537200,
"comment": None,
},
]
:raises Exception: when the server could not be read.
"""
...
def get_backup_snapshots(
self, datastore: str, namespace: str, group_type: str, group_id: str
) -> list[BackupSnapshotDict]:
"""
Returns every snapshot of one group, newest first.
:param namespace: ``""`` for the datastore's root.
:raises ValueError: when the datastore or the group does not exist.
"""
...
def get_backup_jobs(self) -> list[BackupServerJobDict]:
"""
Returns the server's scheduled jobs of every kind, garbage
collection included (one per datastore, ``id`` = the datastore).
Example::
[
{
"id": "store1",
"kind": "gc",
"datastore": "store1",
"namespace": None,
"schedule": "daily",
"enabled": True,
"last_run": 1793664000,
"last_state": "ok",
"last_error": None,
"next_run": 1793750400,
"remote": None,
"direction": None,
"comment": None,
},
{
"id": "offsite",
"kind": "sync",
"datastore": "store1",
"namespace": "",
"schedule": "sat 03:00",
"enabled": True,
"last_run": 1793502000,
"last_state": "error",
"last_error": "connection refused",
"next_run": 1794106800,
"remote": "pbs-offsite",
"direction": "push",
"comment": None,
},
]
:raises Exception: when the server could not be read.
"""
...
def get_backup_remotes(self) -> list[BackupRemoteDict]:
"""
Returns the other backup servers this one syncs with, without their
secrets.
:raises Exception: when the server could not be read.
"""
...
# ------------------------------------------------------------------
# Actions
# ------------------------------------------------------------------
def start_garbage_collection(self, datastore: str) -> BackupTaskDict:
"""
Starts garbage collection on *datastore*.
:raises ValueError: when the datastore does not exist.
:raises RuntimeError: when the server refuses (e.g. one is running).
"""
...
def run_backup_job(self, kind: BackupJobKind, job_id: str) -> BackupTaskDict:
"""
Runs a scheduled job now, outside its schedule.
:param kind: a :data:`~napalm_device_types.backups.BackupJobKind`;
``"gc"`` runs the datastore named *job_id*'s garbage collection.
:raises ValueError: when there is no such job.
:raises RuntimeError: when the server refuses.
"""
...
def verify_backup_snapshot(self, snapshot: BackupSnapshotRefDict) -> BackupTaskDict:
"""
Verifies one snapshot now.
:raises ValueError: when the snapshot does not exist.
"""
...
def set_backup_snapshot_protected(
self, snapshot: BackupSnapshotRefDict, protected: bool
) -> None:
"""
Protects a snapshot from pruning and deletion, or lifts that.
:raises ValueError: when the snapshot does not exist.
"""
...
def delete_backup_snapshot(self, snapshot: BackupSnapshotRefDict) -> None:
"""
Deletes one snapshot. The data is gone once garbage collection has
run; there is no undo.
:raises ValueError: when the snapshot does not exist.
:raises RuntimeError: when it is protected.
"""
...
def get_backup_task(self, task: str) -> BackupTaskStatusDict:
"""
Returns the state and the log so far of a task an action started.
:raises ValueError: when the server does not know the task.
"""
...
+119
View File
@@ -0,0 +1,119 @@
"""The shapes of a backup server (``BackupServerDriver``).
Apart from ``models.py``, which is over the file size limit. A value the
platform does not report is None; it is never guessed.
"""
from typing import Literal, TypedDict, get_args
#: What a backup server's jobs do. Garbage collection is one of them: it has a
#: schedule and a last run like the others.
BackupJobKind = Literal["gc", "verify", "prune", "sync", "tape"]
BACKUP_JOB_KINDS: tuple[str, ...] = get_args(BackupJobKind)
#: How a job's or a task's run ended; "running" while it has not.
BackupRunState = Literal["ok", "warning", "error", "running"]
#: The outcome of the last verification of a snapshot.
BackupVerifyState = Literal["ok", "failed"]
class BackupDatastoreDict(TypedDict):
"""A datastore: where a backup server keeps its backups."""
name: str
path: str | None # local path; for an object-store backend the local cache, if any
backend: str # "local", "s3", "removable", ... as the platform names it
total: int | None # bytes
used: int | None # bytes
available: int | None # bytes
estimated_full: int | None # unix seconds the platform expects it to be full
maintenance: str | None # "read-only", "offline", ...; None when in service
comment: str | None
class BackupSnapshotDict(TypedDict):
"""One backup of a group: the state of its source at one time."""
time: int # unix seconds the backup was taken
size: int | None # bytes
verify: BackupVerifyState | None # None: never verified
protected: bool # exempt from pruning and deletion
comment: str | None
class BackupGroupDict(TypedDict):
"""The backups of one source (a VM, a container, a host) in one namespace."""
datastore: str
namespace: str # "" for the datastore's root
type: str # "vm", "ct", "host"
id: str # the guest's id ("101") or the host's name
owner: str | None # the account that writes the group's backups
count: int # snapshots in the group
newest: BackupSnapshotDict | None # None for a group without a snapshot
oldest_time: int | None # unix seconds of the oldest snapshot
comment: str | None
class BackupSnapshotRefDict(TypedDict):
"""Names one snapshot, for an action on it."""
datastore: str
namespace: str # "" for the datastore's root
group_type: str # BackupGroupDict.type
group_id: str # BackupGroupDict.id
time: int # BackupSnapshotDict.time
class BackupServerJobDict(TypedDict):
"""A scheduled job of a backup server."""
id: str # for "gc", the datastore's name
kind: BackupJobKind
datastore: str | None
namespace: str | None
schedule: str | None # as the platform writes it: "daily", "sat 02:00", "*:0/30"
enabled: bool
last_run: int | None # unix seconds the last run ended (or started, while running)
last_state: BackupRunState | None # None: never ran
last_error: str | None
next_run: int | None # unix seconds
remote: str | None # a sync job's BackupRemoteDict.name
direction: Literal["pull", "push"] | None # a sync job's
comment: str | None
class BackupRemoteDict(TypedDict):
"""Another backup server this one syncs with. Never its secret."""
name: str
host: str
port: int | None
fingerprint: str | None # of the remote's TLS certificate
auth_id: str | None # the account the sync logs in as
comment: str | None
class BackupTaskDict(TypedDict):
"""A task an action started on the server, to follow with ``get_backup_task``."""
task: str # the platform's task id
class BackupTaskStatusDict(TypedDict):
task: str
running: bool
state: BackupRunState | None # "running" while it runs
error: str | None
log: list[str] # the task's log lines so far
class BackupServerRefDict(TypedDict):
"""Which backup server and datastore a hypervisor's backup storage writes to."""
server: str # host name or address, as the hypervisor has it configured
port: int | None
datastore: str
namespace: str | None
fingerprint: str | None # of the server's TLS certificate, as the hypervisor pins it
+5
View File
@@ -5,6 +5,8 @@ Apart from ``models.py``, which is over the file size limit.
from typing import NotRequired, TypedDict
from napalm_device_types.backups import BackupServerRefDict
class ClusterNodeDict(TypedDict):
"""One host of a cluster, as its cluster sees it. A value the platform does not
@@ -34,6 +36,9 @@ class ClusterStorageDict(TypedDict):
nodes: list[str] # the nodes it is available on
total: int | None # bytes; None where the platform does not report it
used: int | None # bytes
# A backup storage that writes to a backup server: which one. Left out for every
# other storage; None where the platform does not say.
backup_server: NotRequired[BackupServerRefDict | None]
class ClusterNetworkDict(TypedDict):