Skip to content

Commit

Permalink
feat: 升级回滚增加相关数据库文件 (1Panel-dev#4638)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu committed Apr 22, 2024
1 parent 884a169 commit 7fd672c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
17 changes: 13 additions & 4 deletions backend/app/service/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,26 @@ 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
}

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)
Expand Down
37 changes: 35 additions & 2 deletions cmd/server/cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand All @@ -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
}

0 comments on commit 7fd672c

Please sign in to comment.