fix: Fix PostgreSQL database backup/restore failures (#9854)

This commit is contained in:
ssongliu
2025-08-05 14:30:34 +08:00
committed by GitHub
parent f617c9d5dd
commit 58b95ef2ba
5 changed files with 25 additions and 15 deletions

View File

@@ -136,7 +136,11 @@ func (r *Local) Backup(info BackupInfo) error {
}
defer outfile.Close()
global.LOG.Infof("start to pg_dump | gzip > %s.gzip", info.TargetDir+"/"+info.FileName)
cmd := exec.Command(
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
defer cancel()
cmd := exec.CommandContext(
ctx,
"docker", "exec", "-i", r.ContainerName,
"sh", "-c",
fmt.Sprintf("PGPASSWORD=%s pg_dump -F c -U %s -d %s", r.Password, r.Username, info.Name),
@@ -159,8 +163,11 @@ func (r *Local) Backup(info BackupInfo) error {
func (r *Local) Recover(info RecoverInfo) error {
fi, _ := os.Open(info.SourceFile)
defer fi.Close()
cmd := exec.Command("docker", "exec", r.ContainerName, "sh", "-c",
fmt.Sprintf("PGPASSWORD=%s pg_dump -F c -U %s -d %s", r.Password, r.Username, info.Name),
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(info.Timeout*uint(time.Second)))
defer cancel()
cmd := exec.CommandContext(ctx, "docker", "exec", "-i", r.ContainerName, "sh", "-c",
fmt.Sprintf("PGPASSWORD=%s pg_restore -F c -U %s -d %s", r.Password, r.Username, info.Name),
)
if strings.HasSuffix(info.SourceFile, ".gz") {
gzipFile, err := os.Open(info.SourceFile)