refactor: code review

This commit is contained in:
meilin.huang
2023-12-29 16:48:15 +08:00
parent 664118a709
commit 76fd6675b5
35 changed files with 343 additions and 349 deletions

View File

@@ -61,6 +61,7 @@ func (d *DbBackup) Create(rc *req.Ctx) {
Enabled: true,
Repeated: form.Repeated,
DbInstanceId: db.InstanceId,
LastTime: form.StartTime,
}
tasks = append(tasks, task)
}
@@ -78,6 +79,7 @@ func (d *DbBackup) Save(rc *req.Ctx) {
Name: form.Name,
StartTime: form.StartTime,
Interval: form.Interval,
LastTime: form.StartTime,
}
task.Id = form.Id
biz.ErrIsNilAppendErr(d.DbBackupApp.Save(rc.MetaCtx, task), "保存数据库备份任务失败: %v")

View File

@@ -1,10 +1,14 @@
package config
import sysapp "mayfly-go/internal/sys/application"
import (
sysapp "mayfly-go/internal/sys/application"
)
const (
ConfigKeyDbSaveQuerySQL string = "DbSaveQuerySQL" // 数据库是否记录查询相关sql
ConfigKeyDbQueryMaxCount string = "DbQueryMaxCount" // 数据库查询的最大数量
ConfigKeyDbBackupRestore string = "DbBackupRestore" // 数据库备份
ConfigKeyDbMysqlBin string = "MysqlBin" // mysql可执行文件配置
)
// 获取数据库最大查询数量配置
@@ -16,3 +20,65 @@ func GetDbQueryMaxCount() int {
func GetDbSaveQuerySql() bool {
return sysapp.GetConfigApp().GetConfig(ConfigKeyDbSaveQuerySQL).BoolValue(false)
}
type DbBackupRestore struct {
BackupPath string // 备份文件路径呢
}
// 获取数据库备份配置
func GetDbBackupRestore() *DbBackupRestore {
c := sysapp.GetConfigApp().GetConfig(ConfigKeyDbBackupRestore)
jm := c.GetJsonMap()
dbrc := new(DbBackupRestore)
backupPath := jm["backupPath"]
if backupPath == "" {
backupPath = "./db/backup"
}
dbrc.BackupPath = backupPath
return dbrc
}
// mysql客户端可执行文件配置
type MysqlBin struct {
Path string // 可执行文件路径
MysqlPath string // mysql可执行文件路径
MysqldumpPath string // mysqldump可执行文件路径
MysqlbinlogPath string // mysqlbinlog可执行文件路径
}
// 获取数据库备份配置
func GetMysqlBin() *MysqlBin {
c := sysapp.GetConfigApp().GetConfig(ConfigKeyDbMysqlBin)
jm := c.GetJsonMap()
mbc := new(MysqlBin)
path := jm["path"]
if path == "" {
path = "./db/backup"
}
mbc.Path = path
mysqlPath := jm["mysql"]
if mysqlPath == "" {
mysqlPath = path + "mysql"
}
mbc.MysqlPath = mysqlPath
mysqldumpPath := jm["mysqldump"]
if mysqldumpPath == "" {
mysqldumpPath = path + "mysqldump"
}
mbc.MysqldumpPath = mysqldumpPath
mysqlbinlogPath := jm["mysqlbinlog"]
if mysqlbinlogPath == "" {
mysqlbinlogPath = path + "mysqlbinlog"
}
mbc.MysqlbinlogPath = mysqlbinlogPath
return mbc
}

View File

@@ -0,0 +1 @@
package dbm

View File

@@ -29,6 +29,7 @@ func NewDbBinlog(history *DbBackupHistory) *DbBinlog {
Enabled: true,
Interval: BinlogDownloadInterval,
DbInstanceId: history.DbInstanceId,
LastTime: time.Now(),
}
binlogTask.Id = binlogTask.DbInstanceId
return binlogTask

View File

@@ -4,12 +4,13 @@ import (
"context"
"encoding/binary"
"fmt"
"github.com/google/uuid"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/internal/db/domain/service"
"mayfly-go/pkg/model"
"time"
"github.com/google/uuid"
)
var _ service.DbBackupSvc = (*DbBackupSvcImpl)(nil)

View File

@@ -18,11 +18,12 @@ import (
"github.com/pkg/errors"
"golang.org/x/sync/singleflight"
"mayfly-go/internal/db/config"
"mayfly-go/internal/db/dbm"
"mayfly-go/internal/db/domain/entity"
"mayfly-go/internal/db/domain/repository"
"mayfly-go/internal/db/domain/service"
"mayfly-go/pkg/config"
"mayfly-go/pkg/logx"
"mayfly-go/pkg/utils/structx"
)
@@ -145,13 +146,13 @@ func (svc *DbInstanceSvcImpl) Backup(ctx context.Context, backupHistory *entity.
}
cmd := exec.CommandContext(ctx, mysqldumpPath(), args...)
logx.Debug("backup database using mysqldump binary: ", cmd.String())
logx.Debugf("backup database using mysqldump binary: %s", cmd.String())
if err := runCmd(cmd); err != nil {
logx.Errorf("运行 mysqldump 程序失败: %v", err)
return nil, errors.Wrap(err, "运行 mysqldump 程序失败")
}
logx.Debug("Checking dumped file stat", tmpFile)
logx.Debugf("Checking dumped file stat: %s", tmpFile)
if _, err := os.Stat(tmpFile); err != nil {
logx.Errorf("未找到备份文件: %v", err)
return nil, errors.Wrapf(err, "未找到备份文件")
@@ -297,20 +298,20 @@ func parseLocalBinlogFirstEventTime(ctx context.Context, filePath string) (event
// getBinlogDir gets the binlogDir.
func getBinlogDir(instanceId uint64) string {
return filepath.Join(
config.Conf.Db.BackupPath,
config.GetDbBackupRestore().BackupPath,
fmt.Sprintf("instance-%d", instanceId),
"binlog")
}
func getDbInstanceBackupRoot(instanceId uint64) string {
return filepath.Join(
config.Conf.Db.BackupPath,
config.GetDbBackupRestore().BackupPath,
fmt.Sprintf("instance-%d", instanceId))
}
func getDbBackupDir(instanceId, backupId uint64) string {
return filepath.Join(
config.Conf.Db.BackupPath,
config.GetDbBackupRestore().BackupPath,
fmt.Sprintf("instance-%d", instanceId),
fmt.Sprintf("backup-%d", backupId))
}
@@ -885,13 +886,13 @@ func formatDateTime(t time.Time) string {
}
func mysqlPath() string {
return config.Conf.Db.MysqlUtil.Mysql
return config.GetMysqlBin().MysqlPath
}
func mysqldumpPath() string {
return config.Conf.Db.MysqlUtil.MysqlDump
return config.GetMysqlBin().MysqldumpPath
}
func mysqlbinlogPath() string {
return config.Conf.Db.MysqlUtil.MysqlBinlog
return config.GetMysqlBin().MysqlbinlogPath
}