Files
backup-docker-to-local/src/baudolo/backup/layout.py
T
Kevin Veen-BirkenbachandClaude Opus 5.5 b3c22a1286
CI (make tests, stable, publish) / make test (push) Waiting to run
fix(backup): isolate a failed volume and fail the run at the end
f783f2f made has_tool() re-raise every probe failure but a missing
executable, and nothing above it caught the BackupError: one paused or
crash-looping container (docker exec exit 1) aborted the whole host
backup, so every later volume, the manifest and the stamp were lost.

main() now catches BackupError around a volume's probe and dump only:
the volume's directory is removed from the generation, so the stamp
never vouches for a partial dump and a later --link-dest never takes
it for the last good copy; the error and the volume go to stderr and
the run continues. Manifest and stamp cover the volumes that
succeeded, and main() returns 1 when any volume failed. Other exception
types still abort. The existing message for an already claimed
generation is reworded (MEDUSA SQL wording rule).

test_app_volume_failure.py runs main() over a real generation in a
temp dir: non-zero exit, the later volume still copied, no directory
for the failed volume at stamp time, the manifest listing only the good
volume, stderr naming volume and error, and a RuntimeError still
aborting. Against f783f2f five of the six tests fail. Operator decision
310c8a1f5201cbb1.

🎯 Fix: 92 %
💥 Break: 4 %
💸 Tokens: 100 %
⏱️ Velocity: 100 %
🛡️ Security: 100 %
🏆 Score: 92
🚦 Gate: unit suite 281 OK + negative control; ruff clean; MEDUSA 100

Created-with: https://s.veen.world/skills

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 15:12:55 +00:00

74 lines
2.3 KiB
Python

"""Where a backup run puts its files, and how a finished run is stamped."""
from __future__ import annotations
import json
import pathlib
import shutil
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 the mirroring copy would replace the files of 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 discard_volume_directory(volume_dir: str) -> None:
"""A volume whose backup failed must not sit inside the stamped generation,
where the stamp would vouch for a partial dump, and where a later run's
``--link-dest`` would pick it up as the last good copy.
Args:
volume_dir: the directory ``create_volume_directory`` returned.
"""
shutil.rmtree(volume_dir)
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)