forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (92 loc) · 2.78 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/siddontang/go-mysql/canal"
"github.com/siddontang/go-mysql/mysql"
)
var host = flag.String("host", "127.0.0.1", "MySQL host")
var port = flag.Int("port", 3306, "MySQL port")
var user = flag.String("user", "root", "MySQL user, must have replication privilege")
var password = flag.String("password", "", "MySQL password")
var flavor = flag.String("flavor", "mysql", "Flavor: mysql or mariadb")
var serverID = flag.Int("server-id", 101, "Unique Server ID")
var mysqldump = flag.String("mysqldump", "mysqldump", "mysqldump execution path")
var dbs = flag.String("dbs", "test", "dump databases, seperated by comma")
var tables = flag.String("tables", "", "dump tables, seperated by comma, will overwrite dbs")
var tableDB = flag.String("table_db", "test", "database for dump tables")
var ignoreTables = flag.String("ignore_tables", "", "ignore tables, must be database.table format, separated by comma")
var startName = flag.String("bin_name", "", "start sync from binlog name")
var startPos = flag.Uint("bin_pos", 0, "start sync from binlog position of")
var heartbeatPeriod = flag.Duration("heartbeat", 60*time.Second, "master heartbeat period")
var readTimeout = flag.Duration("read_timeout", 90*time.Second, "connection read timeout")
func main() {
flag.Parse()
cfg := canal.NewDefaultConfig()
cfg.Addr = fmt.Sprintf("%s:%d", *host, *port)
cfg.User = *user
cfg.Password = *password
cfg.Flavor = *flavor
cfg.UseDecimal = true
cfg.ReadTimeout = *readTimeout
cfg.HeartbeatPeriod = *heartbeatPeriod
cfg.ServerID = uint32(*serverID)
cfg.Dump.ExecutionPath = *mysqldump
cfg.Dump.DiscardErr = false
c, err := canal.NewCanal(cfg)
if err != nil {
fmt.Printf("create canal err %v", err)
os.Exit(1)
}
if len(*ignoreTables) > 0 {
subs := strings.Split(*ignoreTables, ",")
for _, sub := range subs {
if seps := strings.Split(sub, "."); len(seps) == 2 {
c.AddDumpIgnoreTables(seps[0], seps[1])
}
}
}
if len(*tables) > 0 && len(*tableDB) > 0 {
subs := strings.Split(*tables, ",")
c.AddDumpTables(*tableDB, subs...)
} else if len(*dbs) > 0 {
subs := strings.Split(*dbs, ",")
c.AddDumpDatabases(subs...)
}
c.SetEventHandler(&handler{})
startPos := mysql.Position{
Name: *startName,
Pos: uint32(*startPos),
}
go func() {
err = c.RunFrom(startPos)
if err != nil {
fmt.Printf("start canal err %v", err)
}
}()
sc := make(chan os.Signal, 1)
signal.Notify(sc,
os.Kill,
os.Interrupt,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
<-sc
c.Close()
}
type handler struct {
canal.DummyEventHandler
}
func (h *handler) OnRow(e *canal.RowsEvent) error {
fmt.Printf("%v\n", e)
return nil
}
func (h *handler) String() string {
return "TestHandler"
}