Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
Allow regex to specify databases to dump
Browse files Browse the repository at this point in the history
  • Loading branch information
fdellwing authored and BohuTANG committed Mar 17, 2020
1 parent 01e1b4d commit 8516ad3
Show file tree
Hide file tree
Showing 5 changed files with 567 additions and 23 deletions.
7 changes: 7 additions & 0 deletions conf/mydumper.ini.sample
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ chunksize = 128
# vars= "xx=xx;xx=xx;"
vars= ""

# Use this to use regexp to control what databases to export. These are optional
[database]
# regexp = ^(mysql|sys|information_schema|performance_schema)$
# As the used regexp lib does not allow for lookarounds, you may use this to invert the whole regexp
# This option should be refactored as soon as a GPLv3 compliant go-pcre lib is found
# invert_regexp = on

# Use this to restrict exported data. These are optional
[where]
# sample_table1 = created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY)
Expand Down
8 changes: 8 additions & 0 deletions src/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,18 @@ func parseDumperConfig(file string) (*common.Args, error) {
}
}

database_regexp, _ := cfg.GetString("database", "regexp")
database_invert_regexp, err := cfg.GetBool("database", "invert_regexp")
if err != nil {
database_invert_regexp = false
}

args.Address = fmt.Sprintf("%s:%d", host, port)
args.User = user
args.Password = password
args.Database = database
args.DatabaseRegexp = database_regexp
args.DatabaseInvertRegexp = database_invert_regexp
args.Table = table
args.Outdir = outdir
args.ChunksizeInMB = chunksizemb
Expand Down
42 changes: 22 additions & 20 deletions src/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,28 @@ import (

// Args tuple.
type Args struct {
User string
Password string
Address string
ToUser string
ToPassword string
ToAddress string
ToDatabase string
ToEngine string
Database string
Table string
Outdir string
SessionVars string
Threads int
ChunksizeInMB int
StmtSize int
Allbytes uint64
Allrows uint64
OverwriteTables bool
Wheres map[string]string
Selects map[string]map[string]string
User string
Password string
Address string
ToUser string
ToPassword string
ToAddress string
ToDatabase string
ToEngine string
Database string
DatabaseRegexp string
DatabaseInvertRegexp bool
Table string
Outdir string
SessionVars string
Threads int
ChunksizeInMB int
StmtSize int
Allbytes uint64
Allrows uint64
OverwriteTables bool
Wheres map[string]string
Selects map[string]map[string]string

// Interval in millisecond.
IntervalMs int
Expand Down
25 changes: 22 additions & 3 deletions src/common/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package common

import (
"fmt"
"regexp"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -166,6 +167,19 @@ func allDatabases(log *xlog.Log, conn *Connection) []string {
return databases
}

func filterDatabases(log *xlog.Log, conn *Connection, filter *regexp.Regexp, invert bool) []string {
qr, err := conn.Fetch("SHOW DATABASES")
AssertNil(err)

databases := make([]string, 0, 128)
for _, t := range qr.Rows {
if (!invert && filter.MatchString(t[0].String())) || (invert && !filter.MatchString(t[0].String())) {
databases = append(databases, t[0].String())
}
}
return databases
}

// Dumper used to start the dumper worker.
func Dumper(log *xlog.Log, args *Args) {
pool, err := NewPool(log, args.Threads, args.Address, args.User, args.Password, args.SessionVars)
Expand All @@ -180,10 +194,15 @@ func Dumper(log *xlog.Log, args *Args) {
conn := pool.Get()
var databases []string
t := time.Now()
if args.Database != "" {
databases = strings.Split(args.Database, ",")
if args.DatabaseRegexp != "" {
r := regexp.MustCompile(args.DatabaseRegexp)
databases = filterDatabases(log, conn, r, args.DatabaseInvertRegexp)
} else {
databases = allDatabases(log, conn)
if args.Database != "" {
databases = strings.Split(args.Database, ",")
} else {
databases = allDatabases(log, conn)
}
}
for _, database := range databases {
dumpDatabaseSchema(log, conn, args, database)
Expand Down
Loading

0 comments on commit 8516ad3

Please sign in to comment.