Browse Source

fix: 修改升级等过程中复制方式 (#4402)

pull/4414/head
ssongliu 8 months ago committed by GitHub
parent
commit
cdbe24a523
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 8
      backend/app/service/cronjob_backup.go
  2. 9
      backend/app/service/snapshot.go
  3. 12
      backend/app/service/snapshot_create.go
  4. 13
      backend/app/service/snapshot_recover.go
  5. 17
      backend/app/service/upgrade.go
  6. 12
      backend/utils/cloud_storage/client/local.go
  7. 25
      backend/utils/common/common.go
  8. 18
      cmd/server/cmd/restore.go

8
backend/app/service/cronjob_backup.go

@ -326,7 +326,7 @@ func handleBackupLogs(targetDir, fileName string) error {
if len(logFiles) != 0 { if len(logFiles) != 0 {
for i := 0; i < len(logFiles); i++ { for i := 0; i < len(logFiles); i++ {
if !logFiles[i].IsDir() { if !logFiles[i].IsDir() {
_ = cpBinary([]string{path.Join(itemDir, logFiles[i].Name())}, dirItem) _ = common.CopyFile(path.Join(itemDir, logFiles[i].Name()), dirItem)
} }
} }
} }
@ -335,7 +335,7 @@ func handleBackupLogs(targetDir, fileName string) error {
if len(logFiles2) != 0 { if len(logFiles2) != 0 {
for i := 0; i < len(logFiles2); i++ { for i := 0; i < len(logFiles2); i++ {
if !logFiles2[i].IsDir() { if !logFiles2[i].IsDir() {
_ = cpBinary([]string{path.Join(itemDir2, logFiles2[i].Name())}, dirItem) _ = common.CopyFile(path.Join(itemDir2, logFiles2[i].Name()), dirItem)
} }
} }
} }
@ -354,7 +354,7 @@ func handleBackupLogs(targetDir, fileName string) error {
if len(systemLogFiles) != 0 { if len(systemLogFiles) != 0 {
for i := 0; i < len(systemLogFiles); i++ { for i := 0; i < len(systemLogFiles); i++ {
if !systemLogFiles[i].IsDir() { if !systemLogFiles[i].IsDir() {
_ = cpBinary([]string{path.Join(systemLogDir, systemLogFiles[i].Name())}, systemDir) _ = common.CopyFile(path.Join(systemLogDir, systemLogFiles[i].Name()), systemDir)
} }
} }
} }
@ -370,7 +370,7 @@ func handleBackupLogs(targetDir, fileName string) error {
if len(loginLogFiles) != 0 { if len(loginLogFiles) != 0 {
for i := 0; i < len(loginLogFiles); i++ { for i := 0; i < len(loginLogFiles); i++ {
if !loginLogFiles[i].IsDir() && (strings.HasPrefix(loginLogFiles[i].Name(), "secure") || strings.HasPrefix(loginLogFiles[i].Name(), "auth.log")) { if !loginLogFiles[i].IsDir() && (strings.HasPrefix(loginLogFiles[i].Name(), "secure") || strings.HasPrefix(loginLogFiles[i].Name(), "auth.log")) {
_ = cpBinary([]string{path.Join("/var/log", loginLogFiles[i].Name())}, loginDir) _ = common.CopyFile(path.Join("/var/log", loginLogFiles[i].Name()), loginDir)
} }
} }
} }

9
backend/app/service/snapshot.go

@ -385,15 +385,6 @@ func updateRecoverStatus(id uint, isRecover bool, interruptStep, status, message
} }
} }
func cpBinary(src []string, dst string) error {
global.LOG.Debugf(fmt.Sprintf("\\cp -f %s %s", strings.Join(src, " "), dst))
stdout, err := cmd.Exec(fmt.Sprintf("\\cp -f %s %s", strings.Join(src, " "), dst))
if err != nil {
return fmt.Errorf("cp file failed, stdout: %v, err: %v", stdout, err)
}
return nil
}
func (u *SnapshotService) handleUnTar(sourceDir, targetDir string) error { func (u *SnapshotService) handleUnTar(sourceDir, targetDir string) error {
if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) { if _, err := os.Stat(targetDir); err != nil && os.IsNotExist(err) {
if err = os.MkdirAll(targetDir, os.ModePerm); err != nil { if err = os.MkdirAll(targetDir, os.ModePerm); err != nil {

12
backend/app/service/snapshot_create.go

@ -43,7 +43,15 @@ func snapPanel(snap snapHelper, targetDir string) {
defer snap.Wg.Done() defer snap.Wg.Done()
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel": constant.Running}) _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"panel": constant.Running})
status := constant.StatusDone status := constant.StatusDone
if err := cpBinary([]string{"/usr/local/bin/1panel", "/usr/local/bin/1pctl", "/etc/systemd/system/1panel.service"}, targetDir); err != nil { if err := common.CopyFile("/usr/local/bin/1panel", targetDir); err != nil {
status = err.Error()
}
if err := common.CopyFile("/usr/local/bin/1pctl", targetDir); err != nil {
status = err.Error()
}
if err := common.CopyFile("/etc/systemd/system/1panel.service", targetDir); err != nil {
status = err.Error() status = err.Error()
} }
snap.Status.Panel = status snap.Status.Panel = status
@ -59,7 +67,7 @@ func snapDaemonJson(snap snapHelper, targetDir string) {
return return
} }
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"daemon_json": constant.Running}) _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"daemon_json": constant.Running})
if err := cpBinary([]string{"/etc/docker/daemon.json"}, path.Join(targetDir, "daemon.json")); err != nil { if err := common.CopyFile("/etc/docker/daemon.json", targetDir); err != nil {
status = err.Error() status = err.Error()
} }
snap.Status.DaemonJson = status snap.Status.DaemonJson = status

13
backend/app/service/snapshot_recover.go

@ -13,6 +13,7 @@ import (
"github.com/1Panel-dev/1Panel/backend/constant" "github.com/1Panel-dev/1Panel/backend/constant"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/1Panel-dev/1Panel/backend/utils/files" "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -88,7 +89,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b
} }
if req.IsNew || snap.InterruptStep == "1PanelBinary" { if req.IsNew || snap.InterruptStep == "1PanelBinary" {
if err := recoverPanel(path.Join(snapFileDir, "1panel/1panel"), "/usr/local/bin/1panel"); err != nil { if err := recoverPanel(path.Join(snapFileDir, "1panel/1panel"), "/usr/local/bin"); err != nil {
updateRecoverStatus(snap.ID, isRecover, "1PanelBinary", constant.StatusFailed, err.Error()) updateRecoverStatus(snap.ID, isRecover, "1PanelBinary", constant.StatusFailed, err.Error())
return return
} }
@ -96,7 +97,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b
req.IsNew = true req.IsNew = true
} }
if req.IsNew || snap.InterruptStep == "1PctlBinary" { if req.IsNew || snap.InterruptStep == "1PctlBinary" {
if err := recoverPanel(path.Join(snapFileDir, "1panel/1pctl"), "/usr/local/bin/1pctl"); err != nil { if err := recoverPanel(path.Join(snapFileDir, "1panel/1pctl"), "/usr/local/bin"); err != nil {
updateRecoverStatus(snap.ID, isRecover, "1PctlBinary", constant.StatusFailed, err.Error()) updateRecoverStatus(snap.ID, isRecover, "1PctlBinary", constant.StatusFailed, err.Error())
return return
} }
@ -104,7 +105,7 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b
req.IsNew = true req.IsNew = true
} }
if req.IsNew || snap.InterruptStep == "1PanelService" { if req.IsNew || snap.InterruptStep == "1PanelService" {
if err := recoverPanel(path.Join(snapFileDir, "1panel/1panel.service"), "/etc/systemd/system/1panel.service"); err != nil { if err := recoverPanel(path.Join(snapFileDir, "1panel/1panel.service"), "/etc/systemd/system"); err != nil {
updateRecoverStatus(snap.ID, isRecover, "1PanelService", constant.StatusFailed, err.Error()) updateRecoverStatus(snap.ID, isRecover, "1PanelService", constant.StatusFailed, err.Error())
return return
} }
@ -235,10 +236,8 @@ func recoverPanel(src string, dst string) error {
if _, err := os.Stat(src); err != nil { if _, err := os.Stat(src); err != nil {
return fmt.Errorf("file is not found in %s, err: %v", src, err) return fmt.Errorf("file is not found in %s, err: %v", src, err)
} }
global.LOG.Debugf(fmt.Sprintf("\\cp -f %s %s", src, dst)) if err := common.CopyFile(src, dst); err != nil {
stdout, err := cmd.Exec(fmt.Sprintf("\\cp -f %s %s", src, dst)) return fmt.Errorf("cp file failed, err: %v", err)
if err != nil {
return fmt.Errorf("cp file failed, stdout: %v, err: %v", stdout, err)
} }
return nil return nil
} }

17
backend/app/service/upgrade.go

@ -130,24 +130,24 @@ func (u *UpgradeService) Upgrade(req dto.Upgrade) error {
} }
global.LOG.Info("backup original data successful, now start to upgrade!") global.LOG.Info("backup original data successful, now start to upgrade!")
if err := cpBinary([]string{tmpDir + "/1panel"}, "/usr/local/bin/1panel"); err != nil { if err := common.CopyFile(path.Join(tmpDir, "1panel"), "/usr/local/bin"); err != nil {
global.LOG.Errorf("upgrade 1panel failed, err: %v", err) global.LOG.Errorf("upgrade 1panel failed, err: %v", err)
u.handleRollback(originalDir, 1) u.handleRollback(originalDir, 1)
return return
} }
if err := cpBinary([]string{tmpDir + "/1pctl"}, "/usr/local/bin/1pctl"); err != nil { if err := common.CopyFile(path.Join(tmpDir, "1pctl"), "/usr/local/bin"); err != nil {
global.LOG.Errorf("upgrade 1pctl failed, err: %v", err) global.LOG.Errorf("upgrade 1pctl failed, err: %v", err)
u.handleRollback(originalDir, 2) u.handleRollback(originalDir, 2)
return return
} }
if _, err := cmd.Execf("sed -i -e 's#BASE_DIR=.*#BASE_DIR=%s#g' /usr/local/bin/1pctl", global.CONF.System.BaseDir); err != nil { if _, err := cmd.Execf("sed -i -e 's#BASE_DIR=.*#BASE_DIR=%s#g' /usr/local/bin", global.CONF.System.BaseDir); err != nil {
global.LOG.Errorf("upgrade basedir in 1pctl failed, err: %v", err) global.LOG.Errorf("upgrade basedir in 1pctl failed, err: %v", err)
u.handleRollback(originalDir, 2) u.handleRollback(originalDir, 2)
return return
} }
if err := cpBinary([]string{tmpDir + "/1panel.service"}, "/etc/systemd/system/1panel.service"); err != nil { if err := common.CopyFile(path.Join(tmpDir, "1panel.service"), "/etc/systemd/system"); err != nil {
global.LOG.Errorf("upgrade 1panel.service failed, err: %v", err) global.LOG.Errorf("upgrade 1panel.service failed, err: %v", err)
u.handleRollback(originalDir, 3) u.handleRollback(originalDir, 3)
return return
@ -181,24 +181,23 @@ func (u *UpgradeService) handleBackup(fileOp files.FileOp, originalDir string) e
} }
func (u *UpgradeService) handleRollback(originalDir string, errStep int) { func (u *UpgradeService) handleRollback(originalDir string, errStep int) {
dbPath := global.CONF.System.DbPath + "/1Panel.db"
_ = settingRepo.Update("SystemStatus", "Free") _ = settingRepo.Update("SystemStatus", "Free")
if err := cpBinary([]string{originalDir + "/1Panel.db"}, dbPath); err != nil { if err := common.CopyFile(path.Join(originalDir, "1Panel.db"), global.CONF.System.DbPath); err != nil {
global.LOG.Errorf("rollback 1panel failed, err: %v", err) global.LOG.Errorf("rollback 1panel failed, err: %v", err)
} }
if err := cpBinary([]string{originalDir + "/1panel"}, "/usr/local/bin/1panel"); err != nil { if err := common.CopyFile(path.Join(originalDir, "1panel"), "/usr/local/bin"); err != nil {
global.LOG.Errorf("rollback 1pctl failed, err: %v", err) global.LOG.Errorf("rollback 1pctl failed, err: %v", err)
} }
if errStep == 1 { if errStep == 1 {
return return
} }
if err := cpBinary([]string{originalDir + "/1pctl"}, "/usr/local/bin/1pctl"); err != nil { if err := common.CopyFile(path.Join(originalDir, "1pctl"), "/usr/local/bin"); err != nil {
global.LOG.Errorf("rollback 1panel failed, err: %v", err) global.LOG.Errorf("rollback 1panel failed, err: %v", err)
} }
if errStep == 2 { if errStep == 2 {
return return
} }
if err := cpBinary([]string{originalDir + "/1panel.service"}, "/etc/systemd/system/1panel.service"); err != nil { if err := common.CopyFile(path.Join(originalDir, "1panel.service"), "/etc/systemd/system"); err != nil {
global.LOG.Errorf("rollback 1panel failed, err: %v", err) global.LOG.Errorf("rollback 1panel failed, err: %v", err)
} }
} }

12
backend/utils/cloud_storage/client/local.go

@ -6,7 +6,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/common"
) )
type localClient struct { type localClient struct {
@ -54,9 +54,8 @@ func (c localClient) Upload(src, target string) (bool, error) {
} }
} }
stdout, err := cmd.Execf("\\cp -f %s %s", src, path.Join(c.dir, target)) if err := common.CopyFile(src, target); err != nil {
if err != nil { return false, fmt.Errorf("cp file failed, err: %v", err)
return false, fmt.Errorf("cp file failed, stdout: %v, err: %v", stdout, err)
} }
return true, nil return true, nil
} }
@ -73,9 +72,8 @@ func (c localClient) Download(src, target string) (bool, error) {
} }
} }
stdout, err := cmd.Execf("\\cp -f %s %s", localPath, target) if err := common.CopyFile(localPath, target); err != nil {
if err != nil { return false, fmt.Errorf("cp file failed, err: %v", err)
return false, fmt.Errorf("cp file failed, stdout: %v, err: %v", stdout, err)
} }
return true, nil return true, nil
} }

