mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-03-17 03:04:46 +08:00
feat: 优化 PostgreSQL 远程数据库备份恢复逻辑 (#3528)
This commit is contained in:
parent
39818b8424
commit
3c2472ab6c
@ -11,11 +11,14 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
||||||
"github.com/1Panel-dev/1Panel/backend/global"
|
"github.com/1Panel-dev/1Panel/backend/global"
|
||||||
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/backend/buserr"
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
||||||
"github.com/1Panel-dev/1Panel/backend/constant"
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/utils/docker"
|
||||||
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
"github.com/1Panel-dev/1Panel/backend/utils/files"
|
||||||
_ "github.com/jackc/pgx/v5/stdlib"
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
)
|
)
|
||||||
@ -106,10 +109,10 @@ func (r *Remote) Backup(info BackupInfo) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
fileNameItem := info.TargetDir + "/" + strings.TrimSuffix(info.FileName, ".gz")
|
fileNameItem := info.TargetDir + "/" + strings.TrimSuffix(info.FileName, ".gz")
|
||||||
|
imageTag := loadImageTag()
|
||||||
backupCommand := exec.Command("bash", "-c",
|
backupCommand := exec.Command("bash", "-c",
|
||||||
fmt.Sprintf("docker run --rm --net=host -i postgres:alpine /bin/bash -c 'PGPASSWORD=%s pg_dump -h %s -p %d --no-owner -Fc -U %s %s' > %s",
|
fmt.Sprintf("docker run --rm --net=host -i %s /bin/bash -c 'PGPASSWORD=%s pg_dump -h %s -p %d --no-owner -Fc -U %s %s' > %s",
|
||||||
r.Password, r.Address, r.Port, r.User, info.Name, fileNameItem))
|
imageTag, r.Password, r.Address, r.Port, r.User, info.Name, fileNameItem))
|
||||||
_ = backupCommand.Run()
|
_ = backupCommand.Run()
|
||||||
b := make([]byte, 5)
|
b := make([]byte, 5)
|
||||||
n := []byte{80, 71, 68, 77, 80}
|
n := []byte{80, 71, 68, 77, 80}
|
||||||
@ -146,9 +149,10 @@ func (r *Remote) Recover(info RecoverInfo) error {
|
|||||||
_, _ = gzipCmd.CombinedOutput()
|
_, _ = gzipCmd.CombinedOutput()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
imageTag := loadImageTag()
|
||||||
recoverCommand := exec.Command("bash", "-c",
|
recoverCommand := exec.Command("bash", "-c",
|
||||||
fmt.Sprintf("docker run --rm --net=host -i postgres:16.1-alpine /bin/bash -c 'PGPASSWORD=%s pg_restore -h %s -p %d --verbose --clean --no-privileges --no-owner -Fc -U %s -d %s --role=%s' < %s",
|
fmt.Sprintf("docker run --rm --net=host -i %s /bin/bash -c 'PGPASSWORD=%s pg_restore -h %s -p %d --verbose --clean --no-privileges --no-owner -Fc -U %s -d %s --role=%s' < %s",
|
||||||
r.Password, r.Address, r.Port, r.User, info.Name, info.Username, fileName))
|
imageTag, r.Password, r.Address, r.Port, r.User, info.Name, info.Username, fileName))
|
||||||
pipe, _ := recoverCommand.StdoutPipe()
|
pipe, _ := recoverCommand.StdoutPipe()
|
||||||
stderrPipe, _ := recoverCommand.StderrPipe()
|
stderrPipe, _ := recoverCommand.StderrPipe()
|
||||||
defer pipe.Close()
|
defer pipe.Close()
|
||||||
@ -216,3 +220,37 @@ func (r *Remote) ExecSQL(command string, timeout uint) error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadImageTag() string {
|
||||||
|
var (
|
||||||
|
app model.App
|
||||||
|
appDetails []model.AppDetail
|
||||||
|
itemTag = "postgres:16.1-alpine"
|
||||||
|
)
|
||||||
|
if err := global.DB.Where("key = ?", "postgresql").First(&app).Error; err != nil {
|
||||||
|
return itemTag
|
||||||
|
}
|
||||||
|
if err := global.DB.Where("app_id = ?", app.ID).Find(&appDetails).Error; err != nil {
|
||||||
|
return itemTag
|
||||||
|
}
|
||||||
|
|
||||||
|
client, err := docker.NewDockerClient()
|
||||||
|
if err != nil {
|
||||||
|
return itemTag
|
||||||
|
}
|
||||||
|
images, err := client.ImageList(context.Background(), types.ImageListOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return itemTag
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, item := range appDetails {
|
||||||
|
for _, image := range images {
|
||||||
|
for _, tag := range image.RepoTags {
|
||||||
|
if tag == "postgres:"+item.Version {
|
||||||
|
return tag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return itemTag
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user