-
Notifications
You must be signed in to change notification settings - Fork 3.2k
fix: Fix the problem that the website directory is not backed up duri… #8280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
| "strings" | ||
| "sync" | ||
|
|
||
| "github.com/1Panel-dev/1Panel/agent/app/repo" | ||
|
|
||
|
|
@@ -19,7 +17,6 @@ import ( | |
| "github.com/1Panel-dev/1Panel/agent/i18n" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/cmd" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/common" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/compose" | ||
| "github.com/1Panel-dev/1Panel/agent/utils/files" | ||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
@@ -160,6 +157,17 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error { | |
| itemHelper.Task.LogStart(i18n.GetWithName("RecoverPanelData", snap.Name)) | ||
| err := itemHelper.FileOp.TarGzExtractPro(path.Join(rootDir, snap.Name, "/1panel_data.tar.gz"), path.Join(snapJson.BaseDir, "1panel"), "") | ||
| itemHelper.Task.LogWithStatus(i18n.GetMsgByKey("Decompress"), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(snapJson.OperestyDir) != 0 { | ||
| err := itemHelper.FileOp.TarGzExtractPro(path.Join(rootDir, snap.Name, "/website.tar.gz"), snapJson.OperestyDir, "") | ||
| itemHelper.Task.LogWithStatus(i18n.GetMsgByKey("RecoverWebsite"), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return err | ||
| }, | ||
| nil, | ||
|
|
@@ -219,11 +227,24 @@ func backupBeforeRecover(name string, itemHelper *snapRecoverHelper) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
| err = itemHelper.FileOp.CopyDirWithExclude(global.Dir.LocalBackupDir, rootDir, []string{"system_snapshot"}) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", global.Dir.LocalBackupDir), err) | ||
| if err != nil { | ||
| return err | ||
|
|
||
| openrestyDir, _ := settingRepo.GetValueByKey("WEBSITE_DIR") | ||
| if len(openrestyDir) != 0 && !strings.Contains(openrestyDir, global.Dir.DataDir) { | ||
| err := itemHelper.FileOp.CopyDirWithExclude(openrestyDir, rootDir, nil) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", openrestyDir), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| if len(global.Dir.LocalBackupDir) != 0 && !strings.Contains(global.Dir.LocalBackupDir, global.Dir.DataDir) { | ||
| err = itemHelper.FileOp.CopyDirWithExclude(global.Dir.LocalBackupDir, rootDir, []string{"system_snapshot"}) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", global.Dir.LocalBackupDir), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| err = itemHelper.FileOp.CopyFile("/usr/local/bin/1pctl", baseDir) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/usr/local/bin/1pctl"), err) | ||
| if err != nil { | ||
|
|
@@ -235,19 +256,17 @@ func backupBeforeRecover(name string, itemHelper *snapRecoverHelper) error { | |
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| err = itemHelper.FileOp.CopyFile("/usr/local/bin/1panel-agent", baseDir) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/usr/local/bin/1panel-agent"), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if global.IsMaster { | ||
| err = itemHelper.FileOp.CopyFile("/etc/systemd/system/1panel-core.service", baseDir) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-core.service"), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| err = itemHelper.FileOp.CopyFile("/usr/local/bin/1panel-agent", baseDir) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/usr/local/bin/1panel-agent"), err) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| err = itemHelper.FileOp.CopyFile("/etc/systemd/system/1panel-agent.service", baseDir) | ||
| itemHelper.Task.LogWithStatus(i18n.GetWithName("SnapCopy", "/etc/systemd/system/1panel-agent.service"), err) | ||
| if err != nil { | ||
|
|
@@ -294,35 +313,13 @@ func recoverAppData(src string, itemHelper *snapRecoverHelper) error { | |
| if _, err := os.Stat(path.Join(src, "images.tar.gz")); err != nil { | ||
| itemHelper.Task.Log(i18n.GetMsgByKey("RecoverAppEmpty")) | ||
| return nil | ||
| } else { | ||
| std, err := cmd.Execf("docker load < %s", path.Join(src, "images.tar.gz")) | ||
| if err != nil { | ||
| itemHelper.Task.LogFailedWithErr(i18n.GetMsgByKey("RecoverAppImage"), errors.New(std)) | ||
| return fmt.Errorf("docker load images failed, err: %v", err) | ||
| } | ||
| itemHelper.Task.LogSuccess(i18n.GetMsgByKey("RecoverAppImage")) | ||
| } | ||
|
|
||
| appInstalls, err := appInstallRepo.ListBy(context.Background()) | ||
| itemHelper.Task.LogWithStatus(i18n.GetMsgByKey("RecoverAppList"), err) | ||
| std, err := cmd.Execf("docker load < %s", path.Join(src, "images.tar.gz")) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
| for i := 0; i < len(appInstalls); i++ { | ||
| wg.Add(1) | ||
| appInstalls[i].Status = constant.StatusRebuilding | ||
| _ = appInstallRepo.Save(context.Background(), &appInstalls[i]) | ||
| go func(app model.AppInstall) { | ||
| defer wg.Done() | ||
| dockerComposePath := app.GetComposePath() | ||
| _, _ = compose.Up(dockerComposePath) | ||
| app.Status = constant.StatusRunning | ||
| _ = appInstallRepo.Save(context.Background(), &app) | ||
| }(appInstalls[i]) | ||
| itemHelper.Task.LogFailedWithErr(i18n.GetMsgByKey("RecoverAppImage"), errors.New(std)) | ||
| return fmt.Errorf("docker load images failed, err: %v", err) | ||
| } | ||
| wg.Wait() | ||
| itemHelper.Task.LogSuccess(i18n.GetMsgByKey("RecoverAppImage")) | ||
| return nil | ||
| } | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are several areas in the provided code where changes can be made to improve readability, performance, and maintainability:
Here is an improved version of the code snippet incorporating some of these suggestions: package service
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/1Panel-dev/1Panel/agent/app/repo"
"github.com/1Panel-dev/1Panel/i18n"
"github.com/1Panel-dev/1Panel/utils/cmd"
"github.com/1Panel-dev/1Panel/utils/constants"
)
const systemSnapshot folderNameString = "system_snapshot"
const defaultExclusionsForCopying = []string{
systemSnapshotFolderName,
}
type snapRecoverHelper struct {
Task taskHelperInterface
FileOp fileOperationInterface
}
func (u *snapshotService) SnapshotRecover(req dto.SnapshotRecover) error {
rootDir := req.GetRootDir()
snapJson := req.GetResponse()
if snappy, err := u.repo.SnapGetOne(context.Background(), req.GetSnapID()); err == nil || err.Error() != constants.ErrSnapNotExists.Error() {
itemHelper.Task.LogStart(i18n.GetWithName("RecoverPanelData", snap.GetName()))
err := itemHelper.FileOp.UnTar(path.Join(rootDir, snap.GetName(), "/1panel_data.tar.gz"), filepath.Join(snappy.BaseDir, "1panel"))
itemHelper.Task.LogWithStatus(i18n.GetMsgByName("Decompress"), err)
if err != nil {
return fmt.Errorf("decompress panel data archive failed: %w", err)
}
if len(snappy.OperestyDir) > 0 && dirNotEmpty(snappy.OperestyDir) {
itemHelper.Task.LogStart(i18n.GetMsgByName("RecoverWebsite", snappy.OperestyDir))
err := itemHelper.FileOp.UnTar(path.Join(rootDir, snap.GetName(), "/website.tar.gz"), snappy.OperestyDir, nil)
itemHelper.Task.LogWithStatus(i18n.GetMsgByName("RecoverWebsiteComplete"), err)
if err != nil {
return fmt.Errorf("decompress website content archive failed: %w", err)
}
}
} else {
return err
}
baseDir := req.GetResponse().BaseDir
return backupBeforeRecover(req.GetSnapName(), itemHelper)
}
// ... rest of the modified code ...Note that this example assumes you have other utility functions like |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few modifications and optimizations I can suggest:
taskItemis not null before calling its methods to avoid runtime errors.existStr, especially on larger sets of data.Other than these minor improvements, the general structure and functionality appear to be working correctly.