Files
docker-volume-backup/tests/e2e/snapshot_db_driver.py
Kevin Veen-Birkenbach 1d86277a94 refactor(backup)!: run argv lists, never a shell
The backup built command strings and handed them to shell=True, so four interpolated values per dump - user, password, container, database - were each a way out of the command. validate_database covered one of them since the previous commit; now there is nothing to cover: every command is an argv list, and a value can only ever be an argument.

execute_to_file absorbs the atomic dump write. The shell redirect into <file>.tmp and the separate mv process become a Python file handle and os.replace, and a failing dump deletes its partial file instead of leaving it. PGPASSWORD moves out of the command string into the child's environment, where a process listing does not show it.

docker exec is built in one place, docker_exec_argv; db.py's three hand-built copies and the probe use it. The dead docker_volume_exists goes - never called, and the restore side owns the living twin. The rsync quoting in --link-dest falls away: inside an argv it would have become part of the path.

The snapshot module's injected runner changes type with it, which the three e2e drivers implement - the first conversion missed them, btrfs ran with no arguments, and the e2e caught it. Marked breaking for that contract: any external runner injected into volume_snapshot must now accept a list.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-17 16:30:25 +02:00

44 lines
1.2 KiB
Python

"""Copy a live database's volume out of a snapshot, using the real backup path.
Runs inside the privileged container built by test_e2e_snapshot_db.py, where a
database is mid-write on a btrfs subvolume. Exercises volume_snapshot and
backup_volume exactly as a backup run would.
"""
from __future__ import annotations
import subprocess
import sys
sys.path.insert(0, "/src")
from baudolo.backup.snapshot import SnapshotError, volume_snapshot
from baudolo.backup.volume import backup_volume
SUBJECT = "/subject/docker"
VOLUME = "mariadb_data"
DATADIR = f"{SUBJECT}/volumes/{VOLUME}/_data"
VERSIONS = "/backups"
GENERATION = f"{VERSIONS}/20260731"
def shell(command: list[str]) -> list[str]:
proc = subprocess.run(command, capture_output=True, text=True, check=False)
if proc.returncode != 0:
raise SnapshotError(
f"{' '.join(command)} exited {proc.returncode}: {proc.stderr.strip()}"
)
return proc.stdout.splitlines()
with volume_snapshot("btrfs", SUBJECT, "dbtest", run=shell) as resolve:
backup_volume(
VERSIONS,
VOLUME,
f"{GENERATION}/{VOLUME}",
authoritative=True,
source=resolve(f"{DATADIR}/"),
)
print("SNAPSHOT COPY DONE", flush=True)