Skip to content

Commit

Permalink
mydumper: support config file
Browse files Browse the repository at this point in the history
  • Loading branch information
BohuTANG committed Aug 15, 2019
1 parent af49192 commit 6e2ca22
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 104 deletions.
30 changes: 5 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,13 @@ $make test
### mydumper

```
./bin/mydumper --help
Usage: ./bin/mydumper -h [HOST] -P [PORT] -u [USER] -p [PASSWORD] -db [DATABASE] -o [OUTDIR]
-F int
Split tables into chunks of this output file size. This value is in MB (default 128)
-P int
TCP/IP port to connect to (default 3306)
-db string
Database to dump
-h string
The host to connect to
-o string
Directory to output files to
-p string
User password
-s int
Attempted size of INSERT statement in bytes (default 1000000)
-t int
Number of threads to use (default 16)
-table string
Table to dump
-u string
Username with privileges to run the dump
-vars string
Session variables
./bin/mydumper -h
Usage: ./bin/mydumper -c conf/mydumper.ini.sample
-c string
config file
Examples:
$./bin/mydumper -h 192.168.0.1 -P 3306 -u mock -p mock -db sbtest -o sbtest.sql
$./bin/mydumper -c conf/mydumper.ini.sample
2017/10/25 13:12:52.933391 dumper.go:35: [INFO] dumping.database[sbtest].schema...
2017/10/25 13:12:52.937743 dumper.go:45: [INFO] dumping.table[sbtest.benchyou0].schema...
2017/10/25 13:12:52.937791 dumper.go:168: [INFO] dumping.table[sbtest.benchyou0].datas.thread[1]...
Expand Down
13 changes: 13 additions & 0 deletions conf/mydumper.ini.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[mysql]
# The host to connect to
host = 127.0.0.1
# TCP/IP port to conect to
port = 3306
# Username with privileges to run the dump
user = root
# User password
password = pwd
# Database to dump
database = xx
# Directory to dump files to
outdir = ./dumper-sql
7 changes: 4 additions & 3 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ all: get build test
get:
@echo "--> go get..."
go get github.com/xelabs/go-mysqlstack/driver
go get github.com/dlintw/goconf
go get github.com/stretchr/testify/assert
go get github.com/pierrre/gotestcover

build:
@$(MAKE) get
@echo "--> Building..."
go build -v -o bin/mydumper src/mydumper/main.go
go build -v -o bin/myloader src/myloader/main.go
go build -v -o bin/mystreamer src/mystreamer/main.go
go build -v -o bin/mydumper src/cmd/mydumper.go src/cmd/config.go
go build -v -o bin/myloader src/cmd/myloader.go
go build -v -o bin/mystreamer src/cmd/mystreamer.go
@chmod 755 bin/*

clean:
Expand Down
66 changes: 66 additions & 0 deletions src/cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* go-mydumper
* xelabs.org
*
* Copyright (c) XeLabs
* GPL License
*
*/

package main

import (
"common"
"fmt"

ini "github.com/dlintw/goconf"
)

func parseConfig(file string) (*common.Args, error) {
args := &common.Args{}

cfg, err := ini.ReadConfigFile(file)
if err != nil {
return nil, err
}

host, err := cfg.GetString("mysql", "host")
if err != nil {
return nil, err
}
port, err := cfg.GetInt("mysql", "port")
if err != nil {
return nil, err
}
user, err := cfg.GetString("mysql", "user")
if err != nil {
return nil, err
}
password, err := cfg.GetString("mysql", "password")
if err != nil {
return nil, err
}
database, err := cfg.GetString("mysql", "database")
if err != nil {
return nil, err
}
outdir, err := cfg.GetString("mysql", "outdir")
if err != nil {
return nil, err
}
table, _ := cfg.GetString("mysql", "table")
sessionVars, _ := cfg.GetString("mysql", "vars")

args.Address = fmt.Sprintf("%s:%d", host, port)
args.User = user
args.Password = password
args.Database = database
args.Table = table
args.Outdir = outdir
args.ChunksizeInMB = 128
args.Threads = 16
args.StmtSize = 1000000
args.IntervalMs = 10 * 1000
args.SessionVars = sessionVars
return args, nil
}
54 changes: 54 additions & 0 deletions src/cmd/mydumper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* go-mydumper
* xelabs.org
*
* Copyright (c) XeLabs
* GPL License
*
*/

package main

import (
"common"
"flag"
"fmt"
"os"

"github.com/xelabs/go-mysqlstack/xlog"
)

var (
flagConfig string

log = xlog.NewStdLog(xlog.Level(xlog.INFO))
)

func init() {
flag.StringVar(&flagConfig, "c", "", "config file")
}

func usage() {
fmt.Println("Usage: " + os.Args[0] + " -c conf/mydumper.ini.sample")
flag.PrintDefaults()
}

func main() {
flag.Usage = func() { usage() }
flag.Parse()

if flagConfig == "" {
usage()
os.Exit(0)
}

args, err := parseConfig(flagConfig)
common.AssertNil(err)

if _, err := os.Stat(args.Outdir); os.IsNotExist(err) {
x := os.MkdirAll(args.Outdir, 0777)
common.AssertNil(x)
}

common.Dumper(log, args)
}
File renamed without changes.
File renamed without changes.
76 changes: 0 additions & 76 deletions src/mydumper/main.go

This file was deleted.

0 comments on commit 6e2ca22

Please sign in to comment.