mirror of
https://github.com/kevinveenbirkenbach/docker-volume-backup.git
synced 2025-12-29 11:43:17 +00:00
fix(backup): log missing db config instead of raising
- Use module logger in backup/db.py - Skip db dump when no databases.csv entry is present - Apply black/formatting cleanup across backup/restore/tests https://chatgpt.com/share/69519d45-b0dc-800f-acb6-6fb8504e9b46
This commit is contained in:
@@ -5,9 +5,12 @@ import pathlib
|
||||
import re
|
||||
|
||||
import pandas
|
||||
import logging
|
||||
|
||||
from .shell import BackupException, execute_shell_command
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_instance(container: str, database_containers: list[str]) -> str:
|
||||
if container in database_containers:
|
||||
@@ -15,7 +18,9 @@ def get_instance(container: str, database_containers: list[str]) -> str:
|
||||
return re.split(r"(_|-)(database|db|postgres)", container)[0]
|
||||
|
||||
|
||||
def fallback_pg_dumpall(container: str, username: str, password: str, out_file: str) -> None:
|
||||
def fallback_pg_dumpall(
|
||||
container: str, username: str, password: str, out_file: str
|
||||
) -> None:
|
||||
cmd = (
|
||||
f"PGPASSWORD={password} docker exec -i {container} "
|
||||
f"pg_dumpall -U {username} -h localhost > {out_file}"
|
||||
@@ -34,7 +39,8 @@ def backup_database(
|
||||
instance_name = get_instance(container, database_containers)
|
||||
entries = databases_df.loc[databases_df["instance"] == instance_name]
|
||||
if entries.empty:
|
||||
raise BackupException(f"No entry found for instance '{instance_name}'")
|
||||
log.warning("No entry found for instance '%s'", instance_name)
|
||||
return
|
||||
|
||||
out_dir = os.path.join(volume_dir, "sql")
|
||||
pathlib.Path(out_dir).mkdir(parents=True, exist_ok=True)
|
||||
@@ -68,6 +74,9 @@ def backup_database(
|
||||
execute_shell_command(cmd)
|
||||
except BackupException as e:
|
||||
print(f"pg_dump failed: {e}", flush=True)
|
||||
print(f"Falling back to pg_dumpall for instance '{instance_name}'", flush=True)
|
||||
print(
|
||||
f"Falling back to pg_dumpall for instance '{instance_name}'",
|
||||
flush=True,
|
||||
)
|
||||
fallback_pg_dumpall(container, user, password, cluster_file)
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user