mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	!100 定时清理数据库备份数据
* feat: 优化数据库 BINLOG 同步机制 * feat: 删除数据库实例前需删除关联的数据库备份与恢复任务 * refactor: 重构数据库备份与恢复模块 * feat: 定时清理数据库备份历史和本地 Binlog 文件 * feat: 压缩数据库备份文件
This commit is contained in:
		@@ -78,8 +78,6 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
 | 
			
		||||
		d.DbApp.Delete(ctx, dbId)
 | 
			
		||||
		// 删除该库的sql执行记录
 | 
			
		||||
		d.DbSqlExecApp.DeleteBy(ctx, &entity.DbSqlExec{DbId: dbId})
 | 
			
		||||
 | 
			
		||||
		// todo delete restore task and histories
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -81,6 +81,7 @@ func (d *DbBackup) Update(rc *req.Ctx) {
 | 
			
		||||
	job.Name = backupForm.Name
 | 
			
		||||
	job.StartTime = backupForm.StartTime
 | 
			
		||||
	job.Interval = backupForm.Interval
 | 
			
		||||
	job.MaxSaveDays = backupForm.MaxSaveDays
 | 
			
		||||
	biz.ErrIsNilAppendErr(d.backupApp.Update(rc.MetaCtx, job), "保存数据库备份任务失败: %v")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -178,7 +179,7 @@ func (d *DbBackup) GetHistoryPageList(rc *req.Ctx) {
 | 
			
		||||
	rc.ResData = res
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// RestoreHistories 删除数据库备份历史
 | 
			
		||||
// RestoreHistories 从数据库备份历史中恢复数据库
 | 
			
		||||
// @router /api/dbs/:dbId/backup-histories/:backupHistoryId/restore [POST]
 | 
			
		||||
func (d *DbBackup) RestoreHistories(rc *req.Ctx) {
 | 
			
		||||
	pm := ginx.PathParam(rc.GinCtx, "backupHistoryId")
 | 
			
		||||
 
 | 
			
		||||
@@ -87,16 +87,10 @@ func (d *Instance) DeleteInstance(rc *req.Ctx) {
 | 
			
		||||
 | 
			
		||||
	for _, v := range ids {
 | 
			
		||||
		value, err := strconv.Atoi(v)
 | 
			
		||||
		biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
 | 
			
		||||
		biz.ErrIsNilAppendErr(err, "删除数据库实例失败: %s")
 | 
			
		||||
		instanceId := uint64(value)
 | 
			
		||||
		if d.DbApp.Count(&entity.DbQuery{InstanceId: instanceId}) != 0 {
 | 
			
		||||
			instance, err := d.InstanceApp.GetById(new(entity.DbInstance), instanceId, "name")
 | 
			
		||||
			biz.ErrIsNil(err, "获取数据库实例错误,数据库实例ID为: %d", instance.Id)
 | 
			
		||||
			biz.IsTrue(false, "不能删除数据库实例【%s】,请先删除其关联的数据库资源。", instance.Name)
 | 
			
		||||
		}
 | 
			
		||||
		// todo check if backup task has been disabled and backup histories have been deleted
 | 
			
		||||
 | 
			
		||||
		d.InstanceApp.Delete(rc.MetaCtx, instanceId)
 | 
			
		||||
		err = d.InstanceApp.Delete(rc.MetaCtx, instanceId)
 | 
			
		||||
		biz.ErrIsNilAppendErr(err, "删除数据库实例失败: %s")
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -14,6 +14,7 @@ type DbBackupForm struct {
 | 
			
		||||
	Interval    time.Duration `json:"-"`                            // 间隔时间: 为零表示单次执行,为正表示反复执行
 | 
			
		||||
	IntervalDay uint64        `json:"intervalDay"`                  // 间隔天数: 为零表示单次执行,为正表示反复执行
 | 
			
		||||
	Repeated    bool          `json:"repeated"`                     // 是否重复执行
 | 
			
		||||
	MaxSaveDays int           `json:"maxSaveDays"`                  // 数据库备份历史保留天数,过期将自动删除
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (restore *DbBackupForm) UnmarshalJSON(data []byte) error {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,6 +15,7 @@ type DbBackup struct {
 | 
			
		||||
	StartTime    time.Time          `json:"startTime"`            // 开始时间
 | 
			
		||||
	Interval     time.Duration      `json:"-"`                    // 间隔时间
 | 
			
		||||
	IntervalDay  uint64             `json:"intervalDay" gorm:"-"` // 间隔天数
 | 
			
		||||
	MaxSaveDays  int                `json:"maxSaveDays"`          // 数据库备份历史保留天数,过期将自动删除
 | 
			
		||||
	Enabled      bool               `json:"enabled"`              // 是否启用
 | 
			
		||||
	EnabledDesc  string             `json:"enabledDesc"`          // 启用状态描述
 | 
			
		||||
	LastTime     timex.NullTime     `json:"lastTime"`             // 最近一次执行时间
 | 
			
		||||
@@ -29,9 +30,9 @@ func (backup *DbBackup) MarshalJSON() ([]byte, error) {
 | 
			
		||||
	backup.IntervalDay = uint64(backup.Interval / time.Hour / 24)
 | 
			
		||||
	if len(backup.EnabledDesc) == 0 {
 | 
			
		||||
		if backup.Enabled {
 | 
			
		||||
			backup.EnabledDesc = "任务已启用"
 | 
			
		||||
			backup.EnabledDesc = "已启用"
 | 
			
		||||
		} else {
 | 
			
		||||
			backup.EnabledDesc = "任务已禁用"
 | 
			
		||||
			backup.EnabledDesc = "已禁用"
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return json.Marshal((*dbBackup)(backup))
 | 
			
		||||
 
 | 
			
		||||
@@ -30,9 +30,9 @@ func (restore *DbRestore) MarshalJSON() ([]byte, error) {
 | 
			
		||||
	restore.IntervalDay = uint64(restore.Interval / time.Hour / 24)
 | 
			
		||||
	if len(restore.EnabledDesc) == 0 {
 | 
			
		||||
		if restore.Enabled {
 | 
			
		||||
			restore.EnabledDesc = "任务已启用"
 | 
			
		||||
			restore.EnabledDesc = "已启用"
 | 
			
		||||
		} else {
 | 
			
		||||
			restore.EnabledDesc = "任务已禁用"
 | 
			
		||||
			restore.EnabledDesc = "已禁用"
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return json.Marshal((*dbBackup)(restore))
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user