2025-12-26 18:13:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-08-17 04:35:50 +02:00
|
|
|
import logging
|
2025-12-26 18:13:26 +01:00
|
|
|
import pathlib
|
|
|
|
|
import re
|
2026-08-18 01:47:38 +02:00
|
|
|
from typing import TYPE_CHECKING
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-17 16:30:25 +02:00
|
|
|
from baudolo.databases import CLUSTER_ROW, validate_database
|
2026-08-18 01:47:38 +02:00
|
|
|
from baudolo.generation import CLUSTER_SUFFIX, DUMP_SUFFIX, SQL_DIR
|
2026-08-17 16:30:25 +02:00
|
|
|
|
|
|
|
|
from .docker import docker_exec_argv
|
2026-08-18 01:47:38 +02:00
|
|
|
from .shell import BackupError, execute_to_file
|
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
import pandas as pd
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2025-12-28 22:12:31 +01:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
def get_instance(container: str, database_containers: list[str]) -> str:
|
2025-12-29 11:39:57 +01:00
|
|
|
"""
|
|
|
|
|
Derive a stable instance name from the container name.
|
|
|
|
|
"""
|
2025-12-26 18:13:26 +01:00
|
|
|
if container in database_containers:
|
|
|
|
|
return container
|
|
|
|
|
return re.split(r"(_|-)(database|db|postgres)", container)[0]
|
|
|
|
|
|
|
|
|
|
|
2026-01-10 18:40:22 +01:00
|
|
|
def fallback_pg_dumpall(
|
|
|
|
|
container: str, username: str, password: str, out_file: str
|
|
|
|
|
) -> None:
|
2025-12-29 11:39:57 +01:00
|
|
|
"""
|
|
|
|
|
Perform a full Postgres cluster dump using pg_dumpall.
|
|
|
|
|
"""
|
2026-08-17 16:30:25 +02:00
|
|
|
execute_to_file(
|
|
|
|
|
docker_exec_argv(
|
|
|
|
|
container,
|
|
|
|
|
["pg_dumpall", "-U", username, "-h", "localhost"],
|
|
|
|
|
interactive=True,
|
|
|
|
|
),
|
|
|
|
|
out_file,
|
|
|
|
|
env={"PGPASSWORD": password},
|
2025-12-26 18:13:26 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def backup_database(
|
|
|
|
|
*,
|
|
|
|
|
container: str,
|
|
|
|
|
volume_dir: str,
|
|
|
|
|
db_type: str,
|
2026-08-17 13:51:26 +02:00
|
|
|
dump_tool: str,
|
2026-08-18 01:47:38 +02:00
|
|
|
databases_df: pd.DataFrame,
|
2025-12-26 18:13:26 +01:00
|
|
|
database_containers: list[str],
|
2025-12-28 22:51:12 +01:00
|
|
|
) -> bool:
|
|
|
|
|
"""
|
2025-12-29 11:39:57 +01:00
|
|
|
Backup databases for a given DB container.
|
|
|
|
|
|
2026-08-17 13:51:26 +02:00
|
|
|
Args:
|
|
|
|
|
dump_tool: the MariaDB client found in the container, so an image
|
|
|
|
|
that ships only mysqldump is dumped with the tool it has.
|
|
|
|
|
|
2025-12-29 11:39:57 +01:00
|
|
|
Returns True if at least one dump was produced.
|
2025-12-28 22:51:12 +01:00
|
|
|
"""
|
2025-12-26 18:13:26 +01:00
|
|
|
instance_name = get_instance(container, database_containers)
|
2025-12-29 11:39:57 +01:00
|
|
|
|
|
|
|
|
entries = databases_df[databases_df["instance"] == instance_name]
|
2025-12-26 18:13:26 +01:00
|
|
|
if entries.empty:
|
2025-12-29 11:39:57 +01:00
|
|
|
log.debug("No database entries for instance '%s'", instance_name)
|
2025-12-28 22:51:12 +01:00
|
|
|
return False
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
out_dir = pathlib.Path(volume_dir) / SQL_DIR
|
|
|
|
|
out_dir.mkdir(parents=True, exist_ok=True)
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = False
|
|
|
|
|
|
|
|
|
|
for row in entries.itertuples(index=False):
|
2025-12-29 11:39:57 +01:00
|
|
|
raw_db = getattr(row, "database", "")
|
|
|
|
|
user = (getattr(row, "username", "") or "").strip()
|
|
|
|
|
password = (getattr(row, "password", "") or "").strip()
|
2025-12-26 18:13:26 +01:00
|
|
|
|
2026-08-17 16:30:25 +02:00
|
|
|
db_value = validate_database(raw_db, instance=instance_name)
|
2025-12-29 11:39:57 +01:00
|
|
|
|
2026-08-17 16:30:25 +02:00
|
|
|
if db_value == CLUSTER_ROW:
|
2025-12-29 11:39:57 +01:00
|
|
|
if db_type != "postgres":
|
|
|
|
|
raise ValueError(
|
|
|
|
|
f"databases.csv entry for instance '{instance_name}': "
|
2026-08-17 16:30:25 +02:00
|
|
|
f"'{CLUSTER_ROW}' is currently only supported for Postgres."
|
2025-12-29 11:39:57 +01:00
|
|
|
)
|
|
|
|
|
|
2026-08-18 01:47:38 +02:00
|
|
|
cluster_file = str(out_dir / f"{instance_name}{CLUSTER_SUFFIX}")
|
2025-12-29 11:39:57 +01:00
|
|
|
fallback_pg_dumpall(container, user, password, cluster_file)
|
|
|
|
|
produced = True
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
db_name = db_value
|
2026-08-18 01:47:38 +02:00
|
|
|
dump_file = str(out_dir / f"{db_name}{DUMP_SUFFIX}")
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
if db_type == "mariadb":
|
2026-05-26 00:46:47 +02:00
|
|
|
# Force TCP so auth matches '<user>'@'%' instead of socket -> 'localhost'.
|
2026-08-17 16:30:25 +02:00
|
|
|
execute_to_file(
|
|
|
|
|
docker_exec_argv(
|
|
|
|
|
container,
|
|
|
|
|
[
|
|
|
|
|
dump_tool,
|
|
|
|
|
"-h",
|
|
|
|
|
"127.0.0.1",
|
|
|
|
|
"--protocol=tcp",
|
|
|
|
|
"-u",
|
|
|
|
|
user,
|
|
|
|
|
f"-p{password}",
|
|
|
|
|
db_name,
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
dump_file,
|
2025-12-26 18:13:26 +01:00
|
|
|
)
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = True
|
2025-12-26 18:13:26 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if db_type == "postgres":
|
|
|
|
|
try:
|
2026-08-17 16:30:25 +02:00
|
|
|
execute_to_file(
|
|
|
|
|
docker_exec_argv(
|
|
|
|
|
container,
|
|
|
|
|
[
|
|
|
|
|
"pg_dump",
|
|
|
|
|
"-U",
|
|
|
|
|
user,
|
|
|
|
|
"-d",
|
|
|
|
|
db_name,
|
|
|
|
|
"-h",
|
|
|
|
|
"localhost",
|
|
|
|
|
"--no-owner",
|
|
|
|
|
"--no-privileges",
|
|
|
|
|
],
|
|
|
|
|
interactive=True,
|
|
|
|
|
),
|
|
|
|
|
dump_file,
|
|
|
|
|
env={"PGPASSWORD": password},
|
2025-12-26 18:13:26 +01:00
|
|
|
)
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = True
|
2026-08-18 01:47:38 +02:00
|
|
|
except BackupError as e:
|
|
|
|
|
raise BackupError(
|
2025-12-29 11:39:57 +01:00
|
|
|
f"Postgres dump failed for instance '{instance_name}', "
|
|
|
|
|
f"database '{db_name}'. This database was explicitly configured "
|
|
|
|
|
"and therefore must succeed.\n"
|
|
|
|
|
f"{e}"
|
2026-08-18 01:47:38 +02:00
|
|
|
) from e
|
2025-12-26 18:13:26 +01:00
|
|
|
continue
|
2025-12-28 22:51:12 +01:00
|
|
|
|
|
|
|
|
return produced
|