From d5d11256604816c8fc2f6f914812d094f5e7568f Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Mon, 26 Feb 2024 22:45:52 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3=E5=BF=AB=E7=85=A7?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E8=BF=87=E7=A8=8B=E4=B8=AD=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=9B=9E=E6=BB=9A=E6=96=87=E4=BB=B6=E4=B8=8D=E5=AE=8C=E6=95=B4?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=20(#3987)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/service/snapshot.go | 23 ++++++++++++++++++++++- backend/app/service/snapshot_create.go | 14 ++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/backend/app/service/snapshot.go b/backend/app/service/snapshot.go index ac0c4f07e..203267415 100644 --- a/backend/app/service/snapshot.go +++ b/backend/app/service/snapshot.go @@ -234,7 +234,6 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error { } isReTry = false } - _ = u.saveJson(snapJson, rootDir) _, _ = cmd.Exec("systemctl stop docker") if !isReTry || snap.InterruptStep == "DockerDir" { @@ -253,6 +252,7 @@ func (u *SnapshotService) SnapshotRecover(req dto.SnapshotRecover) error { isReTry = false } } + _ = u.saveJson(snapJson, u.OriginalPath) if !isReTry || snap.InterruptStep == "DaemonJson" { 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 { 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": if err := u.handleDockerDatasWithSave(fileOp, "snapshot", "", u.OriginalPath); err != nil { 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")) if err != nil { return errors.New(std) } 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")) if err != nil { return errors.New(std) } 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")) if err != nil { return errors.New(std) diff --git a/backend/app/service/snapshot_create.go b/backend/app/service/snapshot_create.go index 3113cd234..bfd171a62 100644 --- a/backend/app/service/snapshot_create.go +++ b/backend/app/service/snapshot_create.go @@ -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")) - std, err := cmd.Execf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar")) - if err != nil { - snap.Status.AppData = err.Error() - _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": std}) - return + if len(imageSaveList) != 0 { + global.LOG.Debugf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar")) + std, err := cmd.Execf("docker save %s | gzip -c > %s", strings.Join(imageSaveList, " "), path.Join(targetDir, "docker_image.tar")) + if err != nil { + snap.Status.AppData = err.Error() + _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": std}) + return + } } snap.Status.AppData = constant.StatusDone _ = snapshotRepo.UpdateStatus(snap.Status.ID, map[string]interface{}{"app_data": constant.StatusDone})