(req dto.CommonBackup, parentTask *task.Task, recordID uint, backupDir, fileName string)
| 109 | } |
| 110 | |
| 111 | func handleComposeBackup(req dto.CommonBackup, parentTask *task.Task, recordID uint, backupDir, fileName string) error { |
| 112 | composeCtx, err := newComposeBackupContext(req, backupDir, fileName) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | containerNames, err := loadComposeContainerNames(composeCtx) |
| 117 | if err != nil { |
| 118 | composeCtx.close() |
| 119 | return err |
| 120 | } |
| 121 | |
| 122 | backupTask := parentTask |
| 123 | if backupTask == nil { |
| 124 | backupTask, err = task.NewTaskWithOps(composeCtx.composeName, task.TaskBackup, task.TaskScopeBackup, req.TaskID, 1) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
| 128 | } |
| 129 | if req.StopBefore { |
| 130 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupStop"), func(t *task.Task) error { |
| 131 | return stepStopComposeForBackup(composeCtx) |
| 132 | }, func(t *task.Task) { |
| 133 | _ = stepStartComposeAfterBackup(composeCtx) |
| 134 | }, 3, time.Hour) |
| 135 | } |
| 136 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupPrepare"), func(t *task.Task) error { return stepPrepareComposeBackup(composeCtx) }, nil, 3, time.Hour) |
| 137 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupFiles"), func(t *task.Task) error { return stepBackupComposeFiles(composeCtx) }, nil, 3, time.Hour) |
| 138 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupContainers"), func(t *task.Task) error { return nil }, nil, 3, time.Hour) |
| 139 | for _, containerName := range containerNames { |
| 140 | backupFileName := fmt.Sprintf("%s.tar.gz", sanitizeComposeFileName(containerName)) |
| 141 | backupFile := path.Join(composeCtx.tmpDir, "containers", backupFileName) |
| 142 | if err := handleContainerBackup(containerName, backupTask, 0, path.Dir(backupFile), path.Base(backupFile), "", "", false); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | composeCtx.meta.Containers = append(composeCtx.meta.Containers, path.Join("containers", backupFileName)) |
| 146 | } |
| 147 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupMeta"), func(t *task.Task) error { return stepWriteComposeBackupMeta(composeCtx) }, nil, 3, time.Hour) |
| 148 | backupTask.AddSubTaskWithOps(task.GetTaskName(composeCtx.composeName, task.TaskBackup, task.TaskScopeBackup), func(t *task.Task) error { |
| 149 | return stepPackComposeBackup(composeCtx) |
| 150 | }, nil, 3, time.Hour) |
| 151 | if req.StopBefore { |
| 152 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupStart"), func(t *task.Task) error { |
| 153 | return stepStartComposeAfterBackup(composeCtx) |
| 154 | }, nil, 3, time.Hour) |
| 155 | } |
| 156 | backupTask.AddSubTaskWithOps(i18n.GetMsgByKey("ComposeBackupCleanup"), func(t *task.Task) error { |
| 157 | composeCtx.close() |
| 158 | return nil |
| 159 | }, nil, 0, time.Hour) |
| 160 | if parentTask != nil { |
| 161 | return nil |
| 162 | } |
| 163 | go func() { |
| 164 | defer composeCtx.close() |
| 165 | if err := backupTask.Execute(); err != nil { |
| 166 | backupRepo.UpdateRecordByMap(recordID, map[string]interface{}{"status": constant.StatusFailed, "message": err.Error()}) |
| 167 | return |
| 168 | } |
no test coverage detected