diff --git a/conf/mydumper.ini.sample b/conf/mydumper.ini.sample index 6a2656e..efadf09 100644 --- a/conf/mydumper.ini.sample +++ b/conf/mydumper.ini.sample @@ -15,3 +15,8 @@ outdir = ./dumper-sql chunksize = 128 # Session variables, split by ; vars= "xx=xx;xx=xx;" + +# Use this to restrict exported data. These are optional +[where] +sample_table1 = created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) +sample_table2 = created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY) diff --git a/src/cmd/config.go b/src/cmd/config.go index 392e092..50e34a4 100644 --- a/src/cmd/config.go +++ b/src/cmd/config.go @@ -17,7 +17,9 @@ import ( ) func parseDumperConfig(file string) (*common.Args, error) { - args := &common.Args{} + args := &common.Args{ + Wheres: make(map[string]string, 0), + } cfg, err := ini.ReadConfigFile(file) if err != nil { @@ -58,6 +60,11 @@ func parseDumperConfig(file string) (*common.Args, error) { } table, _ := cfg.GetString("mysql", "table") + // Options + if err := loadOptions(cfg, "where", args.Wheres); err != nil { + return nil, err + } + args.Address = fmt.Sprintf("%s:%d", host, port) args.User = user args.Password = password @@ -71,3 +78,19 @@ func parseDumperConfig(file string) (*common.Args, error) { args.IntervalMs = 10 * 1000 return args, nil } + +func loadOptions(cfg *ini.ConfigFile, section string, optMap map[string]string) error { + var err error + var opts []string + + if opts, err = cfg.GetOptions(section); err != nil { + return err + } + + for _, key := range opts { + if optMap[key], err = cfg.GetString(section, key); err != nil { + return err + } + } + return nil +} diff --git a/src/common/common.go b/src/common/common.go index 4d69e55..39d312b 100644 --- a/src/common/common.go +++ b/src/common/common.go @@ -37,6 +37,7 @@ type Args struct { Allbytes uint64 Allrows uint64 OverwriteTables bool + Wheres map[string]string // Interval in millisecond. IntervalMs int diff --git a/src/common/dumper.go b/src/common/dumper.go index 13f2556..6605488 100644 --- a/src/common/dumper.go +++ b/src/common/dumper.go @@ -48,8 +48,13 @@ func dumpTableSchema(log *xlog.Log, conn *Connection, args *Args, table string) func dumpTable(log *xlog.Log, conn *Connection, args *Args, table string) { var allBytes uint64 var allRows uint64 + var where string - cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT /*backup*/ * FROM `%s`.`%s`", args.Database, table)) + if v, ok := args.Wheres[table]; ok { + where = fmt.Sprintf(" WHERE %v", v) + } + + cursor, err := conn.StreamFetch(fmt.Sprintf("SELECT * FROM `%s`.`%s` %s", args.Database, table, where)) AssertNil(err) fields := make([]string, 0, 16)