mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2025-12-29 03:42:08 +00:00
refactor: migrate to src/ package + add DinD-based E2E runner with debug artifacts
- Replace legacy standalone scripts with a proper src-layout Python package (baudolo backup/restore/configure entrypoints via pyproject.toml) - Remove old scripts/files (backup-docker-to-local.py, recover-docker-from-local.sh, databases.csv.tpl, Todo.md) - Add Dockerfile to build the project image for local/E2E usage - Update Makefile: build image and run E2E via external runner script - Add scripts/test-e2e.sh: - start DinD + dedicated network - recreate DinD data volume (and shared /tmp volume) - pre-pull helper images (alpine-rsync, alpine) - load local baudolo:local image into DinD - run unittest E2E suite inside DinD and abort on first failure - on failure: dump host+DinD diagnostics and archive shared /tmp into artifacts/ - Add artifacts/ debug outputs produced by failing E2E runs (logs, events, tmp archive) https://chatgpt.com/share/694ec23f-0794-800f-9a59-8365bc80f435
This commit is contained in:
83
src/baudolo/restore/db/mariadb.py
Normal file
83
src/baudolo/restore/db/mariadb.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import sys
|
||||
|
||||
from ..run import docker_exec, docker_exec_sh
|
||||
|
||||
|
||||
def _pick_client(container: str) -> str:
|
||||
# Prefer mariadb, fallback to mysql (many images provide one or both).
|
||||
script = r"""
|
||||
set -eu
|
||||
if command -v mariadb >/dev/null 2>&1; then echo mariadb; exit 0; fi
|
||||
if command -v mysql >/dev/null 2>&1; then echo mysql; exit 0; fi
|
||||
exit 42
|
||||
"""
|
||||
try:
|
||||
out = docker_exec_sh(container, script, capture=True).stdout.decode().strip()
|
||||
if not out:
|
||||
raise RuntimeError("empty client detection output")
|
||||
return out
|
||||
except Exception as e:
|
||||
print("ERROR: neither 'mariadb' nor 'mysql' found in container.", file=sys.stderr)
|
||||
raise e
|
||||
|
||||
|
||||
def restore_mariadb_sql(
|
||||
*,
|
||||
container: str,
|
||||
db_name: str,
|
||||
user: str,
|
||||
password: str,
|
||||
sql_path: str,
|
||||
empty: bool,
|
||||
) -> None:
|
||||
client = _pick_client(container)
|
||||
|
||||
if not os.path.isfile(sql_path):
|
||||
raise FileNotFoundError(sql_path)
|
||||
|
||||
if empty:
|
||||
# Close to old behavior: list tables, drop individually, then re-enable checks.
|
||||
docker_exec(
|
||||
container,
|
||||
["mysql", "-u", user, f"--password={password}", "-e", "SET FOREIGN_KEY_CHECKS=0;"],
|
||||
)
|
||||
|
||||
result = docker_exec(
|
||||
container,
|
||||
[
|
||||
"mysql",
|
||||
"-u",
|
||||
user,
|
||||
f"--password={password}",
|
||||
"-N",
|
||||
"-e",
|
||||
f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{db_name}';",
|
||||
],
|
||||
capture=True,
|
||||
)
|
||||
tables = result.stdout.decode().split()
|
||||
for tbl in tables:
|
||||
docker_exec(
|
||||
container,
|
||||
[
|
||||
"mysql",
|
||||
"-u",
|
||||
user,
|
||||
f"--password={password}",
|
||||
"-e",
|
||||
f"DROP TABLE IF EXISTS `{db_name}`.`{tbl}`;",
|
||||
],
|
||||
)
|
||||
|
||||
docker_exec(
|
||||
container,
|
||||
["mysql", "-u", user, f"--password={password}", "-e", "SET FOREIGN_KEY_CHECKS=1;"],
|
||||
)
|
||||
|
||||
with open(sql_path, "rb") as f:
|
||||
docker_exec(container, [client, "-u", user, f"--password={password}", db_name], stdin=f)
|
||||
|
||||
print(f"MariaDB/MySQL restore complete for db '{db_name}'.")
|
||||
Reference in New Issue
Block a user