mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2025-12-27 11:06:35 +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:
1
src/baudolo/restore/db/__init__.py
Normal file
1
src/baudolo/restore/db/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
"""Database restore handlers (Postgres, MariaDB/MySQL)."""
|
||||
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}'.")
|
||||
53
src/baudolo/restore/db/postgres.py
Normal file
53
src/baudolo/restore/db/postgres.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
|
||||
from ..run import docker_exec
|
||||
|
||||
|
||||
def restore_postgres_sql(
|
||||
*,
|
||||
container: str,
|
||||
db_name: str,
|
||||
user: str,
|
||||
password: str,
|
||||
sql_path: str,
|
||||
empty: bool,
|
||||
) -> None:
|
||||
if not os.path.isfile(sql_path):
|
||||
raise FileNotFoundError(sql_path)
|
||||
|
||||
# Make password available INSIDE the container for psql.
|
||||
docker_env = {"PGPASSWORD": password}
|
||||
|
||||
if empty:
|
||||
drop_sql = r"""
|
||||
DO $$ DECLARE r RECORD;
|
||||
BEGIN
|
||||
FOR r IN (
|
||||
SELECT table_name AS name, 'TABLE' AS type FROM information_schema.tables WHERE table_schema='public'
|
||||
UNION ALL
|
||||
SELECT routine_name AS name, 'FUNCTION' AS type FROM information_schema.routines WHERE specific_schema='public'
|
||||
UNION ALL
|
||||
SELECT sequence_name AS name, 'SEQUENCE' AS type FROM information_schema.sequences WHERE sequence_schema='public'
|
||||
) LOOP
|
||||
EXECUTE format('DROP %s public.%I CASCADE', r.type, r.name);
|
||||
END LOOP;
|
||||
END $$;
|
||||
"""
|
||||
docker_exec(
|
||||
container,
|
||||
["psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db_name],
|
||||
stdin=drop_sql.encode(),
|
||||
docker_env=docker_env,
|
||||
)
|
||||
|
||||
with open(sql_path, "rb") as f:
|
||||
docker_exec(
|
||||
container,
|
||||
["psql", "-v", "ON_ERROR_STOP=1", "-U", user, "-d", db_name],
|
||||
stdin=f,
|
||||
docker_env=docker_env,
|
||||
)
|
||||
|
||||
print(f"PostgreSQL restore complete for db '{db_name}'.")
|
||||
Reference in New Issue
Block a user