forked from go-mysql-org/go-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dumper.go
267 lines (210 loc) · 6.29 KB
/
dumper.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package dump
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
. "github.com/dollarkillerx/go-mysql/mysql"
"github.com/pingcap/errors"
"github.com/siddontang/go-log/log"
)
// Unlick mysqldump, Dumper is designed for parsing and syning data easily.
type Dumper struct {
// mysqldump execution path, like mysqldump or /usr/bin/mysqldump, etc...
ExecutionPath string
Addr string
User string
Password string
Protocol string
// Will override Databases
Tables []string
TableDB string
Databases []string
Where string
Charset string
IgnoreTables map[string][]string
ExtraOptions []string
ErrOut io.Writer
masterDataSkipped bool
maxAllowedPacket int
hexBlob bool
// see detectColumnStatisticsParamSupported
isColumnStatisticsParamSupported bool
}
func NewDumper(executionPath string, addr string, user string, password string) (*Dumper, error) {
if len(executionPath) == 0 {
return nil, nil
}
path, err := exec.LookPath(executionPath)
if err != nil {
return nil, errors.Trace(err)
}
d := new(Dumper)
d.ExecutionPath = path
d.Addr = addr
d.User = user
d.Password = password
d.Tables = make([]string, 0, 16)
d.Databases = make([]string, 0, 16)
d.Charset = DEFAULT_CHARSET
d.IgnoreTables = make(map[string][]string)
d.ExtraOptions = make([]string, 0, 5)
d.masterDataSkipped = false
d.isColumnStatisticsParamSupported = d.detectColumnStatisticsParamSupported()
d.ErrOut = os.Stderr
return d, nil
}
// New mysqldump versions try to send queries to information_schema.COLUMN_STATISTICS table which does not exist in old MySQL (<5.x).
// And we got error: "Unknown table 'COLUMN_STATISTICS' in information_schema (1109)".
//
// mysqldump may not send this query if it is started with parameter --column-statistics.
// But this parameter exists only for versions >=8.0.2 (https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-2.html).
//
// For environments where the version of mysql-server and mysqldump differs, we try to check this parameter and use it if available.
func (d *Dumper) detectColumnStatisticsParamSupported() bool {
out, err := exec.Command(d.ExecutionPath, `--help`).CombinedOutput()
if err != nil {
return false
}
return bytes.Contains(out, []byte(`--column-statistics`))
}
func (d *Dumper) SetCharset(charset string) {
d.Charset = charset
}
func (d *Dumper) SetProtocol(protocol string) {
d.Protocol = protocol
}
func (d *Dumper) SetWhere(where string) {
d.Where = where
}
func (d *Dumper) SetExtraOptions(options []string) {
d.ExtraOptions = options
}
func (d *Dumper) SetErrOut(o io.Writer) {
d.ErrOut = o
}
// SkipMasterData: In some cloud MySQL, we have no privilege to use `--master-data`.
func (d *Dumper) SkipMasterData(v bool) {
d.masterDataSkipped = v
}
func (d *Dumper) SetMaxAllowedPacket(i int) {
d.maxAllowedPacket = i
}
func (d *Dumper) SetHexBlob(v bool) {
d.hexBlob = v
}
func (d *Dumper) AddDatabases(dbs ...string) {
d.Databases = append(d.Databases, dbs...)
}
func (d *Dumper) AddTables(db string, tables ...string) {
if d.TableDB != db {
d.TableDB = db
d.Tables = d.Tables[0:0]
}
d.Tables = append(d.Tables, tables...)
}
func (d *Dumper) AddIgnoreTables(db string, tables ...string) {
t, _ := d.IgnoreTables[db]
t = append(t, tables...)
d.IgnoreTables[db] = t
}
func (d *Dumper) Reset() {
d.Tables = d.Tables[0:0]
d.TableDB = ""
d.IgnoreTables = make(map[string][]string)
d.Databases = d.Databases[0:0]
d.Where = ""
}
func (d *Dumper) Dump(w io.Writer) error {
args := make([]string, 0, 16)
// Common args
if strings.Contains(d.Addr, "/") {
args = append(args, fmt.Sprintf("--socket=%s", d.Addr))
} else {
seps := strings.SplitN(d.Addr, ":", 2)
args = append(args, fmt.Sprintf("--host=%s", seps[0]))
if len(seps) > 1 {
args = append(args, fmt.Sprintf("--port=%s", seps[1]))
}
}
args = append(args, fmt.Sprintf("--user=%s", d.User))
args = append(args, fmt.Sprintf("--password=%s", d.Password))
if !d.masterDataSkipped {
args = append(args, "--master-data")
}
if d.maxAllowedPacket > 0 {
// mysqldump param should be --max-allowed-packet=%dM not be --max_allowed_packet=%dM
args = append(args, fmt.Sprintf("--max-allowed-packet=%dM", d.maxAllowedPacket))
}
if d.Protocol != "" {
args = append(args, fmt.Sprintf("--protocol=%s", d.Protocol))
}
args = append(args, "--single-transaction")
args = append(args, "--skip-lock-tables")
// Disable uncessary data
args = append(args, "--compact")
args = append(args, "--skip-opt")
args = append(args, "--quick")
// We only care about data
args = append(args, "--no-create-info")
// Multi row is easy for us to parse the data
args = append(args, "--skip-extended-insert")
args = append(args, "--skip-tz-utc")
if d.hexBlob {
// Use hex for the binary type
args = append(args, "--hex-blob")
}
for db, tables := range d.IgnoreTables {
for _, table := range tables {
args = append(args, fmt.Sprintf("--ignore-table=%s.%s", db, table))
}
}
if len(d.Charset) != 0 {
args = append(args, fmt.Sprintf("--default-character-set=%s", d.Charset))
}
if len(d.Where) != 0 {
args = append(args, fmt.Sprintf("--where=%s", d.Where))
}
if len(d.ExtraOptions) != 0 {
args = append(args, d.ExtraOptions...)
}
if d.isColumnStatisticsParamSupported {
args = append(args, `--column-statistics=0`)
}
if len(d.Tables) == 0 && len(d.Databases) == 0 {
args = append(args, "--all-databases")
} else if len(d.Tables) == 0 {
args = append(args, "--databases")
args = append(args, d.Databases...)
} else {
args = append(args, d.TableDB)
args = append(args, d.Tables...)
// If we only dump some tables, the dump data will not have database name
// which makes us hard to parse, so here we add it manually.
_, err := w.Write([]byte(fmt.Sprintf("USE `%s`;\n", d.TableDB)))
if err != nil {
return fmt.Errorf(`could not write USE command: %w`, err)
}
}
log.Infof("exec mysqldump with %v", args)
cmd := exec.Command(d.ExecutionPath, args...)
cmd.Stderr = d.ErrOut
cmd.Stdout = w
return cmd.Run()
}
// DumpAndParse: Dump MySQL and parse immediately
func (d *Dumper) DumpAndParse(h ParseHandler) error {
r, w := io.Pipe()
done := make(chan error, 1)
go func() {
err := Parse(r, h, !d.masterDataSkipped)
_ = r.CloseWithError(err)
done <- err
}()
err := d.Dump(w)
_ = w.CloseWithError(err)
err = <-done
return errors.Trace(err)
}