diff --git a/backend/app/service/backup.go b/backend/app/service/backup.go index cc84b56fa..a5b5ab1cf 100644 --- a/backend/app/service/backup.go +++ b/backend/app/service/backup.go @@ -37,6 +37,7 @@ type IBackupService interface { GetBuckets(backupDto dto.ForBuckets) ([]interface{}, error) Update(ireq dto.BackupOperate) error Delete(id uint) error + DeleteRecordByName(backupType, name, detailName string, withDeleteFile bool) error BatchDeleteRecord(ids []uint) error NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error) @@ -260,6 +261,35 @@ func (u *BackupService) Delete(id uint) error { return backupRepo.Delete(commonRepo.WithByID(id)) } +func (u *BackupService) DeleteRecordByName(backupType, name, detailName string, withDeleteFile bool) error { + if !withDeleteFile { + return backupRepo.DeleteRecord(context.Background(), commonRepo.WithByType(backupType), commonRepo.WithByName(name), backupRepo.WithByDetailName(detailName)) + } + + records, err := backupRepo.ListRecord(commonRepo.WithByType(backupType), commonRepo.WithByName(name), backupRepo.WithByDetailName(detailName)) + if err != nil { + return err + } + + for _, record := range records { + backupAccount, err := backupRepo.Get(commonRepo.WithByType(record.Source)) + if err != nil { + global.LOG.Errorf("load backup account %s info from db failed, err: %v", record.Source, err) + continue + } + client, err := u.NewClient(&backupAccount) + if err != nil { + global.LOG.Errorf("new client for backup account %s failed, err: %v", record.Source, err) + continue + } + if _, err = client.Delete(path.Join(record.FileDir, record.FileName)); err != nil { + global.LOG.Errorf("remove file %s from %s failed, err: %v", path.Join(record.FileDir, record.FileName), record.Source, err) + } + _ = backupRepo.DeleteRecord(context.Background(), commonRepo.WithByID(record.ID)) + } + return nil +} + func (u *BackupService) BatchDeleteRecord(ids []uint) error { records, err := backupRepo.ListRecord(commonRepo.WithIdsIn(ids)) if err != nil { diff --git a/backend/app/service/website.go b/backend/app/service/website.go index ac5512d1a..35dcca161 100644 --- a/backend/app/service/website.go +++ b/backend/app/service/website.go @@ -454,7 +454,8 @@ func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error { tx, ctx := helper.GetTxAndContext() defer tx.Rollback() - _ = backupRepo.DeleteRecord(ctx, commonRepo.WithByType("website"), commonRepo.WithByName(website.Alias)) + + go NewIBackupService().DeleteRecordByName("website", website.PrimaryDomain, website.Alias, req.DeleteBackup) if err := websiteRepo.DeleteBy(ctx, commonRepo.WithByID(req.ID)); err != nil { return err } @@ -463,14 +464,6 @@ func (w WebsiteService) DeleteWebsite(req request.WebsiteDelete) error { } tx.Commit() - if req.DeleteBackup { - localDir, _ := loadLocalDir() - backupDir := path.Join(localDir, fmt.Sprintf("website/%s", website.Alias)) - if _, err := os.Stat(backupDir); err == nil { - _ = os.RemoveAll(backupDir) - } - global.LOG.Infof("delete website %s backups successful", website.Alias) - } uploadDir := path.Join(global.CONF.System.BaseDir, fmt.Sprintf("1panel/uploads/website/%s", website.Alias)) if _, err := os.Stat(uploadDir); err == nil { _ = os.RemoveAll(uploadDir)