From e044d92075266fce13198fd30508179bc5e8e84a Mon Sep 17 00:00:00 2001 From: ssongliu <73214554+ssongliu@users.noreply.github.com> Date: Fri, 31 May 2024 17:31:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=BF=AB=E7=85=A7=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=87=E4=BB=BD=E6=96=87=E4=BB=B6=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=80=89=E9=A1=B9=20(#5237)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs #4962 --- backend/app/api/v1/snapshot.go | 4 +-- backend/app/dto/setting.go | 4 +++ backend/app/service/snapshot.go | 32 +++++++++---------- cmd/server/docs/docs.go | 19 ++++++++++- cmd/server/docs/swagger.json | 19 ++++++++++- cmd/server/docs/swagger.yaml | 13 +++++++- frontend/src/api/modules/setting.ts | 2 +- frontend/src/lang/modules/en.ts | 2 ++ frontend/src/lang/modules/tw.ts | 1 + frontend/src/lang/modules/zh.ts | 1 + frontend/src/views/setting/snapshot/index.vue | 16 ++++++++-- 11 files changed, 88 insertions(+), 25 deletions(-) diff --git a/backend/app/api/v1/snapshot.go b/backend/app/api/v1/snapshot.go index 44bf1ceb2..3f79be8ac 100644 --- a/backend/app/api/v1/snapshot.go +++ b/backend/app/api/v1/snapshot.go @@ -168,13 +168,13 @@ func (b *BaseApi) RollbackSnapshot(c *gin.Context) { // @Summary Delete system backup // @Description 删除系统快照 // @Accept json -// @Param request body dto.BatchDeleteReq true "request" +// @Param request body dto.SnapshotBatchDelete true "request" // @Success 200 // @Security ApiKeyAuth // @Router /settings/snapshot/del [post] // @x-panel-log {"bodyKeys":["ids"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"ids","isList":true,"db":"snapshots","output_column":"name","output_value":"name"}],"formatZH":"删除系统快照 [name]","formatEN":"Delete system backup [name]"} func (b *BaseApi) DeleteSnapshot(c *gin.Context) { - var req dto.BatchDeleteReq + var req dto.SnapshotBatchDelete if err := helper.CheckBindAndValidate(&req, c); err != nil { return } diff --git a/backend/app/dto/setting.go b/backend/app/dto/setting.go index 866fbe347..8af4fdcce 100644 --- a/backend/app/dto/setting.go +++ b/backend/app/dto/setting.go @@ -122,6 +122,10 @@ type SnapshotRecover struct { ReDownload bool `json:"reDownload"` ID uint `json:"id" validate:"required"` } +type SnapshotBatchDelete struct { + DeleteWithFile bool `json:"deleteWithFile"` + Ids []uint `json:"ids" validate:"required"` +} type SnapshotImport struct { From string `json:"from"` Names []string `json:"names"` diff --git a/backend/app/service/snapshot.go b/backend/app/service/snapshot.go index 828944616..667920a31 100644 --- a/backend/app/service/snapshot.go +++ b/backend/app/service/snapshot.go @@ -32,7 +32,7 @@ type ISnapshotService interface { SnapshotRecover(req dto.SnapshotRecover) error SnapshotRollback(req dto.SnapshotRecover) error SnapshotImport(req dto.SnapshotImport) error - Delete(req dto.BatchDeleteReq) error + Delete(req dto.SnapshotBatchDelete) error LoadSnapShotStatus(id uint) (*dto.SnapshotStatus, error) @@ -323,25 +323,23 @@ func (u *SnapshotService) HandleSnapshot(isCronjob bool, logPath string, req dto return snap.Name, nil } -func (u *SnapshotService) Delete(req dto.BatchDeleteReq) error { - backups, _ := snapshotRepo.GetList(commonRepo.WithIdsIn(req.Ids)) - localDir, err := loadLocalDir() - if err != nil { - return err - } - for _, snap := range backups { - itemFile := path.Join(localDir, "system", snap.Name) - _ = os.RemoveAll(itemFile) - - itemTarFile := path.Join(global.CONF.System.TmpDir, "system", snap.Name+".tar.gz") - _ = os.Remove(itemTarFile) +func (u *SnapshotService) Delete(req dto.SnapshotBatchDelete) error { + snaps, _ := snapshotRepo.GetList(commonRepo.WithIdsIn(req.Ids)) + for _, snap := range snaps { + targetAccounts, err := loadClientMap(snap.From) + if err != nil { + return err + } + for _, item := range targetAccounts { + global.LOG.Debugf("remove snapshot file %s.tar.gz from %s", snap.Name, item.backType) + _, _ = item.client.Delete(path.Join(item.backupPath, "system_snapshot", snap.Name+".tar.gz")) + } _ = snapshotRepo.DeleteStatus(snap.ID) + if err := snapshotRepo.Delete(commonRepo.WithByID(snap.ID)); err != nil { + return err + } } - if err := snapshotRepo.Delete(commonRepo.WithIdsIn(req.Ids)); err != nil { - return err - } - return nil } diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index 46a83bd82..7e178039b 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -10501,7 +10501,7 @@ const docTemplate = `{ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.BatchDeleteReq" + "$ref": "#/definitions/dto.SnapshotBatchDelete" } } ], @@ -18607,6 +18607,23 @@ const docTemplate = `{ } } }, + "dto.SnapshotBatchDelete": { + "type": "object", + "required": [ + "ids" + ], + "properties": { + "deleteWithFile": { + "type": "boolean" + }, + "ids": { + "type": "array", + "items": { + "type": "integer" + } + } + } + }, "dto.SnapshotCreate": { "type": "object", "required": [ diff --git a/cmd/server/docs/swagger.json b/cmd/server/docs/swagger.json index 6f7548fa4..4369ae461 100644 --- a/cmd/server/docs/swagger.json +++ b/cmd/server/docs/swagger.json @@ -10494,7 +10494,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/dto.BatchDeleteReq" + "$ref": "#/definitions/dto.SnapshotBatchDelete" } } ], @@ -18600,6 +18600,23 @@ } } }, + "dto.SnapshotBatchDelete": { + "type": "object", + "required": [ + "ids" + ], + "properties": { + "deleteWithFile": { + "type": "boolean" + }, + "ids": { + "type": "array", + "items": { + "type": "integer" + } + } + } + }, "dto.SnapshotCreate": { "type": "object", "required": [ diff --git a/cmd/server/docs/swagger.yaml b/cmd/server/docs/swagger.yaml index d81d2367c..7269337ac 100644 --- a/cmd/server/docs/swagger.yaml +++ b/cmd/server/docs/swagger.yaml @@ -2744,6 +2744,17 @@ definitions: required: - key type: object + dto.SnapshotBatchDelete: + properties: + deleteWithFile: + type: boolean + ids: + items: + type: integer + type: array + required: + - ids + type: object dto.SnapshotCreate: properties: defaultDownload: @@ -11846,7 +11857,7 @@ paths: name: request required: true schema: - $ref: '#/definitions/dto.BatchDeleteReq' + $ref: '#/definitions/dto.SnapshotBatchDelete' responses: "200": description: OK diff --git a/frontend/src/api/modules/setting.ts b/frontend/src/api/modules/setting.ts index fdee4e3c7..8594646ab 100644 --- a/frontend/src/api/modules/setting.ts +++ b/frontend/src/api/modules/setting.ts @@ -183,7 +183,7 @@ export const snapshotImport = (param: Setting.SnapshotImport) => { export const updateSnapshotDescription = (param: DescriptionUpdate) => { return http.post(`/settings/snapshot/description/update`, param); }; -export const snapshotDelete = (param: { ids: number[] }) => { +export const snapshotDelete = (param: { ids: number[]; deleteWithFile: boolean }) => { return http.post(`/settings/snapshot/del`, param); }; export const snapshotRecover = (param: Setting.SnapshotRecover) => { diff --git a/frontend/src/lang/modules/en.ts b/frontend/src/lang/modules/en.ts index 3e01d59d8..d92a170a4 100644 --- a/frontend/src/lang/modules/en.ts +++ b/frontend/src/lang/modules/en.ts @@ -1472,6 +1472,8 @@ const message = { 'Backup files not in the current backup list, please try downloading from the file directory and importing for backup.', snapshot: 'Snapshot', + deleteHelper: + 'All backup files for the snapshot, including those in the third-party backup account, will be deleted.', status: 'Snapshot status', ignoreRule: 'Ignore Rule', ignoreHelper: diff --git a/frontend/src/lang/modules/tw.ts b/frontend/src/lang/modules/tw.ts index 3c6643e39..75b67587d 100644 --- a/frontend/src/lang/modules/tw.ts +++ b/frontend/src/lang/modules/tw.ts @@ -1298,6 +1298,7 @@ const message = { backupJump: '未在當前備份列表中的備份檔案,請嘗試從檔案目錄中下載後導入備份。', snapshot: '快照', + deleteHelper: '將刪除該快照的所有備份文件,包括第三方備份賬號中的文件。', status: '快照狀態', ignoreRule: '排除規則', ignoreHelper: '快照時將使用該規則對 1Panel 數據目錄進行壓縮備份,請謹慎修改。', diff --git a/frontend/src/lang/modules/zh.ts b/frontend/src/lang/modules/zh.ts index 33372e9be..6b7754778 100644 --- a/frontend/src/lang/modules/zh.ts +++ b/frontend/src/lang/modules/zh.ts @@ -1299,6 +1299,7 @@ const message = { backupJump: '未在当前备份列表中的备份文件,请尝试从文件目录中下载后导入备份。', snapshot: '快照', + deleteHelper: '将删除该快照的所有备份文件,包括第三方备份账号中的文件。', ignoreRule: '排除规则', ignoreHelper: '快照时将使用该规则对 1Panel 数据目录进行压缩备份,请谨慎修改。', ignoreHelper1: '一行一个,例: \n*.log\n/opt/1panel/cache', diff --git a/frontend/src/views/setting/snapshot/index.vue b/frontend/src/views/setting/snapshot/index.vue index e4752bf06..0395b21ae 100644 --- a/frontend/src/views/setting/snapshot/index.vue +++ b/frontend/src/views/setting/snapshot/index.vue @@ -164,7 +164,18 @@ - + + + @@ -221,6 +232,7 @@ let snapInfo = reactive({ fromAccounts: [], description: '', }); +const cleanData = ref(); const drawerVisible = ref(false); @@ -332,7 +344,7 @@ const batchDelete = async (row: Setting.SnapshotInfo | null) => { i18n.global.t('commons.button.delete'), ]), api: snapshotDelete, - params: { ids: ids }, + params: { ids: ids, deleteWithFile: cleanData.value }, }); };