mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 16:00:25 +08:00
feat: 实现数据库备份与恢复
This commit is contained in:
86
server/internal/db/domain/entity/db_binlog.go
Normal file
86
server/internal/db/domain/entity/db_binlog.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user