mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2026-08-24 23:04:34 +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>
62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
"""Where a backup run puts its files, and how a finished run is stamped."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import pathlib
|
|
|
|
from dirval import create_stamp_file
|
|
|
|
from baudolo.generation import MANIFEST_FILE, manifest_document
|
|
|
|
from .shell import BackupError, execute_shell_command
|
|
|
|
|
|
def get_machine_id() -> str:
|
|
return execute_shell_command(["sha256sum", "/etc/machine-id"])[0][0:64]
|
|
|
|
|
|
def stamp_directory(version_dir: str) -> None:
|
|
"""
|
|
Use dirval as a Python library to stamp the directory (no CLI dependency).
|
|
"""
|
|
create_stamp_file(version_dir)
|
|
|
|
|
|
def create_version_directory(versions_dir: str, backup_time: str) -> str:
|
|
version_dir = str(pathlib.Path(versions_dir) / backup_time)
|
|
try:
|
|
pathlib.Path(version_dir).mkdir(parents=True)
|
|
except FileExistsError:
|
|
raise BackupError(
|
|
f"generation {backup_time} already exists at {version_dir}; "
|
|
"another run claimed this second - refusing to write into it, "
|
|
"since rsync --delete would overwrite that generation"
|
|
) from None
|
|
return version_dir
|
|
|
|
|
|
def create_volume_directory(version_dir: str, volume_name: str) -> str:
|
|
path = pathlib.Path(version_dir) / volume_name
|
|
path.mkdir(parents=True, exist_ok=True)
|
|
return str(path)
|
|
|
|
|
|
def write_manifest(version_dir: str, volumes: dict[str, dict[str, bool]]) -> str:
|
|
"""Record the generation's layout and per-volume outcome.
|
|
|
|
Written before the directory is stamped, so the stamp covers it.
|
|
|
|
Args:
|
|
version_dir: the generation directory.
|
|
volumes: per volume name, ``database`` and ``dumped``.
|
|
|
|
Returns:
|
|
The path written.
|
|
"""
|
|
path = pathlib.Path(version_dir) / MANIFEST_FILE
|
|
with path.open("w", encoding="utf-8") as handle:
|
|
json.dump(manifest_document(volumes), handle, indent=2, sort_keys=True)
|
|
handle.write("\n")
|
|
return str(path)
|