diff --git a/backend/app/service/cronjob_helper.go b/backend/app/service/cronjob_helper.go index 91bd0834e..44b43435a 100644 --- a/backend/app/service/cronjob_helper.go +++ b/backend/app/service/cronjob_helper.go @@ -167,24 +167,32 @@ func (u *CronjobService) handleBackup(cronjob *model.Cronjob, startTime time.Tim func (u *CronjobService) HandleRmExpired(backType, backupPath, localDir string, cronjob *model.Cronjob, backClient cloud_storage.CloudStorageClient) { global.LOG.Infof("start to handle remove expired, retain copies: %d", cronjob.RetainCopies) records, _ := cronjobRepo.ListRecord(cronjobRepo.WithByJobID(int(cronjob.ID)), commonRepo.WithOrderBy("created_at desc")) - if len(records) > int(cronjob.RetainCopies) { - for i := int(cronjob.RetainCopies); i < len(records); i++ { - if len(records[i].File) != 0 { - files := strings.Split(records[i].File, ",") - for _, file := range files { - if backType != "LOCAL" { - _, _ = backClient.Delete(backupPath + "/" + strings.TrimPrefix(file, localDir+"/")) - _ = os.Remove(file) - } else { - _ = os.Remove(file) - } - _ = backupRepo.DeleteRecord(context.TODO(), backupRepo.WithByFileName(path.Base(file))) + if len(records) <= int(cronjob.RetainCopies) { + return + } + for i := int(cronjob.RetainCopies); i < len(records); i++ { + if len(records[i].File) != 0 { + files := strings.Split(records[i].File, ",") + for _, file := range files { + _ = os.Remove(file) + _ = backupRepo.DeleteRecord(context.TODO(), backupRepo.WithByFileName(path.Base(file))) + if backType == "LOCAL" { + continue } - } - _ = cronjobRepo.DeleteRecord(commonRepo.WithByID(uint(records[i].ID))) - _ = os.Remove(records[i].Records) + fileItem := file + if cronjob.KeepLocal { + if len(backupPath) != 0 { + itemPath := strings.TrimPrefix(backupPath, "/") + itemPath = strings.TrimSuffix(itemPath, "/") + "/" + fileItem = itemPath + strings.TrimPrefix(file, localDir+"/") + } + } + _, _ = backClient.Delete(fileItem) + } } + _ = cronjobRepo.DeleteRecord(commonRepo.WithByID(uint(records[i].ID))) + _ = os.Remove(records[i].Records) } } diff --git a/backend/utils/cloud_storage/client/onedrive.go b/backend/utils/cloud_storage/client/onedrive.go index 458a1a6c9..c1f8a877a 100644 --- a/backend/utils/cloud_storage/client/onedrive.go +++ b/backend/utils/cloud_storage/client/onedrive.go @@ -67,14 +67,14 @@ func (onedrive oneDriveClient) Exist(path string) (bool, error) { func (onedrive oneDriveClient) Delete(path string) (bool, error) { path = "/" + strings.TrimPrefix(path, "/") - fileID, err := onedrive.loadIDByPath(path) + req, err := onedrive.client.NewRequest("DELETE", fmt.Sprintf("me/drive/root:%s", path), nil) if err != nil { - return false, err + return false, fmt.Errorf("new request for delete file failed, err: %v \n", err) + } + if err := onedrive.client.Do(context.Background(), req, false, nil); err != nil { + return false, fmt.Errorf("do request for delete file failed, err: %v \n", err) } - if err := onedrive.client.DriveItems.Delete(context.Background(), "", fileID); err != nil { - return false, err - } return true, nil } @@ -205,11 +205,11 @@ func (onedrive *oneDriveClient) ListObjects(prefix string) ([]interface{}, error req, err := onedrive.client.NewRequest("GET", fmt.Sprintf("me/drive/items/%s/children", folderID), nil) if err != nil { - return nil, fmt.Errorf("new request for delete failed, err: %v", err) + return nil, fmt.Errorf("new request for list failed, err: %v", err) } var driveItems *odsdk.OneDriveDriveItemsResponse if err := onedrive.client.Do(context.Background(), req, false, &driveItems); err != nil { - return nil, fmt.Errorf("do request for delete failed, err: %v", err) + return nil, fmt.Errorf("do request for list failed, err: %v", err) } for _, item := range driveItems.DriveItems { return nil, fmt.Errorf("id: %v, name: %s \n", item.Id, item.Name)