From 7fd672c1c9b4bfe2cc66daf91d3731f7148fa787 Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 22 Apr 2024 22:22:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=87=E7=BA=A7=E5=9B=9E=E6=BB=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=9B=B8=E5=85=B3=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=96=87=E4=BB=B6=20(#4638)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/upgrade.go | 17 ++++++++++++---- cmd/server/cmd/restore.go | 37 ++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/backend/app/service/upgrade.go b/backend/app/service/upgrade.go index f4732dc67..8e662fa17 100644 --- a/backend/app/service/upgrade.go +++ b/backend/app/service/upgrade.go @@ -173,8 +173,8 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e if err := fileOp.Copy("/etc/systemd/system/1panel.service", originalDir); err != nil { return err } - dbPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile - if err := fileOp.Copy(dbPath, originalDir); err != nil { + checkPointOfWal() + if err := handleTar(path.Join(global.CONF.System.BaseDir, "1panel/db"), originalDir, "db.tar.gz", "./1Panel.db-*"); err != nil { return err } return nil @@ -182,8 +182,17 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e func (u *UpgradeService) handleRollback(originalDir string, errStep int) { _ = settingRepo.Update("SystemStatus", "Free") - if err := common.CopyFile(path.Join(originalDir, "1Panel.db"), global.CONF.System.DbPath); err != nil { - global.LOG.Errorf("rollback 1panel failed, err: %v", err) + + checkPointOfWal() + if _, err := os.Stat(path.Join(originalDir, "1Panel.db")); err == nil { + if err := common.CopyFile(path.Join(originalDir, "1Panel.db"), global.CONF.System.DbPath); err != nil { + global.LOG.Errorf("rollback 1panel db failed, err: %v", err) + } + } + if _, err := os.Stat(path.Join(originalDir, "db.tar.gz")); err == nil { + if err := handleUnTar(path.Join(originalDir, "db.tar.gz"), global.CONF.System.DbPath); err != nil { + global.LOG.Errorf("rollback 1panel db failed, err: %v", err) + } } if err := common.CopyFile(path.Join(originalDir, "1panel"), "/usr/local/bin"); err != nil { global.LOG.Errorf("rollback 1pctl failed, err: %v", err) diff --git a/cmd/server/cmd/restore.go b/cmd/server/cmd/restore.go index 9708b0b6e..c59fba859 100644 --- a/cmd/server/cmd/restore.go +++ b/cmd/server/cmd/restore.go @@ -6,9 +6,11 @@ import ( "path" "sort" "strings" + "time" cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common" + "github.com/pkg/errors" "github.com/spf13/cobra" ) @@ -55,8 +57,16 @@ var restoreCmd = &cobra.Command{ return err } fmt.Println("(3/4) 1panel 服务回滚成功") - if err := common.CopyFile(path.Join(tmpPath, "1Panel.db"), path.Join(baseDir, "1panel", "db")); err != nil { - return err + checkPointOfWal() + if _, err := os.Stat(path.Join(tmpPath, "1Panel.db")); err == nil { + if err := common.CopyFile(path.Join(tmpPath, "1Panel.db"), path.Join(baseDir, "1panel/db")); err != nil { + return err + } + } + if _, err := os.Stat(path.Join(tmpPath, "db.tar.gz")); err == nil { + if err := handleUnTar(path.Join(tmpPath, "db.tar.gz"), path.Join(baseDir, "1panel")); err != nil { + return err + } } fmt.Printf("(4/4) 1panel 数据回滚成功 \n\n") @@ -65,6 +75,14 @@ var restoreCmd = &cobra.Command{ }, } +func checkPointOfWal() { + db, err := loadDBConn() + if err != nil { + return + } + _ = db.Exec("PRAGMA wal_checkpoint(TRUNCATE);").Error +} + func loadRestorePath(upgradeDir string) (string, error) { if _, err := os.Stat(upgradeDir); err != nil && os.IsNotExist(err) { return "暂无可回滚文件", nil @@ -87,3 +105,18 @@ func loadRestorePath(upgradeDir string) (string, error) { }) return folders[0], nil } + +func handleUnTar(sourceFile, targetDir string) error { + if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) { + if err = os.MkdirAll(targetDir, os.ModePerm); err != nil { + return err + } + } + + commands := fmt.Sprintf("tar zxvfC %s %s", sourceFile, targetDir) + stdout, err := cmdUtils.ExecWithTimeOut(commands, 20*time.Second) + if err != nil { + return errors.New(stdout) + } + return nil +}