fix: 解决快照恢复过程中生成回滚文件不完整的问题 (#3987)

pull/3989/head
ssongliu 2024-02-26 22:45:52 +08:00 committed by GitHub
parent 08801ebfcd
commit d5d1125660
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 7 deletions

View File

@ -234,7 +234,6 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error {
} }
isReTry = false isReTry = false
} }
_ = u.saveJson(snapJson, rootDir)
_, _ = cmd.Exec("systemctl stop docker") _, _ = cmd.Exec("systemctl stop docker")
if !isReTry || snap.InterruptStep == "DockerDir" { if !isReTry || snap.InterruptStep == "DockerDir" {
@ -253,6 +252,7 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error {
isReTry = false isReTry = false
} }
} }
_ = u.saveJson(snapJson, u.OriginalPath)
if !isReTry || snap.InterruptStep == "DaemonJson" { if !isReTry || snap.InterruptStep == "DaemonJson" {
if err := u.handleDaemonJson(fileOp, operation, rootDir+"/docker/daemon.json", u.OriginalPath); err != nil { if err := u.handleDaemonJson(fileOp, operation, rootDir+"/docker/daemon.json", u.OriginalPath); err != nil {
@ -625,20 +625,41 @@ func (u *SnapshotService) handleDockerDatas(fileOp files.FileOp, operation strin
func (u *SnapshotService) handleDockerDatasWithSave(fileOp files.FileOp, operation, source, target string) error { func (u *SnapshotService) handleDockerDatasWithSave(fileOp files.FileOp, operation, source, target string) error {
switch operation { switch operation {
case "snapshot":
var wg sync.WaitGroup
wg.Add(1)
var status model.SnapshotStatus
go snapAppData(snapHelper{Wg: &wg, Status: &status}, u.OriginalPath)
wg.Wait()
if status.AppData != constant.StatusDone {
return errors.New(status.AppData)
}
case "recover": case "recover":
if err := u.handleDockerDatasWithSave(fileOp, "snapshot", "", u.OriginalPath); err != nil { if err := u.handleDockerDatasWithSave(fileOp, "snapshot", "", u.OriginalPath); err != nil {
return fmt.Errorf("backup docker data failed, err: %v", err) return fmt.Errorf("backup docker data failed, err: %v", err)
} }
if _, err := os.Stat(path.Join(source, "docker/docker_image.tar")); err != nil {
global.LOG.Debug("no such docker images in snapshot")
return nil
}
std, err := cmd.Execf("docker load < %s", path.Join(source, "docker/docker_image.tar")) std, err := cmd.Execf("docker load < %s", path.Join(source, "docker/docker_image.tar"))
if err != nil { if err != nil {
return errors.New(std) return errors.New(std)
} }
case "re-recover": case "re-recover":
if _, err := os.Stat(path.Join(source, "docker/docker_image.tar")); err != nil {
global.LOG.Debug("no such docker images in snapshot")
return nil
}
std, err := cmd.Execf("docker load < %s", path.Join(source, "docker/docker_image.tar")) std, err := cmd.Execf("docker load < %s", path.Join(source, "docker/docker_image.tar"))
if err != nil { if err != nil {
return errors.New(std) return errors.New(std)
} }
case "rollback": case "rollback":
if _, err := os.Stat(path.Join(source, "docker/docker_image.tar")); err != nil {
global.LOG.Debug("no such docker images in snapshot")
return nil
}
std, err := cmd.Execf("docker load < %s", path.Join(source, "docker_image.tar")) std, err := cmd.Execf("docker load < %s", path.Join(source, "docker_image.tar"))
if err != nil { if err != nil {
return errors.New(std) return errors.New(std)

View File

@ -106,12 +106,14 @@ func snapAppData(snap snapHelper, targetDir string) {
} }
} }
global.LOG.Debugf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar")) if len(imageSaveList) != 0 {
std, err := cmd.Execf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar")) global.LOG.Debugf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar"))
if err != nil { std, err := cmd.Execf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar"))
snap.Status.AppData = err.Error() if err != nil {
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": std}) snap.Status.AppData = err.Error()
return _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": std})
return
}
} }
snap.Status.AppData = constant.StatusDone snap.Status.AppData = constant.StatusDone
_ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": constant.StatusDone}) _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": constant.StatusDone})