Skip to content

Commit

Permalink
fix(mysql): 修复 dbbackup table filter 问题 #6935
Browse files Browse the repository at this point in the history
  • Loading branch information
seanlook authored and zhangzhw8 committed Sep 13, 2024
1 parent f40b369 commit b06e8c2
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package backupdemand

import (
"bufio"
"dbm-services/mysql/db-tools/dbactuator/pkg/native"
"encoding/json"
"fmt"
"math/rand"
Expand All @@ -20,6 +19,8 @@ import (
"strings"
"time"

"dbm-services/mysql/db-tools/dbactuator/pkg/native"

"github.com/pkg/errors"

"dbm-services/common/go-pubpkg/cmutil"
Expand Down Expand Up @@ -149,6 +150,7 @@ func (c *Component) GenerateBackupConfig() error {

backupConfig.LogicalBackup.Regex = ""
if c.Params.BackupType == "logical" {
//backupConfig.LogicalBackup.UseMysqldump = "auto"
ignoreDbs := slices.DeleteFunc(native.DBSys, func(s string) bool {
return s == "infodba_schema"
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import (
ToDo
*/
func (c *DbTableFilter) validate() error {
if len(c.IncludeDbPatterns) == 0 || len(c.IncludeTablePatterns) == 0 {
return fmt.Errorf("include patterns can't be empty")
if len(c.IncludeDbPatterns) == 0 {
return fmt.Errorf("include databases patterns can't be empty")
}
if len(c.IncludeTablePatterns) == 0 {
return fmt.Errorf("include tables patterns can't be empty")
}

if slices.Index(c.ExcludeDbPatterns, "*") >= 0 {
return fmt.Errorf("exclude databases patterns can't be *")

if slices.Index(c.ExcludeDbPatterns, "*") >= 0 || slices.Index(c.ExcludeTablePatterns, "*") >= 0 {
return fmt.Errorf("exclude patterns can't be *")
}
if slices.Index(c.ExcludeTablePatterns, "*") >= 0 {
return fmt.Errorf("exclude tables patterns can't be *")
}

if err := globCheck(c.IncludeDbPatterns); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,19 @@ func (f *TableFilter) GetFilterType() FilterType {

func (f *TableFilter) ValidateFilter() error {
if f.Databases != "" || f.Tables != "" || f.ExcludeDatabases != "" || f.ExcludeTables != "" { // FilterTypeForm
logger.Log.Warnf("filter type 'form' will ignore regex=%s or tables-list=%s", f.Regex, f.TablesList)
if f.Regex == "" {
filter, err := db_table_filter.NewFilter(
strings.Split(f.Databases, ","),
strings.Split(f.Tables, ","),
strings.Split(f.ExcludeDatabases, ","),
strings.Split(f.ExcludeTables, ","),
)
if err != nil {
return err
}
f.Regex = filter.TableFilterRegex()
}
//logger.Log.Warnf("filter type 'form' will ignore regex=%s or tables-list=%s", f.Regex, f.TablesList)
} else {
if f.Regex != "" && f.TablesList != "" {
return errors.Errorf("regex and tables-list filter cannot be used together")
Expand All @@ -85,9 +97,7 @@ func (f *TableFilter) BuildArgsTableFilterForMydumper() (args []string, err erro
if databases == "" {
databases = "*"
}
if excludeTables == "" && excludeDatabases != "" {
excludeTables = "*"
}

dbList := strings.Split(databases, ",")
tbList := strings.Split(tables, ",")
dbListExclude := strings.Split(excludeDatabases, ",")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@ import (
"strconv"
"strings"

"github.com/pkg/errors"

"dbm-services/mysql/db-tools/dbactuator/pkg/util"
"dbm-services/mysql/db-tools/mysql-dbbackup/pkg/src/logger"
)

// GetDatadir Get the datadir of mysql server
func GetDatadir(port int) (string, error) {
cnfFileName := util.GetMyCnfFileName(port)
cnfFile := &util.CnfFile{FileName: cnfFileName}
if err := cnfFile.Load(); err != nil {
return "", errors.WithMessage(err, "get data dir")
}
return cnfFile.GetMySQLDataDir()

cmdStr := "ps -ef|grep mysqld|grep " + strconv.Itoa(port) + "|grep datadir|grep -o '\\-\\-datadir=\\S*'"
res, err := exec.Command("/bin/bash", "-c", cmdStr).CombinedOutput()
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func GetStorageEngine(dbh *sql.DB) (string, error) {
return version[0], nil
}

// GetDataDir get datadir from mysql server
// if failed, try to get datadir from my.cnf -- todo not implemented
func GetDataDir(dbh *sql.DB, myCnfPath string) (string, error) {
dataDir, err := MysqlSingleColumnQuery("select @@datadir", dbh)
if err != nil {
logger.Log.Error("can't select @@datadir, error :", err)
return "", err
}
return dataDir[0], nil
}

// GetDatabases show databases like
// like == ” means all
func GetDatabases(like string, dbh *sql.DB) ([]string, error) {
Expand Down
1 change: 0 additions & 1 deletion dbm-services/sqlserver/db-tools/dbactuator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/go-playground/universal-translator v0.18.1
github.com/go-playground/validator/v10 v10.14.1
github.com/gofrs/flock v0.8.1
github.com/golang-jwt/jwt/v4 v4.5.0
github.com/jmoiron/sqlx v1.3.5
github.com/juju/ratelimit v1.0.2
github.com/pkg/errors v0.9.1
Expand Down
2 changes: 0 additions & 2 deletions dbm-services/sqlserver/db-tools/dbactuator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@ github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfC
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw=
github.com/gofrs/flock v0.8.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY=
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0=
github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A=
Expand Down

0 comments on commit b06e8c2

Please sign in to comment.