Skip to content

Commit 716613a

Browse files
feat: 应用升级备份保留 3 份 (#6116)
Refs #6107
1 parent 12eaa66 commit 716613a

File tree

8 files changed

+43
-6
lines changed

8 files changed

+43
-6
lines changed

backend/app/dto/backup.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type CommonBackup struct {
3636
Name string `json:"name"`
3737
DetailName string `json:"detailName"`
3838
Secret string `json:"secret"`
39+
FileName string `json:"fileName"`
3940
}
4041
type CommonRecover struct {
4142
Source string `json:"source" validate:"required,oneof=OSS S3 SFTP MINIO LOCAL COS KODO OneDrive WebDAV"`

backend/app/repo/backup.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type IBackupRepo interface {
2525
WithByFileName(fileName string) DBOption
2626
WithByType(backupType string) DBOption
2727
WithByCronID(cronjobID uint) DBOption
28+
WithFileNameStartWith(filePrefix string) DBOption
2829
}
2930

3031
func NewIBackupRepo() IBackupRepo {
@@ -47,7 +48,7 @@ func (u *BackupRepo) ListRecord(opts ...DBOption) ([]model.BackupRecord, error)
4748
for _, opt := range opts {
4849
db = opt(db)
4950
}
50-
err := db.Find(&users).Error
51+
err := db.Debug().Find(&users).Error
5152
return users, err
5253
}
5354

@@ -81,6 +82,12 @@ func (u *BackupRepo) WithByFileName(fileName string) DBOption {
8182
}
8283
}
8384

85+
func (u *BackupRepo) WithFileNameStartWith(filePrefix string) DBOption {
86+
return func(g *gorm.DB) *gorm.DB {
87+
return g.Where("file_name LIKE ?", filePrefix+"%")
88+
}
89+
}
90+
8491
func (u *BackupRepo) WithByType(backupType string) DBOption {
8592
return func(g *gorm.DB) *gorm.DB {
8693
if len(backupType) == 0 {

backend/app/service/app_utils.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,8 +516,19 @@ func upgradeInstall(req request.AppInstallUpgrade) error {
516516
)
517517
global.LOG.Infof(i18n.GetMsgWithName("UpgradeAppStart", install.Name, nil))
518518
if req.Backup {
519-
backupRecord, err := NewIBackupService().AppBackup(dto.CommonBackup{Name: install.App.Key, DetailName: install.Name})
519+
iBackUpService := NewIBackupService()
520+
fileName := fmt.Sprintf("upgrade_backup_%s_%s.tar.gz", install.Name, time.Now().Format(constant.DateTimeSlimLayout)+common.RandStrAndNum(5))
521+
backupRecord, err := iBackUpService.AppBackup(dto.CommonBackup{Name: install.App.Key, DetailName: install.Name, FileName: fileName})
520522
if err == nil {
523+
backups, _ := iBackUpService.ListAppRecords(install.App.Key, install.Name, "upgrade_backup")
524+
if len(backups) > 3 {
525+
backupsToDelete := backups[:len(backups)-3]
526+
var deleteIDs []uint
527+
for _, backup := range backupsToDelete {
528+
deleteIDs = append(deleteIDs, backup.ID)
529+
}
530+
_ = iBackUpService.BatchDeleteRecord(deleteIDs)
531+
}
521532
localDir, err := loadLocalDir()
522533
if err == nil {
523534
backupFile = path.Join(localDir, backupRecord.FileDir, backupRecord.FileName)

backend/app/service/backup.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type IBackupService interface {
4040
DeleteRecordByName(backupType, name, detailName string, withDeleteFile bool) error
4141
BatchDeleteRecord(ids []uint) error
4242
NewClient(backup *model.BackupAccount) (cloud_storage.CloudStorageClient, error)
43+
ListAppRecords(name, detailName, fileName string) ([]model.BackupRecord, error)
4344

4445
ListFiles(req dto.BackupSearchFile) []string
4546

@@ -100,6 +101,20 @@ func (u *BackupService) SearchRecordsWithPage(search dto.RecordSearch) (int64, [
100101
return total, datas, err
101102
}
102103

104+
func (u *BackupService) ListAppRecords(name, detailName, fileName string) ([]model.BackupRecord, error) {
105+
records, err := backupRepo.ListRecord(
106+
commonRepo.WithOrderBy("created_at asc"),
107+
commonRepo.WithByName(name),
108+
commonRepo.WithByType("app"),
109+
backupRepo.WithFileNameStartWith(fileName),
110+
backupRepo.WithByDetailName(detailName),
111+
)
112+
if err != nil {
113+
return nil, err
114+
}
115+
return records, err
116+
}
117+
103118
func (u *BackupService) SearchRecordsByCronjobWithPage(search dto.RecordSearchByCronjob) (int64, []dto.BackupRecords, error) {
104119
total, records, err := backupRepo.PageRecord(
105120
search.Page, search.PageSize,

backend/app/service/backup_app.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ func (u *BackupService) AppBackup(req dto.CommonBackup) (*model.BackupRecord, er
3939
itemDir := fmt.Sprintf("app/%s/%s", req.Name, req.DetailName)
4040
backupDir := path.Join(localDir, itemDir)
4141

42-
fileName := fmt.Sprintf("%s_%s.tar.gz", req.DetailName, timeNow+common.RandStrAndNum(5))
42+
fileName := req.FileName
43+
if req.FileName == "" {
44+
fileName = fmt.Sprintf("%s_%s.tar.gz", req.DetailName, timeNow+common.RandStrAndNum(5))
45+
}
4346
if err := handleAppBackup(&install, backupDir, fileName, "", req.Secret); err != nil {
4447
return nil, err
4548
}

frontend/src/lang/modules/en.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ const message = {
18291829
appHelper: 'Please view the installation instructions of some applications on the application details page',
18301830
backupApp: 'Backup application before upgrade',
18311831
backupAppHelper:
1832-
'If the upgrade fails, the backup will be automatically rolled back. Please check the failure reason in the log audit-system log',
1832+
'If the upgrade fails, the backup will be automatically rolled back. Please check the failure reason in the log audit-system log. The backup will retain the latest 3 copies by default',
18331833
delete: 'Delete',
18341834
openrestyDeleteHelper:
18351835
'Forcibly deleting OpenResty will delete all websites, please confirm the risk before operation',

frontend/src/lang/modules/tw.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1701,7 +1701,7 @@ const message = {
17011701
ignoreList: '忽略列表',
17021702
appHelper: '部分應用的安裝使用說明請在應用詳情頁查看',
17031703
backupApp: '升級前備份應用',
1704-
backupAppHelper: '升級失敗會使用備份自動回滾,請在日誌審計-系統日誌中查看失敗原因',
1704+
backupAppHelper: '升級失敗會使用備份自動回滾,請在日誌審計-系統日誌中查看失敗原因,備份預設保留最新的3份',
17051705
delete: '刪除',
17061706
openrestyDeleteHelper: '強制刪除 OpenResty 會刪除所有的網站,請確認風險後操作',
17071707
downloadLogHelper1: '即將下載 {0} 套用所有日誌,是否繼續? ',

frontend/src/lang/modules/zh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ const message = {
17021702
ignoreList: '忽略列表',
17031703
appHelper: '部分应用的安装使用说明请在应用详情页查看',
17041704
backupApp: '升级前备份应用',
1705-
backupAppHelper: '升级失败会使用备份自动回滚,请在日志审计-系统日志中查看失败原因',
1705+
backupAppHelper: '升级失败会使用备份自动回滚,请在日志审计-系统日志中查看失败原因,备份默认保留最新的3份',
17061706
delete: '删除',
17071707
openrestyDeleteHelper: '强制删除 OpenResty 会删除所有的网站,请确认风险之后操作',
17081708
downloadLogHelper1: '即将下载 {0} 应用所有日志,是否继续?',

0 commit comments

Comments
 (0)