Skip to content

Commit

Permalink
pref: 快照增加恢复前资源检查 (#5489)
Browse files Browse the repository at this point in the history
  • Loading branch information
ssongliu authored Jun 18, 2024
1 parent e321045 commit 7e182d3
Show file tree
Hide file tree
Showing 16 changed files with 582 additions and 104 deletions.
2 changes: 2 additions & 0 deletions backend/app/dto/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type OsInfo struct {
PlatformFamily string `json:"platformFamily"`
KernelArch string `json:"kernelArch"`
KernelVersion string `json:"kernelVersion"`

DiskSize int64 `json:"diskSize"`
}

type DashboardCurrent struct {
Expand Down
1 change: 1 addition & 0 deletions backend/app/dto/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type SnapshotInfo struct {
Message string `json:"message"`
CreatedAt time.Time `json:"createdAt"`
Version string `json:"version"`
Size int64 `json:"size"`

InterruptStep string `json:"interruptStep"`
RecoverStatus string `json:"recoverStatus"`
Expand Down
5 changes: 5 additions & 0 deletions backend/app/service/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ func (u *DashboardService) LoadOsInfo() (*dto.OsInfo, error) {
baseInfo.KernelArch = hostInfo.KernelArch
baseInfo.KernelVersion = hostInfo.KernelVersion

diskInfo, err := disk.Usage(global.CONF.System.BaseDir)
if err == nil {
baseInfo.DiskSize = int64(diskInfo.Free)
}

if baseInfo.KernelArch == "armv7l" {
baseInfo.KernelArch = "armv7"
}
Expand Down
55 changes: 48 additions & 7 deletions backend/app/service/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,9 @@ func NewISnapshotService() ISnapshotService {

func (u *SnapshotService) SearchWithPage(req dto.SearchWithPage) (int64, interface{}, error) {
total, systemBackups, err := snapshotRepo.Page(req.Page, req.PageSize, commonRepo.WithLikeName(req.Info))
var dtoSnap []dto.SnapshotInfo
for _, systemBackup := range systemBackups {
var item dto.SnapshotInfo
if err := copier.Copy(&item, &systemBackup); err != nil {
return 0, nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
dtoSnap = append(dtoSnap, item)
dtoSnap, err := loadSnapSize(systemBackups)
if err != nil {
return 0, nil, err
}
return total, dtoSnap, err
}
Expand Down Expand Up @@ -510,3 +506,48 @@ func loadOs() string {
return hostInfo.KernelArch
}
}

func loadSnapSize(records []model.Snapshot) ([]dto.SnapshotInfo, error) {
var datas []dto.SnapshotInfo
clientMap := make(map[string]loadSizeHelper)
var wg sync.WaitGroup
for i := 0; i < len(records); i++ {
var item dto.SnapshotInfo
if err := copier.Copy(&item, &records[i]); err != nil {
return nil, errors.WithMessage(constant.ErrStructTransform, err.Error())
}
itemPath := fmt.Sprintf("system_snapshot/%s.tar.gz", item.Name)
if _, ok := clientMap[records[i].DefaultDownload]; !ok {
backup, err := backupRepo.Get(commonRepo.WithByType(records[i].DefaultDownload))
if err != nil {
global.LOG.Errorf("load backup model %s from db failed, err: %v", records[i].DefaultDownload, err)
clientMap[records[i].DefaultDownload] = loadSizeHelper{}
datas = append(datas, item)
continue
}
client, err := NewIBackupService().NewClient(&backup)
if err != nil {
global.LOG.Errorf("load backup client %s from db failed, err: %v", records[i].DefaultDownload, err)
clientMap[records[i].DefaultDownload] = loadSizeHelper{}
datas = append(datas, item)
continue
}
item.Size, _ = client.Size(path.Join(strings.TrimLeft(backup.BackupPath, "/"), itemPath))
datas = append(datas, item)
clientMap[records[i].DefaultDownload] = loadSizeHelper{backupPath: strings.TrimLeft(backup.BackupPath, "/"), client: client, isOk: true}
continue
}
if clientMap[records[i].DefaultDownload].isOk {
wg.Add(1)
go func(index int) {
item.Size, _ = clientMap[records[index].DefaultDownload].client.Size(path.Join(clientMap[records[index].DefaultDownload].backupPath, itemPath))
datas = append(datas, item)
wg.Done()
}(i)
} else {
datas = append(datas, item)
}
}
wg.Wait()
return datas, nil
}
8 changes: 4 additions & 4 deletions backend/app/service/snapshot_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ func (u *SnapshotService) HandleSnapshotRecover(snap model.Snapshot, isRecover b
if snap.InterruptStep == "Readjson" {
req.IsNew = true
}
if req.IsNew || snap.InterruptStep == "AppData" {
if err := recoverAppData(snapFileDir); err != nil {
if isRecover && (req.IsNew || snap.InterruptStep == "AppData") {
if err := recoverAppData(snapFileDir); err == nil {
updateRecoverStatus(snap.ID, isRecover, "DockerDir", constant.StatusFailed, fmt.Sprintf("handle recover app data failed, err: %v", err))
return
}
Expand Down Expand Up @@ -163,14 +163,14 @@ func backupBeforeRecover(snap model.Snapshot, secret string) error {
_ = os.MkdirAll(path.Join(baseDir, "1panel"), os.ModePerm)
_ = os.MkdirAll(path.Join(baseDir, "docker"), os.ModePerm)

wg.Add(5)
wg.Add(4)
itemHelper.Wg = &wg
go snapJson(itemHelper, jsonItem, baseDir)
go snapPanel(itemHelper, path.Join(baseDir, "1panel"))
go snapDaemonJson(itemHelper, path.Join(baseDir, "docker"))
go snapAppData(itemHelper, path.Join(baseDir, "docker"))
go snapBackup(itemHelper, global.CONF.System.Backup, path.Join(baseDir, "1panel"))
wg.Wait()
itemHelper.Status.AppData = constant.StatusDone

allDone, msg := checkAllDone(status)
if !allDone {
Expand Down
151 changes: 151 additions & 0 deletions cmd/server/docs/docs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7e182d3

Please sign in to comment.