feat: 实现数据库备份与恢复

This commit is contained in:
kanzihuang
2023-12-27 22:59:20 +08:00
committed by wanli
parent 1a7d425f60
commit e344722794
92 changed files with 5997 additions and 69 deletions

View File

@@ -0,0 +1,86 @@
package entity
import (
"mayfly-go/pkg/model"
"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:"-"`
}
func NewDbBinlog(history *DbBackupHistory) *DbBinlog {
binlogTask := &DbBinlog{
StartTime: time.Now(),
Enabled: true,
Interval: BinlogDownloadInterval,
DbInstanceId: history.DbInstanceId,
}
binlogTask.Id = binlogTask.DbInstanceId
return binlogTask
}
func (d *DbBinlog) TableName() string {
return "t_db_binlog"
}
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
}