25
backend/utils/common/common.go

@ -6,6 +6,8 @@ import (
"io" "io"
mathRand "math/rand" mathRand "math/rand"
"net" "net"
"os"
"path"
"reflect" "reflect"
"regexp" "regexp"
"sort" "sort"
@ -82,6 +84,29 @@ func GetSortedVersions(versions []string) []string {
return versions return versions
} }
func CopyFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
return err
}
defer source.Close()
if path.Base(src) != path.Base(dst) {
dst = path.Join(dst, path.Base(src))
}
dest, err := os.Create(dst)
if err != nil {
return err
}
defer dest.Close()
_, err = io.Copy(dest, source)
if err != nil {
return err
}
return nil
}
func IsCrossVersion(version1, version2 string) bool { func IsCrossVersion(version1, version2 string) bool {
version1s := strings.Split(version1, ".") version1s := strings.Split(version1, ".")
version2s := strings.Split(version2, ".") version2s := strings.Split(version2, ".")

18
cmd/server/cmd/restore.go

@ -8,7 +8,7 @@ import (
"strings" "strings"
cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd" cmdUtils "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/pkg/errors" "github.com/1Panel-dev/1Panel/backend/utils/common"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -43,19 +43,19 @@ var restoreCmd = &cobra.Command{
tmpPath = path.Join(upgradeDir, tmpPath, "original") tmpPath = path.Join(upgradeDir, tmpPath, "original")
fmt.Printf("(0/4) 开始从 %s 目录回滚 1Panel 服务及数据... \n", tmpPath) fmt.Printf("(0/4) 开始从 %s 目录回滚 1Panel 服务及数据... \n", tmpPath)
if err := cpBinary(path.Join(tmpPath, "1panel"), "/usr/local/bin/1panel"); err != nil { if err := common.CopyFile(path.Join(tmpPath, "1panel"), "/usr/local/bin"); err != nil {
return err return err
} }
fmt.Println("(1/4) 1panel 二进制回滚成功") fmt.Println("(1/4) 1panel 二进制回滚成功")
if err := cpBinary(path.Join(tmpPath, "1pctl"), "/usr/local/bin/1pctl"); err != nil { if err := common.CopyFile(path.Join(tmpPath, "1pctl"), "/usr/local/bin"); err != nil {
return err return err
} }
fmt.Println("(2/4) 1panel 脚本回滚成功") fmt.Println("(2/4) 1panel 脚本回滚成功")
if err := cpBinary(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system/1panel.service"); err != nil { if err := common.CopyFile(path.Join(tmpPath, "1panel.service"), "/etc/systemd/system"); err != nil {
return err return err
} }
fmt.Println("(3/4) 1panel 服务回滚成功") fmt.Println("(3/4) 1panel 服务回滚成功")
if err := cpBinary(path.Join(tmpPath, "1Panel.db"), path.Join(baseDir, "1panel", "db", "1Panel.db")); err != nil { if err := common.CopyFile(path.Join(tmpPath, "1Panel.db"), path.Join(baseDir, "1panel", "db")); err != nil {
return err return err
} }
fmt.Printf("(4/4) 1panel 数据回滚成功 \n\n") fmt.Printf("(4/4) 1panel 数据回滚成功 \n\n")
@ -87,11 +87,3 @@ func loadRestorePath(upgradeDir string) (string, error) {
}) })
return folders[0], nil return folders[0], nil
} }
func cpBinary(src string, dst string) error {
stderr, err := cmdUtils.Exec(fmt.Sprintf("\\cp -f %s %s", src, dst))
if err != nil {
return errors.New(stderr)
}
return nil
}

Loading…
Cancel
Save