mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
重构数据库备份与恢复模块 (#80)
* fix: 保存 LastResult 时截断字符串过长部分,以避免数据库报错 * refactor: 新增 entity.DbTaskBase 和 persistence.dbTaskBase, 用于实现数据库备份和恢复任务处理相关部分 * fix: aeskey变更后,解密密码出现数组越界访问错误 * fix: 时间属性为零值时,保存到 mysql 数据库报错 * refactor db.infrastructure.service.scheduler * feat: 实现立即备份功能 * refactor db.infrastructure.service.db_instance * refactor: 从数据库中获取数据库备份目录、mysql文件路径等配置信息 * fix: 数据库备份和恢复问题 * fix: 修改 .gitignore 文件,忽略数据库备份目录和数据库程序目录
This commit is contained in:
@@ -1,77 +1,39 @@
|
||||
package entity
|
||||
|
||||
import (
|
||||
"mayfly-go/pkg/model"
|
||||
"time"
|
||||
)
|
||||
|
||||
var _ DbTask = (*DbBackup)(nil)
|
||||
|
||||
// DbBackup 数据库备份任务
|
||||
type DbBackup struct {
|
||||
model.Model
|
||||
*DbTaskBase
|
||||
|
||||
Name string `gorm:"column(db_name)" json:"name"` // 备份任务名称
|
||||
DbName string `gorm:"column(db_name)" json:"dbName"` // 数据库名
|
||||
StartTime time.Time `gorm:"column(start_time)" json:"startTime"` // 开始时间: 2023-11-08 02:00:00
|
||||
Interval time.Duration `gorm:"column(interval)" json:"interval"` // 间隔时间: 为零表示单次执行,为正表示反复执行
|
||||
Enabled bool `gorm:"column(enabled)" json:"enabled"` // 是否启用
|
||||
Finished bool `gorm:"column(finished)" json:"finished"` // 是否完成
|
||||
Repeated bool `gorm:"column(repeated)" json:"repeated"` // 是否重复执行
|
||||
LastStatus TaskStatus `gorm:"column(last_status)" json:"lastStatus"` // 最近一次执行状态
|
||||
LastResult string `gorm:"column(last_result)" json:"lastResult"` // 最近一次执行结果
|
||||
LastTime time.Time `gorm:"column(last_time)" json:"lastTime"` // 最近一次执行时间: 2023-11-08 02:00:00
|
||||
DbInstanceId uint64 `gorm:"column(db_instance_id)" json:"dbInstanceId"`
|
||||
Deadline time.Time `gorm:"-" json:"-"`
|
||||
Name string `json:"name"` // 备份任务名称
|
||||
DbName string `json:"dbName"` // 数据库名
|
||||
DbInstanceId uint64 `json:"dbInstanceId"` // 数据库实例ID
|
||||
}
|
||||
|
||||
func (d *DbBackup) TableName() string {
|
||||
return "t_db_backup"
|
||||
}
|
||||
|
||||
func (d *DbBackup) GetId() uint64 {
|
||||
if d == nil {
|
||||
return 0
|
||||
var (
|
||||
backupResult = map[TaskStatus]string{
|
||||
TaskDelay: "等待备份数据库",
|
||||
TaskReady: "准备备份数据库",
|
||||
TaskReserved: "数据库备份中",
|
||||
TaskSuccess: "数据库备份成功",
|
||||
TaskFailed: "数据库备份失败",
|
||||
}
|
||||
return d.Id
|
||||
}
|
||||
)
|
||||
|
||||
func (d *DbBackup) GetDeadline() time.Time {
|
||||
return d.Deadline
|
||||
}
|
||||
|
||||
func (d *DbBackup) Schedule() bool {
|
||||
if d.Finished || !d.Enabled {
|
||||
return false
|
||||
}
|
||||
switch d.LastStatus {
|
||||
func (*DbBackup) MessageWithStatus(status TaskStatus) string {
|
||||
var result string
|
||||
switch status {
|
||||
case TaskDelay:
|
||||
result = "等待备份数据库"
|
||||
case TaskReady:
|
||||
result = "准备备份数据库"
|
||||
case TaskReserved:
|
||||
result = "数据库备份中"
|
||||
case TaskSuccess:
|
||||
if d.Interval == 0 {
|
||||
return false
|
||||
}
|
||||
lastTime := d.LastTime
|
||||
if d.LastTime.Sub(d.StartTime) < 0 {
|
||||
lastTime = d.StartTime.Add(-d.Interval)
|
||||
}
|
||||
d.Deadline = lastTime.Add(d.Interval - d.LastTime.Sub(d.StartTime)%d.Interval)
|
||||
result = "数据库备份成功"
|
||||
case TaskFailed:
|
||||
d.Deadline = time.Now().Add(time.Minute)
|
||||
default:
|
||||
d.Deadline = d.StartTime
|
||||
result = "数据库备份失败"
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (d *DbBackup) IsFinished() bool {
|
||||
return !d.Repeated && d.LastStatus == TaskSuccess
|
||||
}
|
||||
|
||||
func (d *DbBackup) Update(task DbTask) bool {
|
||||
switch t := task.(type) {
|
||||
case *DbBackup:
|
||||
d.StartTime = t.StartTime
|
||||
d.Interval = t.Interval
|
||||
return true
|
||||
}
|
||||
return false
|
||||
return result
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user