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
import asyncio
import shlex
import shutil
from datetime import datetime, timezone
from urllib.parse import urlparse
from fastapi.responses import StreamingResponse
@ -64,11 +65,18 @@ class AdminDatabaseService:
db_name = p.path.lstrip("/") or "acupedia_db"
scope_flag = "" if scope == "full" else "-n content"
cmd = (
f"PGPASSWORD={shlex.quote(pwd)} pg_dump "
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"
)
if shutil.which("pg_dump"):
cmd = (
f"PGPASSWORD={shlex.quote(pwd)} pg_dump "
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")
filename = f"acupedia_db_backup_{scope}_{timestamp_str}.sql.gz"