mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-03 07:50:25 +08:00
* feat: 修复数据库备份与恢复问题 * feat: 启用 BINLOG 支持全量备份和增量备份,未启用 BINLOG 仅支持全量备份 * feat: 数据库恢复后自动备份,避免数据丢失
156 lines
3.0 KiB
Go
156 lines
3.0 KiB
Go
package entity
|
|
|
|
import (
|
|
"fmt"
|
|
"mayfly-go/pkg/model"
|
|
"mayfly-go/pkg/runner"
|
|
"mayfly-go/pkg/utils/stringx"
|
|
"mayfly-go/pkg/utils/timex"
|
|
"time"
|
|
)
|
|
|
|
const LastResultSize = 256
|
|
|
|
type DbJobKey = runner.JobKey
|
|
|
|
type DbJobStatus int
|
|
|
|
const (
|
|
DbJobRunning DbJobStatus = iota
|
|
DbJobSuccess
|
|
DbJobFailed
|
|
)
|
|
|
|
type DbJobType string
|
|
|
|
func (typ DbJobType) String() string {
|
|
return string(typ)
|
|
}
|
|
|
|
const (
|
|
DbJobTypeBackup DbJobType = "db-backup"
|
|
DbJobTypeRestore DbJobType = "db-restore"
|
|
DbJobTypeBinlog DbJobType = "db-binlog"
|
|
)
|
|
|
|
const (
|
|
DbJobNameBackup = "数据库备份"
|
|
DbJobNameRestore = "数据库恢复"
|
|
DbJobNameBinlog = "BINLOG同步"
|
|
)
|
|
|
|
var _ runner.Job = (DbJob)(nil)
|
|
|
|
type DbJobBase interface {
|
|
model.ModelI
|
|
|
|
GetKey() string
|
|
GetJobType() DbJobType
|
|
SetJobType(typ DbJobType)
|
|
GetJobBase() *DbJobBaseImpl
|
|
SetLastStatus(status DbJobStatus, err error)
|
|
}
|
|
|
|
type DbJob interface {
|
|
runner.Job
|
|
DbJobBase
|
|
|
|
GetDbName() string
|
|
Schedule() (time.Time, error)
|
|
IsEnabled() bool
|
|
SetEnabled(enabled bool)
|
|
Update(job runner.Job)
|
|
GetInterval() time.Duration
|
|
}
|
|
|
|
func NewDbJob(typ DbJobType) DbJob {
|
|
switch typ {
|
|
case DbJobTypeBackup:
|
|
return &DbBackup{
|
|
DbJobBaseImpl: &DbJobBaseImpl{
|
|
jobType: DbJobTypeBackup},
|
|
}
|
|
case DbJobTypeRestore:
|
|
return &DbRestore{
|
|
DbJobBaseImpl: &DbJobBaseImpl{
|
|
jobType: DbJobTypeRestore},
|
|
}
|
|
default:
|
|
panic(fmt.Sprintf("invalid DbJobType: %v", typ))
|
|
}
|
|
}
|
|
|
|
var _ DbJobBase = (*DbJobBaseImpl)(nil)
|
|
|
|
type DbJobBaseImpl struct {
|
|
model.Model
|
|
|
|
DbInstanceId uint64 // 数据库实例ID
|
|
LastStatus DbJobStatus // 最近一次执行状态
|
|
LastResult string // 最近一次执行结果
|
|
LastTime timex.NullTime // 最近一次执行时间
|
|
jobType DbJobType
|
|
jobKey runner.JobKey
|
|
}
|
|
|
|
func NewDbBJobBase(instanceId uint64, jobType DbJobType) *DbJobBaseImpl {
|
|
return &DbJobBaseImpl{
|
|
DbInstanceId: instanceId,
|
|
jobType: jobType,
|
|
}
|
|
}
|
|
|
|
func (d *DbJobBaseImpl) GetJobType() DbJobType {
|
|
return d.jobType
|
|
}
|
|
|
|
func (d *DbJobBaseImpl) SetJobType(typ DbJobType) {
|
|
d.jobType = typ
|
|
}
|
|
|
|
func (d *DbJobBaseImpl) SetLastStatus(status DbJobStatus, err error) {
|
|
var statusName, jobName string
|
|
switch status {
|
|
case DbJobRunning:
|
|
statusName = "运行中"
|
|
case DbJobSuccess:
|
|
statusName = "成功"
|
|
case DbJobFailed:
|
|
statusName = "失败"
|
|
default:
|
|
return
|
|
}
|
|
switch d.jobType {
|
|
case DbJobTypeBackup:
|
|
jobName = DbJobNameBackup
|
|
case DbJobTypeRestore:
|
|
jobName = DbJobNameRestore
|
|
case DbJobTypeBinlog:
|
|
jobName = DbJobNameBinlog
|
|
default:
|
|
jobName = d.jobType.String()
|
|
}
|
|
d.LastStatus = status
|
|
var result = jobName + statusName
|
|
if err != nil {
|
|
result = fmt.Sprintf("%s: %v", result, err)
|
|
}
|
|
d.LastResult = stringx.TruncateStr(result, LastResultSize)
|
|
d.LastTime = timex.NewNullTime(time.Now())
|
|
}
|
|
|
|
func (d *DbJobBaseImpl) GetJobBase() *DbJobBaseImpl {
|
|
return d
|
|
}
|
|
|
|
func FormatJobKey(typ DbJobType, jobId uint64) DbJobKey {
|
|
return fmt.Sprintf("%v-%d", typ, jobId)
|
|
}
|
|
|
|
func (d *DbJobBaseImpl) GetKey() DbJobKey {
|
|
if len(d.jobKey) == 0 {
|
|
d.jobKey = FormatJobKey(d.jobType, d.Id)
|
|
}
|
|
return d.jobKey
|
|
}
|