fix(backup): fallback to docker container for pg_dump when not installed on host

This commit is contained in:
Urvish Prajapati 2026-09-11 16:29:00 +05:30
parent bbae243054
commit bcfbdc8442
1 changed files with 13 additions and 5 deletions

View File

@ -1,6 +1,7 @@
from __future__ import annotations from __future__ import annotations
import asyncio import asyncio
import shlex import shlex
import shutil
from datetime import datetime, timezone from datetime import datetime, timezone
from urllib.parse import urlparse from urllib.parse import urlparse
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
@ -64,11 +65,18 @@ class AdminDatabaseService:
db_name = p.path.lstrip("/") or "acupedia_db" db_name = p.path.lstrip("/") or "acupedia_db"
scope_flag = "" if scope == "full" else "-n content" scope_flag = "" if scope == "full" else "-n content"
cmd = ( if shutil.which("pg_dump"):
f"PGPASSWORD={shlex.quote(pwd)} pg_dump " cmd = (
f"-h {shlex.quote(host)} -p {shlex.quote(port)} -U {shlex.quote(user)} -d {shlex.quote(db_name)} " f"PGPASSWORD={shlex.quote(pwd)} pg_dump "
f"--clean --if-exists {scope_flag} | gzip -9" f"-h {shlex.quote(host)} -p {shlex.quote(port)} -U {shlex.quote(user)} -d {shlex.quote(db_name)} "
) f"--clean --if-exists {scope_flag} | gzip -9"
)
else:
cmd = (
f"docker exec -e PGPASSWORD={shlex.quote(pwd)} panacea-postgres pg_dump "
f"-U {shlex.quote(user)} -d {shlex.quote(db_name)} "
f"--clean --if-exists {scope_flag} | gzip -9"
)
timestamp_str = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S") timestamp_str = datetime.now(timezone.utc).strftime("%Y%m%d_%H%M%S")
filename = f"acupedia_db_backup_{scope}_{timestamp_str}.sql.gz" filename = f"acupedia_db_backup_{scope}_{timestamp_str}.sql.gz"