2025-12-26 18:13:26 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import pathlib
|
|
|
|
|
import re
|
2025-12-28 22:12:31 +01:00
|
|
|
import logging
|
2025-12-28 22:51:12 +01:00
|
|
|
import pandas
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
from .shell import BackupException, execute_shell_command
|
|
|
|
|
|
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:
|
|
|
|
|
if container in database_containers:
|
|
|
|
|
return container
|
|
|
|
|
return re.split(r"(_|-)(database|db|postgres)", container)[0]
|
|
|
|
|
|
|
|
|
|
|
2025-12-28 22:51:12 +01:00
|
|
|
def fallback_pg_dumpall(container: str, username: str, password: str, out_file: str) -> None:
|
2025-12-26 18:13:26 +01:00
|
|
|
cmd = (
|
|
|
|
|
f"PGPASSWORD={password} docker exec -i {container} "
|
|
|
|
|
f"pg_dumpall -U {username} -h localhost > {out_file}"
|
|
|
|
|
)
|
|
|
|
|
execute_shell_command(cmd)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def backup_database(
|
|
|
|
|
*,
|
|
|
|
|
container: str,
|
|
|
|
|
volume_dir: str,
|
|
|
|
|
db_type: str,
|
|
|
|
|
databases_df: "pandas.DataFrame",
|
|
|
|
|
database_containers: list[str],
|
2025-12-28 22:51:12 +01:00
|
|
|
) -> bool:
|
|
|
|
|
"""
|
|
|
|
|
Returns True if at least one dump file was produced, else False.
|
|
|
|
|
"""
|
2025-12-26 18:13:26 +01:00
|
|
|
instance_name = get_instance(container, database_containers)
|
|
|
|
|
entries = databases_df.loc[databases_df["instance"] == instance_name]
|
|
|
|
|
if entries.empty:
|
2025-12-28 22:51:12 +01:00
|
|
|
log.warning("No entry found for instance '%s' (skipping DB dump)", instance_name)
|
|
|
|
|
return False
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
out_dir = os.path.join(volume_dir, "sql")
|
|
|
|
|
pathlib.Path(out_dir).mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = False
|
|
|
|
|
|
|
|
|
|
for row in entries.itertuples(index=False):
|
|
|
|
|
db_name = row.database
|
|
|
|
|
user = row.username
|
|
|
|
|
password = row.password
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
dump_file = os.path.join(out_dir, f"{db_name}.backup.sql")
|
|
|
|
|
|
|
|
|
|
if db_type == "mariadb":
|
|
|
|
|
cmd = (
|
|
|
|
|
f"docker exec {container} /usr/bin/mariadb-dump "
|
|
|
|
|
f"-u {user} -p{password} {db_name} > {dump_file}"
|
|
|
|
|
)
|
|
|
|
|
execute_shell_command(cmd)
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = True
|
2025-12-26 18:13:26 +01:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if db_type == "postgres":
|
|
|
|
|
cluster_file = os.path.join(out_dir, f"{instance_name}.cluster.backup.sql")
|
2025-12-28 22:51:12 +01:00
|
|
|
|
2025-12-26 18:13:26 +01:00
|
|
|
if not db_name:
|
|
|
|
|
fallback_pg_dumpall(container, user, password, cluster_file)
|
2025-12-28 22:51:12 +01:00
|
|
|
return True
|
2025-12-26 18:13:26 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
cmd = (
|
|
|
|
|
f"PGPASSWORD={password} docker exec -i {container} "
|
|
|
|
|
f"pg_dump -U {user} -d {db_name} -h localhost > {dump_file}"
|
|
|
|
|
)
|
|
|
|
|
execute_shell_command(cmd)
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = True
|
2025-12-26 18:13:26 +01:00
|
|
|
except BackupException as e:
|
|
|
|
|
print(f"pg_dump failed: {e}", flush=True)
|
2025-12-28 22:12:31 +01:00
|
|
|
print(
|
|
|
|
|
f"Falling back to pg_dumpall for instance '{instance_name}'",
|
|
|
|
|
flush=True,
|
|
|
|
|
)
|
2025-12-26 18:13:26 +01:00
|
|
|
fallback_pg_dumpall(container, user, password, cluster_file)
|
2025-12-28 22:51:12 +01:00
|
|
|
produced = True
|
2025-12-26 18:13:26 +01:00
|
|
|
continue
|
2025-12-28 22:51:12 +01:00
|
|
|
|
|
|
|
|
return produced
|