From 428743d600f0760aceeb3575f778cfbc1001ae77 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Wed, 5 Jun 2024 14:31:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E4=B8=8D=E5=B8=A6?= =?UTF-8?q?=E5=AF=86=E7=A0=81=E5=8E=8B=E7=BC=A9=E6=96=87=E4=BB=B6=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98=20(#5296)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ssonglius11 --- backend/app/service/cronjob_helper.go | 12 ++++++++++-- backend/app/service/snapshot.go | 6 +++++- backend/app/service/snapshot_create.go | 6 +++++- backend/app/service/snapshot_recover.go | 5 ++++- backend/utils/files/tar_gz.go | 19 ++++++++++++++----- 5 files changed, 38 insertions(+), 10 deletions(-) diff --git a/backend/app/service/cronjob_helper.go b/backend/app/service/cronjob_helper.go index c4bf920c6..03435b4df 100644 --- a/backend/app/service/cronjob_helper.go +++ b/backend/app/service/cronjob_helper.go @@ -165,7 +165,11 @@ func handleTar(sourceDir, targetDir, name, exclusionRules string, secret string) } else { commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s %s", targetDir+"/"+name, excludeRules, path) } - global.LOG.Debug(strings.ReplaceAll(commands, secret, "******")) + if len(secret) != 0 { + global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) + } else { + global.LOG.Debug(commands) + } stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour) if err != nil { if len(stdout) != 0 { @@ -189,7 +193,11 @@ func handleUnTar(sourceFile, targetDir string, secret string) error { } else { commands = fmt.Sprintf("tar -zxvf %s %s", sourceFile+" -C ", targetDir+" > /dev/null 2>&1") } - global.LOG.Debug(strings.ReplaceAll(commands, secret, "******")) + if len(secret) != 0 { + global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) + } else { + global.LOG.Debug(commands) + } stdout, err := cmd.ExecWithTimeOut(commands, 24*time.Hour) if err != nil { global.LOG.Errorf("do handle untar failed, stdout: %s, err: %v", stdout, err) diff --git a/backend/app/service/snapshot.go b/backend/app/service/snapshot.go index d20083e24..b82a23459 100644 --- a/backend/app/service/snapshot.go +++ b/backend/app/service/snapshot.go @@ -396,7 +396,11 @@ func (u *SnapshotService) handleUnTar(sourceDir, targetDir string, secret string } else { commands = fmt.Sprintf("tar -zxvf %s %s", sourceDir+" -C ", targetDir+" > /dev/null 2>&1") } - global.LOG.Debug(strings.ReplaceAll(commands, secret, "******")) + if len(secret) != 0 { + global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) + } else { + global.LOG.Debug(commands) + } stdout, err := cmd.ExecWithTimeOut(commands, 30*time.Minute) if err != nil { if len(stdout) != 0 { diff --git a/backend/app/service/snapshot_create.go b/backend/app/service/snapshot_create.go index 3926f5bc5..42ec4aaa7 100644 --- a/backend/app/service/snapshot_create.go +++ b/backend/app/service/snapshot_create.go @@ -261,7 +261,11 @@ func handleSnapTar(sourceDir, targetDir, name, exclusionRules string, secret str } else { commands = fmt.Sprintf("tar --warning=no-file-changed --ignore-failed-read -zcf %s %s -C %s .", targetDir+"/"+name, exStr, sourceDir) } - global.LOG.Debug(strings.ReplaceAll(commands, secret, "******")) + if len(secret) != 0 { + global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) + } else { + global.LOG.Debug(commands) + } stdout, err := cmd.ExecWithTimeOut(commands, 30*time.Minute) if err != nil { if len(stdout) != 0 { diff --git a/backend/app/service/snapshot_recover.go b/backend/app/service/snapshot_recover.go index 67528bc81..f5a7e2f08 100644 --- a/backend/app/service/snapshot_recover.go +++ b/backend/app/service/snapshot_recover.go @@ -39,7 +39,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b req.IsNew = true } if req.IsNew || snap.InterruptStep == "Decompress" { - if err := u.handleUnTar(fmt.Sprintf("%s/%s.tar.gz", baseDir, snap.Name), baseDir, req.Secret); err != nil { + if err := handleUnTar(fmt.Sprintf("%s/%s.tar.gz", baseDir, snap.Name), baseDir, req.Secret); err != nil { updateRecoverStatus(snap.ID, isRecover, "Decompress", constant.StatusFailed, fmt.Sprintf("decompress file failed, err: %v", err)) return } @@ -55,6 +55,9 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b req.IsNew = true } snapFileDir = fmt.Sprintf("%s/%s", baseDir, snap.Name) + if _, err := os.Stat(snapFileDir); err != nil { + snapFileDir = baseDir + } } else { snapFileDir = fmt.Sprintf("%s/1panel_original/original_%s", global.CONF.System.BaseDir, snap.Name) if _, err := os.Stat(snapFileDir); err != nil { diff --git a/backend/utils/files/tar_gz.go b/backend/utils/files/tar_gz.go index 6316898fd..4658144f1 100644 --- a/backend/utils/files/tar_gz.go +++ b/backend/utils/files/tar_gz.go @@ -2,10 +2,11 @@ package files import ( "fmt" - "github.com/1Panel-dev/1Panel/backend/global" - "github.com/1Panel-dev/1Panel/backend/utils/cmd" "path/filepath" "strings" + + "github.com/1Panel-dev/1Panel/backend/global" + "github.com/1Panel-dev/1Panel/backend/utils/cmd" ) type TarGzArchiver struct { @@ -24,7 +25,11 @@ func (t TarGzArchiver) Extract(filePath, dstDir string, secret string) error { } else { commands = fmt.Sprintf("tar -zxvf %s %s", filePath+" -C ", dstDir+" > /dev/null 2>&1") } - global.LOG.Debug(strings.ReplaceAll(commands, secret, "******")) + if len(secret) != 0 { + global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) + } else { + global.LOG.Debug(commands) + } if err = cmd.ExecCmd(commands); err != nil { return err } @@ -48,9 +53,13 @@ func (t TarGzArchiver) Compress(sourcePaths []string, dstFile string, secret str extraCmd := "| openssl enc -aes-256-cbc -salt -k " + secret + " -out" commands = fmt.Sprintf("tar -zcf %s %s %s", path, extraCmd, dstFile) } else { - commands = fmt.Sprintf("tar -zcf %s %s", dstFile, path) + commands = fmt.Sprintf("tar -zcf %s -C %s %s", dstFile, aheadDir, itemDir) + } + if len(secret) != 0 { + global.LOG.Debug(strings.ReplaceAll(commands, fmt.Sprintf(" %s ", secret), "******")) + } else { + global.LOG.Debug(commands) } - global.LOG.Debug(strings.ReplaceAll(commands, secret, "******")) if err = cmd.ExecCmd(commands); err != nil { return err }