Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mysql): 备份恢复命令支持EnableBinlog 控制 #978

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type InstallNewDbBackupParam struct {
ClusterId map[Port]int `json:"cluster_id"` // cluster id
ShardValue map[Port]int `json:"shard_value"` // shard value for spider
ExecUser string `json:"exec_user"` // 执行Job的用户
UntarOnly bool `json:"untar_only"` // 只解压,不校验不渲染配置
UntarOnly bool `json:"untar_only"` // 只解压,不校验不渲染配置,不连接 db
}

type runtimeContext struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ type LoaderUtil struct {
TgtInstance native.InsObject `json:"tgt_instance"`
IndexObj *dbbackup.BackupIndexFile

// 不写 binlog -without-binlog: set sql_log_bin=0
WithOutBinlog bool `json:"withoutBinlog"`
// EnableBinlog 导入数据时是否写binlog,默认不启用 (set sql_log_bin=0)
EnableBinlog bool `json:"enable_binlog"`
IndexFilePath string `json:"index_file_path" validate:"required"`
LoaderDir string `json:"loader_dir"`
TaskDir string `json:"taskDir"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (l *LogicalLoader) CreateConfigFile() error {
MysqlLoadDir: p.LoaderDir,
IndexFilePath: p.IndexFilePath,
Threads: 4,
EnableBinlog: !l.WithOutBinlog,
EnableBinlog: p.EnableBinlog,
Regex: l.myloaderRegex,
}
if loaderConfig.MysqlCharset == "" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (m *DBLoader) chooseDBBackupLoader() error {
IndexObj: m.BackupInfo.indexObj,
LoaderDir: m.targetDir,
TaskDir: m.taskDir,
WithOutBinlog: true,
EnableBinlog: m.RestoreOpt.EnableBinlog,
}
// logger.Warn("validate dbLoaderUtil: %+v", m.dbLoaderUtil)
if err := validate.GoValidateStruct(m.dbLoaderUtil, false, false); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type RestoreOpt struct {
// 在指定时间点回档场景才需要,是否恢复 binlog。在 doSlave 场景,是不需要 recover_binlog。这个选项是控制下一步恢复binlog的行为
// 当 recover_binlog 时,要确保实例的所有库表结构都恢复。在逻辑回档场景,只回档部分库表数据时,依然要恢复所有表结构
WillRecoverBinlog bool `json:"recover_binlog"`
// EnableBinlog 导入数据时是否写binlog,默认不启用
EnableBinlog bool `json:"enable_binlog"`
// 在库表级定点回档时有用,如果是 statement/mixed 格式,导入数据时需要全部导入;
// 如果是 row,可只导入指定库表数据, 在 recover-binlog 时可指定 quick_mode=true 也恢复指定库表 binlog
SourceBinlogFormat string `json:"source_binlog_format" enums:",ROW,STATEMENT,MIXED"`
Expand Down
25 changes: 15 additions & 10 deletions dbm-services/mysql/db-tools/mysql-dbbackup/pkg/config/logical.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
package config

// LogicalBackup the config of logical backup
// data or schema is controlled by Public.DataSchemaGrant
type LogicalBackup struct {
ChunkFileSize uint64 `ini:"ChunkFilesize"` // split tables into chunks of this output file size. This value is in MB
ChunkFilesize uint64 `ini:"ChunkFilesize"` // split tables into chunks of this output file size. This value is in MB
Regex string `ini:"Regex"`
Threads int `ini:"Threads"`
DisableCompress bool `ini:"DisableCompress"`
Expand All @@ -21,15 +22,19 @@ type LogicalBackup struct {

// LogicalLoad the config of logical loading
type LogicalLoad struct {
MysqlHost string `ini:"MysqlHost"`
MysqlPort int `ini:"MysqlPort"`
MysqlUser string `ini:"MysqlUser"`
MysqlPasswd string `ini:"MysqlPasswd"`
MysqlCharset string `ini:"MysqlCharset"`
MysqlLoadDir string `ini:"MysqlLoadDir"`
Threads int `ini:"Threads"`
Regex string `ini:"Regex"`
EnableBinlog bool `ini:"EnableBinlog"`
MysqlHost string `ini:"MysqlHost"`
MysqlPort int `ini:"MysqlPort"`
MysqlUser string `ini:"MysqlUser"`
MysqlPasswd string `ini:"MysqlPasswd"`
MysqlCharset string `ini:"MysqlCharset"`
MysqlLoadDir string `ini:"MysqlLoadDir"`
Threads int `ini:"Threads"`
Regex string `ini:"Regex"`
EnableBinlog bool `ini:"EnableBinlog"`
// SchemaOnly import schema,trigger,func,proc (--no-data)
// if you want only table schema, use ExtraOpt = -skip-triggers --skip-post
// mydumper doest not support data only currently, you should backup only data for your purpose
SchemaOnly bool `ini:"SchemaOnly"`
IndexFilePath string `ini:"IndexFilePath" validate:"required"`
ExtraOpt string `ini:"ExtraOpt"` // other myloader options string to be appended
}
60 changes: 13 additions & 47 deletions dbm-services/mysql/db-tools/mysql-dbbackup/pkg/config/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ import (
)

type Public struct {
BkBizId int `ini:"BkBizId" validate:"required"`
BkCloudId int `ini:"BkCloudId"`
BillId string `ini:"BillId"`
BackupId string `ini:"BackupId"`
ClusterId int `ini:"ClusterId"`
ClusterAddress string `ini:"ClusterAddress"`
ShardValue int `ini:"ShardValue"` // 分片 id,仅 spider 有用
MysqlHost string `ini:"MysqlHost" validate:"required,ip"`
MysqlPort int `ini:"MysqlPort" validate:"required"`
MysqlUser string `ini:"MysqlUser" validate:"required"`
MysqlPasswd string `ini:"MysqlPasswd"`
DataSchemaGrant string `ini:"DataSchemaGrant" validate:"required"` // data,grant,priv,all
BkBizId int `ini:"BkBizId" validate:"required"`
BkCloudId int `ini:"BkCloudId"`
BillId string `ini:"BillId"`
BackupId string `ini:"BackupId"`
ClusterId int `ini:"ClusterId"`
ClusterAddress string `ini:"ClusterAddress"`
ShardValue int `ini:"ShardValue"` // 分片 id,仅 spider 有用
MysqlHost string `ini:"MysqlHost" validate:"required,ip"`
MysqlPort int `ini:"MysqlPort" validate:"required"`
MysqlUser string `ini:"MysqlUser" validate:"required"`
MysqlPasswd string `ini:"MysqlPasswd"`
// DataSchemaGrant data,grant,schema,priv,all,写了 data 则只备data,不备份 schema
DataSchemaGrant string `ini:"DataSchemaGrant" validate:"required"`
BackupDir string `ini:"BackupDir" validate:"required"`
MysqlRole string `ini:"MysqlRole" validate:"required"` // oneof=master slave
MysqlCharset string `ini:"MysqlCharset"`
Expand All @@ -47,41 +48,6 @@ type Public struct {
targetName string
}

// ParseDataSchemaGrant Check whether data|schema|grant is backed up
//func (c *Public) ParseDataSchemaGrant() error {
// valueAllowed := []string{cst.BackupGrant, cst.BackupSchema, cst.BackupData, cst.BackupAll}
// arr := strings.Split(c.DataSchemaGrant, ",")
// set := make(map[string]struct{}, len(arr))
// for _, v := range arr {
// v = strings.ToLower(strings.TrimSpace(v))
// if !cmutil.StringsHas(valueAllowed, v) {
// return fmt.Errorf("the part of param DataSchemaGrant [%s] is wrong", v)
// }
// set[v] = struct{}{}
// }
// if _, found := set[cst.BackupData]; found {
// common.BackupData = true
// }
// if _, found := set[cst.BackupSchema]; found {
// common.BackupSchema = true
// }
// if _, found := set[cst.BackupGrant]; found {
// common.BackupGrant = true
// }
// if _, found := set[cst.BackupAll]; found {
// // all is alias to 'grant,schema,data'
// common.BackupGrant = true
// common.BackupData = true
// common.BackupSchema = true
// }
//
// if !common.BackupData && !common.BackupSchema && !common.BackupGrant {
// return fmt.Errorf("need to backup at least one of %v", valueAllowed)
// }
//
// return nil
//}

// GetCnfFileName TODO
func (c *Public) GetCnfFileName() string {
return c.cnfFilename
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (l *LogicalDumper) Execute(enableTimeOut bool) error {
"-o", filepath.Join(l.cnf.Public.BackupDir, l.cnf.Public.TargetName()),
fmt.Sprintf("--long-query-retries=%d", l.cnf.LogicalBackup.FlushRetryCount),
fmt.Sprintf("--set-names=%s", l.cnf.Public.MysqlCharset),
fmt.Sprintf("--chunk-filesize=%d", l.cnf.LogicalBackup.ChunkFileSize),
fmt.Sprintf("--chunk-filesize=%d", l.cnf.LogicalBackup.ChunkFilesize),
fmt.Sprintf("--threads=%d", l.cnf.LogicalBackup.Threads),
"--trx-consistency-only",
"--long-query-retry-interval=10",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ func (l *LogicalLoader) Execute() error {
if l.cnf.LogicalLoad.EnableBinlog {
args = append(args, "--enable-binlog")
}
if l.cnf.LogicalLoad.SchemaOnly {
args = append(args, "--no-data")
}
if l.cnf.LogicalLoad.Regex != "" {
args = append(args, "-x", fmt.Sprintf(`'%s'`, l.cnf.LogicalLoad.Regex))
}
Expand Down
Loading