diff --git a/agent/app/service/backup_record.go b/agent/app/service/backup_record.go index 4c1e93f51..f44dbbeb2 100644 --- a/agent/app/service/backup_record.go +++ b/agent/app/service/backup_record.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "path" - "strings" "sync" "github.com/1Panel-dev/1Panel/agent/app/dto" @@ -194,7 +193,7 @@ func (u *BackupRecordService) ListFiles(req dto.OperateByID) []string { var datas []string for _, file := range files { fileName := path.Base(file) - if len(file) != 0 && (strings.HasPrefix(fileName, "1panel-v2.") || strings.HasPrefix(fileName, "snapshot-1panel-v2.")) { + if len(file) != 0 && checkSnapshotIsOk(fileName) { datas = append(datas, path.Base(file)) } } diff --git a/agent/app/service/snapshot.go b/agent/app/service/snapshot.go index 65a56e262..e94b33676 100644 --- a/agent/app/service/snapshot.go +++ b/agent/app/service/snapshot.go @@ -68,7 +68,7 @@ func (u *SnapshotService) SnapshotImport(req dto.SnapshotImport) error { } for _, snapName := range req.Names { - if !strings.HasPrefix(snapName, "1panel-v2.") && !strings.HasPrefix(snapName, "snapshot-1panel-v2.") { + if !checkSnapshotIsOk(snapName) { return fmt.Errorf("incorrect snapshot name format of %s", snapName) } snap, _ := snapshotRepo.Get(repo.WithByName(strings.ReplaceAll(snapName, ".tar.gz", ""))) @@ -77,11 +77,11 @@ func (u *SnapshotService) SnapshotImport(req dto.SnapshotImport) error { } } for _, snap := range req.Names { - shortName := strings.TrimPrefix(snap, "snapshot_") - nameItems := strings.Split(shortName, "-") - if !strings.HasPrefix(shortName, "1panel-v") || !strings.HasSuffix(shortName, ".tar.gz") || len(nameItems) < 3 { - return fmt.Errorf("incorrect snapshot name format of %s", shortName) - } + shortName := strings.ReplaceAll(snap, "snapshot-", "") + shortName = strings.ReplaceAll(shortName, "1panel-", "") + shortName = strings.ReplaceAll(shortName, "core-", "") + shortName = strings.ReplaceAll(shortName, "agent-", "") + nameItems := strings.Split(shortName, "-linux") if strings.HasSuffix(snap, ".tar.gz") { snap = strings.ReplaceAll(snap, ".tar.gz", "") } @@ -89,7 +89,7 @@ func (u *SnapshotService) SnapshotImport(req dto.SnapshotImport) error { Name: snap, SourceAccountIDs: fmt.Sprintf("%v", req.BackupAccountID), DownloadAccountID: req.BackupAccountID, - Version: nameItems[1], + Version: nameItems[0], Description: req.Description, Status: constant.StatusSuccess, } @@ -403,3 +403,13 @@ func loadFile(pathItem string, index int, fileOp fileUtils.FileOp) ([]dto.DataTr } return data, nil } + +func checkSnapshotIsOk(name string) bool { + names := []string{"1panel-core-v2.", "1panel-agent-v2.", "1panel-v2.", "snapshot-1panel-core-v2.", "snapshot-1panel-agent-v2."} + for _, item := range names { + if strings.HasPrefix(name, item) { + return true + } + } + return false +} diff --git a/agent/app/service/snapshot_create.go b/agent/app/service/snapshot_create.go index 5a570b6b7..7e48a1354 100644 --- a/agent/app/service/snapshot_create.go +++ b/agent/app/service/snapshot_create.go @@ -34,7 +34,7 @@ func (u *SnapshotService) SnapshotCreate(parentTask *task.Task, req dto.Snapshot scope = "agent" } if jobID == 0 { - req.Name = fmt.Sprintf("1panel-%s-%s-linux-%s-%s", versionItem.Value, scope, loadOs(), time.Now().Format(constant.DateTimeSlimLayout)) + req.Name = fmt.Sprintf("1panel-%s-%s-linux-%s-%s", scope, versionItem.Value, loadOs(), time.Now().Format(constant.DateTimeSlimLayout)) } appItem, _ := json.Marshal(req.AppData) panelItem, _ := json.Marshal(req.PanelData) diff --git a/agent/app/service/snapshot_recover.go b/agent/app/service/snapshot_recover.go index 87642f54c..e68be6699 100644 --- a/agent/app/service/snapshot_recover.go +++ b/agent/app/service/snapshot_recover.go @@ -38,6 +38,18 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error { _ = snapshotRepo.Update(snap.ID, map[string]interface{}{"recover_status": constant.StatusFailed, "recover_message": errInfo}) return errors.New(errInfo) } + if !strings.Contains(snap.Name, "-v2.") { + return errors.New("snapshots are currently not supported for recovery across major versions") + } + if !strings.Contains(snap.Name, "-core") && !strings.Contains(snap.Name, "-agent") { + return errors.New("the name of the snapshot file does not conform to the format") + } + if strings.Contains(snap.Name, "-core") && !global.IsMaster { + return errors.New("the snapshot of the master node cannot be restored on the agent nodes") + } + if strings.Contains(snap.Name, "-agent") && global.IsMaster { + return errors.New("the snapshot of the agent node cannot be restored on the master node") + } if len(snap.RollbackStatus) != 0 && snap.RollbackStatus != constant.StatusSuccess { req.IsNew = true }