2025-12-26 18:13:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-08-17 05:26:28 +02:00
|
|
|
import json
|
2025-12-26 18:13:26 +01:00
|
|
|
import os
|
|
|
|
|
import pathlib
|
2026-08-17 05:26:28 +02:00
|
|
|
from dataclasses import dataclass, field
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
from .shell import BackupException, execute_shell_command
|
|
|
|
|
|
|
|
|
|
|
2026-08-17 05:26:28 +02:00
|
|
|
@dataclass(frozen=True)
|
|
|
|
|
class Backing:
|
|
|
|
|
"""Where a docker volume actually keeps its data.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
mountpoint: the path the daemon reports.
|
|
|
|
|
driver: the volume driver, ``local`` for the built-in one.
|
|
|
|
|
options: the driver options; a non-empty map means the mountpoint is a
|
|
|
|
|
mount target rather than the storage itself.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
mountpoint: str
|
|
|
|
|
driver: str = "local"
|
|
|
|
|
options: dict = field(default_factory=dict)
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def source(self) -> str:
|
|
|
|
|
return f"{self.mountpoint}/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def inspect_backing(volume_name: str) -> Backing:
|
|
|
|
|
reported = execute_shell_command(
|
|
|
|
|
f"docker volume inspect --format '{{{{json .}}}}' {volume_name}"
|
2025-12-26 18:13:26 +01:00
|
|
|
)[0]
|
2026-08-17 05:26:28 +02:00
|
|
|
data = json.loads(reported)
|
|
|
|
|
return Backing(
|
|
|
|
|
data.get("Mountpoint") or "",
|
|
|
|
|
data.get("Driver") or "",
|
|
|
|
|
data.get("Options") or {},
|
|
|
|
|
)
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
|
2025-12-28 22:12:31 +01:00
|
|
|
def get_last_backup_dir(
|
|
|
|
|
versions_dir: str, volume_name: str, current_backup_dir: str
|
|
|
|
|
) -> str | None:
|
2025-12-26 18:13:26 +01:00
|
|
|
versions = sorted(os.listdir(versions_dir), reverse=True)
|
|
|
|
|
for version in versions:
|
|
|
|
|
candidate = os.path.join(versions_dir, version, volume_name, "files", "")
|
|
|
|
|
if candidate != current_backup_dir and os.path.isdir(candidate):
|
|
|
|
|
return candidate
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
2026-07-31 11:32:07 +02:00
|
|
|
def backup_volume(
|
2026-07-31 13:01:43 +02:00
|
|
|
versions_dir: str,
|
|
|
|
|
volume_name: str,
|
|
|
|
|
volume_dir: str,
|
|
|
|
|
*,
|
|
|
|
|
authoritative: bool,
|
|
|
|
|
source: str,
|
2026-07-31 11:32:07 +02:00
|
|
|
) -> None:
|
|
|
|
|
"""Perform incremental file backup of a Docker volume.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
authoritative: compare source and destination by content instead of by
|
|
|
|
|
size and whole-second mtime. Required on a pass whose destination was
|
|
|
|
|
already written from a live source, where a file can differ while
|
|
|
|
|
both attributes still agree.
|
2026-07-31 13:01:43 +02:00
|
|
|
source: directory to read from - the volume's mountpoint, or its path
|
|
|
|
|
inside a snapshot.
|
2026-07-31 11:32:07 +02:00
|
|
|
"""
|
2025-12-26 18:13:26 +01:00
|
|
|
dest = os.path.join(volume_dir, "files") + "/"
|
|
|
|
|
pathlib.Path(dest).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
|
last = get_last_backup_dir(versions_dir, volume_name, dest)
|
|
|
|
|
link_dest = f"--link-dest='{last}'" if last else ""
|
2026-07-31 11:32:07 +02:00
|
|
|
verify = "--checksum " if authoritative else ""
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-02 12:06:42 +02:00
|
|
|
cmd = (
|
2026-08-05 12:43:08 +02:00
|
|
|
f"rsync -aP --no-D --delete --delete-excluded "
|
2026-08-02 12:06:42 +02:00
|
|
|
f"{verify}{link_dest} {source} {dest}"
|
|
|
|
|
)
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
execute_shell_command(cmd)
|
|
|
|
|
except BackupException as e:
|
|
|
|
|
if "file has vanished" in str(e):
|
2025-12-28 22:12:31 +01:00
|
|
|
print(
|
|
|
|
|
"Warning: Some files vanished before transfer. Continuing.", flush=True
|
|
|
|
|
)
|
2025-12-26 18:13:26 +01:00
|
|
|
else:
|
|
|
|
|
raise
|