forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (54 loc) · 1.71 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/pingcap/errors"
"github.com/bufferx/go-mysql/dump"
)
var addr = flag.String("addr", "127.0.0.1:3306", "MySQL addr")
var user = flag.String("user", "root", "MySQL user")
var password = flag.String("password", "", "MySQL password")
var execution = flag.String("exec", "mysqldump", "mysqldump execution path")
var output = flag.String("o", "", "dump output, empty for stdout")
var dbs = flag.String("dbs", "", "dump databases, seperated by comma")
var tables = flag.String("tables", "", "dump tables, seperated by comma, will overwrite dbs")
var tableDB = flag.String("table_db", "", "database for dump tables")
var ignoreTables = flag.String("ignore_tables", "", "ignore tables, must be database.table format, separated by comma")
func main() {
flag.Parse()
d, err := dump.NewDumper(*execution, *addr, *user, *password)
if err != nil {
fmt.Printf("Create Dumper error %v\n", errors.ErrorStack(err))
os.Exit(1)
}
if len(*ignoreTables) == 0 {
subs := strings.Split(*ignoreTables, ",")
for _, sub := range subs {
if seps := strings.Split(sub, "."); len(seps) == 2 {
d.AddIgnoreTables(seps[0], seps[1])
}
}
}
if len(*tables) > 0 && len(*tableDB) > 0 {
subs := strings.Split(*tables, ",")
d.AddTables(*tableDB, subs...)
} else if len(*dbs) > 0 {
subs := strings.Split(*dbs, ",")
d.AddDatabases(subs...)
}
var f = os.Stdout
if len(*output) > 0 {
f, err = os.OpenFile(*output, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Printf("Open file error %v\n", errors.ErrorStack(err))
os.Exit(1)
}
}
defer f.Close()
if err = d.Dump(f); err != nil {
fmt.Printf("Dump MySQL error %v\n", errors.ErrorStack(err))
os.Exit(1)
}
}