From 3c2472ab6c2795f2b3914e3737c8d87aad5d36dc Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 8 Jan 2024 18:19:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20PostgreSQL=20?= =?UTF-8?q?=E8=BF=9C=E7=A8=8B=E6=95=B0=E6=8D=AE=E5=BA=93=E5=A4=87=E4=BB=BD?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E9=80=BB=E8=BE=91=20(#3528)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/utils/postgresql/client/remote.go | 48 ++++++++++++++++++++--- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/backend/utils/postgresql/client/remote.go b/backend/utils/postgresql/client/remote.go index 79124a5bb..6c47787da 100644 --- a/backend/utils/postgresql/client/remote.go +++ b/backend/utils/postgresql/client/remote.go @@ -11,11 +11,14 @@ import ( "strings" "time" + "github.com/1Panel-dev/1Panel/backend/app/model" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/docker/docker/api/types" "github.com/pkg/errors" "github.com/1Panel-dev/1Panel/backend/buserr" "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/jackc/pgx/v5/stdlib" ) @@ -106,10 +109,10 @@ func (r *Remote) Backup(info BackupInfo) error { } } fileNameItem := info.TargetDir + "/" + strings.TrimSuffix(info.FileName, ".gz") - + imageTag := loadImageTag() 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", - r.Password, r.Address, r.Port, r.User, info.Name, fileNameItem)) + 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", + imageTag, r.Password, r.Address, r.Port, r.User, info.Name, fileNameItem)) _ = backupCommand.Run() b := make([]byte, 5) n := []byte{80, 71, 68, 77, 80} @@ -146,9 +149,10 @@ func (r *Remote) Recover(info RecoverInfo) error { _, _ = gzipCmd.CombinedOutput() }() } + imageTag := loadImageTag() 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", - r.Password, r.Address, r.Port, r.User, info.Name, info.Username, fileName)) + 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", + imageTag, r.Password, r.Address, r.Port, r.User, info.Name, info.Username, fileName)) pipe, _ := recoverCommand.StdoutPipe() stderrPipe, _ := recoverCommand.StderrPipe() defer pipe.Close() @@ -216,3 +220,37 @@ func (r *Remote) ExecSQL(command string, timeout uint) error { 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 +}