重构数据库备份与恢复模块 (#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:
kanzihuang
2024-01-05 08:55:34 +08:00
committed by GitHub
parent 76fd6675b5
commit ae3d2659aa
83 changed files with 1819 additions and 1688 deletions

View File

@@ -2,86 +2,34 @@ package entity
import (
"mayfly-go/pkg/model"
"mayfly-go/pkg/utils/timex"
"time"
)
const BinlogDownloadInterval = time.Minute * 15
var _ DbTask = (*DbBinlog)(nil)
// DbBinlog 数据库备份任务
type DbBinlog struct {
model.Model
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"` // 是否启用
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:"-"`
LastStatus TaskStatus // 最近一次执行状态
LastResult string // 最近一次执行结果
LastTime timex.NullTime // 最近一次执行时间
DbInstanceId uint64 `json:"dbInstanceId"` // 数据库实例ID
}
func NewDbBinlog(history *DbBackupHistory) *DbBinlog {
binlogTask := &DbBinlog{
StartTime: time.Now(),
Enabled: true,
Interval: BinlogDownloadInterval,
DbInstanceId: history.DbInstanceId,
LastTime: time.Now(),
}
binlogTask.Id = binlogTask.DbInstanceId
func NewDbBinlog(instanceId uint64) *DbBinlog {
binlogTask := &DbBinlog{}
binlogTask.Id = instanceId
binlogTask.DbInstanceId = instanceId
return binlogTask
}
func (d *DbBinlog) TableName() string {
return "t_db_binlog"
}
// BinlogFile is the metadata of the MySQL binlog file.
type BinlogFile struct {
Name string
Size int64
func (d *DbBinlog) GetId() uint64 {
if d == nil {
return 0
}
return d.Id
}
func (d *DbBinlog) GetDeadline() time.Time {
return d.Deadline
}
func (d *DbBinlog) Schedule() bool {
if !d.Enabled {
return false
}
switch d.LastStatus {
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)
case TaskFailed:
d.Deadline = time.Now().Add(time.Minute)
default:
d.Deadline = d.StartTime
}
return true
}
func (d *DbBinlog) IsFinished() bool {
return false
}
func (d *DbBinlog) Update(task DbTask) bool {
switch t := task.(type) {
case *DbBinlog:
d.StartTime = t.StartTime
d.Interval = t.Interval
return true
}
return false
// Sequence is parsed from Name and is for the sorting purpose.
Sequence int64
FirstEventTime time.Time
Downloaded bool
}