mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-25 07:14:32 +00:00
A finished generation cannot show whether a volume held a database, nor whether a dump was produced for it: under --only-sql a failed dump falls back to a file copy, and the resulting files/ tree looks like any other copy. The run knows both and threw the knowledge away as a printed warning, leaving every reader to guess from file names. Each generation now carries a manifest.json stating its layout and, per volume, database / dumped / engine. baudolo.generation is the single place those names are spelled; restore/paths.py, backup/db.py and backup/volume.py stop repeating them. It is deliberately import-free so a consumer can read the manifest with nothing but json, on hosts where this package is not installed. BREAKING CHANGE: BackupException is renamed BackupError. The rename is atomic across the ten modules that define or import it, three of which also carry the manifest change, so it lands in this commit rather than a separate one that could not import. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
94 lines
2.7 KiB
Python
94 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
import os
|
|
import pathlib
|
|
from dataclasses import dataclass, field
|
|
|
|
from baudolo.generation import FILES_DIR
|
|
|
|
from .shell import BackupError, execute_shell_command
|
|
|
|
|
|
@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(
|
|
["docker", "volume", "inspect", "--format", "{{json .}}", volume_name]
|
|
)[0]
|
|
data = json.loads(reported)
|
|
return Backing(
|
|
data.get("Mountpoint") or "",
|
|
data.get("Driver") or "",
|
|
data.get("Options") or {},
|
|
)
|
|
|
|
|
|
def get_last_backup_dir(
|
|
versions_dir: str, volume_name: str, current_backup_dir: str
|
|
) -> str | None:
|
|
versions = sorted(os.listdir(versions_dir), reverse=True)
|
|
for version in versions:
|
|
candidate = f"{pathlib.Path(versions_dir) / version / volume_name / FILES_DIR}/"
|
|
if candidate != current_backup_dir and pathlib.Path(candidate).is_dir():
|
|
return candidate
|
|
return None
|
|
|
|
|
|
def backup_volume(
|
|
versions_dir: str,
|
|
volume_name: str,
|
|
volume_dir: str,
|
|
*,
|
|
authoritative: bool,
|
|
source: str,
|
|
) -> 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.
|
|
source: directory to read from - the volume's mountpoint, or its path
|
|
inside a snapshot.
|
|
"""
|
|
dest = f"{pathlib.Path(volume_dir) / FILES_DIR}/"
|
|
pathlib.Path(dest).mkdir(parents=True, exist_ok=True)
|
|
|
|
last = get_last_backup_dir(versions_dir, volume_name, dest)
|
|
cmd = ["rsync", "-aP", "--no-D", "--delete", "--delete-excluded"]
|
|
if authoritative:
|
|
cmd.append("--checksum")
|
|
if last:
|
|
cmd.append(f"--link-dest={last}")
|
|
cmd += [source, dest]
|
|
|
|
try:
|
|
execute_shell_command(cmd)
|
|
except BackupError as e:
|
|
if "file has vanished" in str(e):
|
|
print(
|
|
"Warning: Some files vanished before transfer. Continuing.", flush=True
|
|
)
|
|
else:
|
|
raise
|