-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheckData.go
More file actions
263 lines (254 loc) · 16.1 KB
/
checkData.go
File metadata and controls
263 lines (254 loc) · 16.1 KB
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
package main
import (
"checkData/check"
"checkData/model"
"fmt"
"github.com/gookit/slog"
"github.com/urfave/cli/v2"
"log"
//_ "net/http/pprof"
"os"
)
func version() {
text := `
####################################################################################################
# Name : checkData
# Author : Elison
# Email : Ly99@qq.com
# Description : 核对迁移或同步的数据库两端的数据是否一致,支持mysql(tidb/doris)、pgsql、mongo等多种数据库
# Updates :
# Version When What
# -------- ----------- -----------------------------------------------------------------
# v2.0 2023-01-20 优化性能,使用golang替换python
# v2.1.0 2023-02-10 为了兼容其他数据库,使用interface重构Checker逻辑
# v2.1.1 2023-02-12 增加mongo核对功能
# v2.1.2 2023-02-13 增加pgsql核对功能
# v2.1.3 2024-11-23 优化checker逻辑,修复bug
# v2.1.4 2025-01-14 修复count模式下检查主键bug
# v2.1.5 2025-01-27 增加sql server核对功能(用到concat函数,需要2012以上版本)
# v2.1.6 2025-03-10 添加oceanbase
# v2.1.7 2025-07-19 修复bug:复核逻辑和导数逻辑
# v2.1.8 2025-08-15 优化内存
####################################################################################################
`
fmt.Println(text)
}
func GetOptions(ctx *cli.Context) *model.Options {
opt := model.Options{}
opt.Source = ctx.String("source")
opt.Target = ctx.String("target")
opt.User = ctx.String("user")
opt.Password = ctx.String("password")
opt.TargetUser = ctx.String("target-user")
opt.TargetPassword = ctx.String("target-password")
opt.Mode = ctx.String("mode")
opt.Db = ctx.String("db")
opt.Tables = ctx.String("tables")
opt.Where = ctx.String("where")
opt.SkipCols = ctx.String("skipcols")
opt.SkipTables = ctx.String("skiptables")
opt.Keys = ctx.String("keys")
opt.Parallel = ctx.Int("parallel")
opt.MaxRecheckTimes = ctx.Int("max-recheck-times")
opt.MaxRecheckRows = ctx.Int("max-recheck-rows")
opt.Init()
return &opt
}
func main() {
//禁用颜色显示
slog.Configure(func(logger *slog.SugaredLogger) {
f := logger.Formatter.(*slog.TextFormatter)
f.EnableColor = false
})
//诊断内存泄漏
//go func() {
// _ = http.ListenAndServe(":9000", nil)
//}()
app := &cli.App{
Name: "./checkData",
Usage: "check data tools",
Commands: []*cli.Command{
{
Name: "version",
Usage: "show the version",
Action: func(ctx *cli.Context) error {
version()
return nil
},
},
{
Name: "mysql",
Usage: "check data from mysql/doris/tidb",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Aliases: []string{"S"}, Required: true, Usage: "The host and port of the source instance, e.g., 10.0.0.201:3306"},
&cli.StringFlag{Name: "target", Aliases: []string{"T"}, Required: true, Usage: "The host and port of the target instance, e.g., 10.0.0.202:3306"},
&cli.StringFlag{Name: "user", Aliases: []string{"u"}, Required: true, Usage: "Login user"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true, Usage: "Login password"},
&cli.StringFlag{Name: "target-user", Aliases: []string{"tu"}, Usage: "Login user of target"},
&cli.StringFlag{Name: "target-password", Aliases: []string{"tp"}, Usage: "Login password of target"},
&cli.StringFlag{Name: "mode", Aliases: []string{"m"}, Value: "fast", Usage: "mode:[fast|slow|count]\n fast: fast check,the database must surport crc32 functionn\n slow: high compatibility, to work on doris/tidb\n count: only check row count"},
&cli.StringFlag{Name: "db", Aliases: []string{"d"}, Required: true, Usage: "dbname,e.g., db1,db2 or db1:db01,db2:db02(use a colon separate these diferent database names of the source and target)"},
&cli.StringFlag{Name: "tables", Aliases: []string{"t"}, Usage: "These tables to check, e.g., users,orders"},
&cli.StringFlag{Name: "where", Aliases: []string{"w"}, Usage: "filter condition, e.g., update_time<curdate()"},
&cli.StringFlag{Name: "keys", Aliases: []string{"k"}, Usage: "These keys using to check, must be unique"},
&cli.StringFlag{Name: "skip-tables", Usage: "These tables to skip check"},
&cli.StringFlag{Name: "skip-cols", Usage: "These columns to skip check, to skip some big columns become faster"},
&cli.IntFlag{Name: "parallel", Aliases: []string{"P"}, Value: 2, Usage: "Parallel"},
&cli.IntFlag{Name: "max-recheck-times", Value: 3, Usage: "The number of recheck times"},
&cli.IntFlag{Name: "max-recheck-rows", Value: 1000, Usage: "No recheck while the number of the found different during check greater than the max-recheck-rows valuse"},
},
Action: func(ctx *cli.Context) error {
opt := GetOptions(ctx)
opt.DbType = "mysql"
check.Start(opt)
return nil
},
},
{
Name: "doris",
Usage: "check data from doris",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Aliases: []string{"S"}, Required: true, Usage: "The host and port of the source instance, e.g., 10.0.0.201:3306"},
&cli.StringFlag{Name: "target", Aliases: []string{"T"}, Required: true, Usage: "The host and port of the target instance, e.g., 10.0.0.202:3306"},
&cli.StringFlag{Name: "user", Aliases: []string{"u"}, Required: true, Usage: "Login user"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true, Usage: "Login password"},
&cli.StringFlag{Name: "target-user", Aliases: []string{"tu"}, Usage: "Login user of target"},
&cli.StringFlag{Name: "target-password", Aliases: []string{"tp"}, Usage: "Login password of target"},
&cli.StringFlag{Name: "mode", Aliases: []string{"m"}, Value: "fast", Usage: "mode:[fast|slow|count]\n fast: fast check,the database must surport crc32 functionn\n slow: high compatibility, to work on doris/tidb\n count: only check row count"},
&cli.StringFlag{Name: "db", Aliases: []string{"d"}, Required: true, Usage: "dbname,e.g., db1,db2 or db1:db01,db2:db02(use a colon separate these diferent database names of the source and target)"},
&cli.StringFlag{Name: "tables", Aliases: []string{"t"}, Usage: "These tables to check, e.g., users,orders"},
&cli.StringFlag{Name: "where", Aliases: []string{"w"}, Usage: "filter condition, e.g., update_time<curdate()"},
&cli.StringFlag{Name: "keys", Aliases: []string{"k"}, Usage: "These keys using to check, must be unique"},
&cli.StringFlag{Name: "skip-tables", Usage: "These tables to skip check"},
&cli.StringFlag{Name: "skip-cols", Usage: "These columns to skip check, to skip some big columns become faster"},
&cli.IntFlag{Name: "parallel", Aliases: []string{"P"}, Value: 2, Usage: "Parallel"},
&cli.IntFlag{Name: "max-recheck-times", Value: 3, Usage: "The number of recheck times"},
&cli.IntFlag{Name: "max-recheck-rows", Value: 1000, Usage: "No recheck while the number of the found different during check greater than the max-recheck-rows valuse"},
},
Action: func(ctx *cli.Context) error {
opt := GetOptions(ctx)
opt.DbType = "doris"
check.Start(opt)
return nil
},
},
{
Name: "oceanbase",
Usage: "check data from oceanbase",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Aliases: []string{"S"}, Required: true, Usage: "The host and port of the source instance, e.g., 10.0.0.201:3306"},
&cli.StringFlag{Name: "target", Aliases: []string{"T"}, Required: true, Usage: "The host and port of the target instance, e.g., 10.0.0.202:3306"},
&cli.StringFlag{Name: "user", Aliases: []string{"u"}, Required: true, Usage: "Login user"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true, Usage: "Login password"},
&cli.StringFlag{Name: "target-user", Aliases: []string{"tu"}, Usage: "Login user of target"},
&cli.StringFlag{Name: "target-password", Aliases: []string{"tp"}, Usage: "Login password of target"},
&cli.StringFlag{Name: "mode", Aliases: []string{"m"}, Value: "fast", Usage: "mode:[fast|slow|count]\n fast: fast check,the database must surport crc32 functionn\n slow: high compatibility, to work on doris/tidb\n count: only check row count"},
&cli.StringFlag{Name: "db", Aliases: []string{"d"}, Required: true, Usage: "dbname,e.g., db1,db2 or db1:db01,db2:db02(use a colon separate these diferent database names of the source and target)"},
&cli.StringFlag{Name: "tables", Aliases: []string{"t"}, Usage: "These tables to check, e.g., users,orders"},
&cli.StringFlag{Name: "where", Aliases: []string{"w"}, Usage: "filter condition, e.g., update_time<curdate()"},
&cli.StringFlag{Name: "keys", Aliases: []string{"k"}, Usage: "These keys using to check, must be unique"},
&cli.StringFlag{Name: "skip-tables", Usage: "These tables to skip check"},
&cli.StringFlag{Name: "skip-cols", Usage: "These columns to skip check, to skip some big columns become faster"},
&cli.IntFlag{Name: "parallel", Aliases: []string{"P"}, Value: 2, Usage: "Parallel"},
&cli.IntFlag{Name: "max-recheck-times", Value: 3, Usage: "The number of recheck times"},
&cli.IntFlag{Name: "max-recheck-rows", Value: 1000, Usage: "No recheck while the number of the found different during check greater than the max-recheck-rows valuse"},
},
Action: func(ctx *cli.Context) error {
opt := GetOptions(ctx)
opt.DbType = "oceanbase"
check.Start(opt)
return nil
},
},
{
Name: "mongo",
Usage: "check data from mongo",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Aliases: []string{"S"}, Required: true, Usage: "The host and port of the source instance, e.g., 10.0.0.201:27017"},
&cli.StringFlag{Name: "target", Aliases: []string{"T"}, Required: true, Usage: "The host and port of the target instance, e.g., 10.0.0.202:27017"},
&cli.StringFlag{Name: "user", Aliases: []string{"u"}, Required: true, Usage: "The login user which authenticationDatabase is admin"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true, Usage: "Login password"},
&cli.StringFlag{Name: "target-user", Aliases: []string{"tu"}, Usage: "Login user of target"},
&cli.StringFlag{Name: "target-password", Aliases: []string{"tp"}, Usage: "Login password of target"},
&cli.StringFlag{Name: "mode", Aliases: []string{"m"}, Value: "fast", Usage: "mode:[fast|slow|count]\n fast: fast check,the database must surport crc32 functionn\n slow: high compatibility, to work on doris/tidb\n count: only check row count"},
&cli.StringFlag{Name: "db", Aliases: []string{"d"}, Required: true, Usage: "dbname,e.g., db1,db2 or db1:db01,db2:db02(use a colon separate these diferent database names of the source and target)"},
&cli.StringFlag{Name: "tables", Aliases: []string{"t"}, Usage: "These tables to check, e.g., users,orders"},
&cli.StringFlag{Name: "skip-tables", Usage: "These tables to skip check"},
&cli.IntFlag{Name: "parallel", Aliases: []string{"P"}, Value: 2, Usage: "Parallel"},
&cli.IntFlag{Name: "max-recheck-times", Value: 3, Usage: "The number of recheck times"},
&cli.IntFlag{Name: "max-recheck-rows", Value: 1000, Usage: "No recheck while the number of the found different during check greater than the max-recheck-rows valuse"},
},
Action: func(ctx *cli.Context) error {
opt := GetOptions(ctx)
//执行主任务
opt.DbType = "mongo"
check.Start(opt)
return nil
},
},
{
Name: "pgsql",
Usage: "check data from postgresql",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Aliases: []string{"S"}, Required: true, Usage: "The host and port of the source instance, e.g., 10.0.0.201:5432"},
&cli.StringFlag{Name: "target", Aliases: []string{"T"}, Required: true, Usage: "The host and port of the target instance, e.g., 10.0.0.202:5432"},
&cli.StringFlag{Name: "user", Aliases: []string{"u"}, Required: true, Usage: "Login user"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true, Usage: "Login password"},
&cli.StringFlag{Name: "target-user", Aliases: []string{"tu"}, Usage: "Login user of target"},
&cli.StringFlag{Name: "target-password", Aliases: []string{"tp"}, Usage: "Login password of target"},
&cli.StringFlag{Name: "mode", Aliases: []string{"m"}, Value: "fast", Usage: "mode:[fast|slow|count]\n fast: fast check,the database must surport crc32 functionn\n slow: high compatibility, to work on doris/tidb\n count: only check row count"},
&cli.StringFlag{Name: "db", Aliases: []string{"d"}, Required: true, Usage: "dbname,e.g., db1,db2 or db1:db01,db2:db02(use a colon separate these diferent database names of the source and target)"},
&cli.StringFlag{Name: "tables", Aliases: []string{"t"}, Usage: "These full names to check table, e.g., public.users,public.orders"},
&cli.StringFlag{Name: "where", Aliases: []string{"w"}, Usage: "filter condition, e.g., update_time<curdate()"},
&cli.StringFlag{Name: "keys", Aliases: []string{"k"}, Usage: "These keys using to check, must be unique"},
&cli.StringFlag{Name: "skip-tables", Usage: "These tables to skip check"},
&cli.StringFlag{Name: "skip-cols", Usage: "These columns to skip check, to skip some big columns become faster"},
&cli.IntFlag{Name: "parallel", Aliases: []string{"P"}, Value: 2, Usage: "Parallel"},
&cli.IntFlag{Name: "max-recheck-times", Value: 3, Usage: "The number of recheck times"},
&cli.IntFlag{Name: "max-recheck-rows", Value: 1000, Usage: "No recheck while the number of the found different during check greater than the max-recheck-rows valuse"},
},
Action: func(ctx *cli.Context) error {
//初始化参数
opt := GetOptions(ctx)
//执行主任务
opt.DbType = "pgsql"
check.Start(opt)
return nil
},
},
{
Name: "mssql",
Usage: "check data from sql server",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Aliases: []string{"S"}, Required: true, Usage: "The host and port of the source instance, e.g., 10.0.0.201:1433"},
&cli.StringFlag{Name: "target", Aliases: []string{"T"}, Required: true, Usage: "The host and port of the target instance, e.g., 10.0.0.202:1433"},
&cli.StringFlag{Name: "user", Aliases: []string{"u"}, Required: true, Usage: "Login user"},
&cli.StringFlag{Name: "password", Aliases: []string{"p"}, Required: true, Usage: "Login password"},
&cli.StringFlag{Name: "target-user", Aliases: []string{"tu"}, Usage: "Login user of target"},
&cli.StringFlag{Name: "target-password", Aliases: []string{"tp"}, Usage: "Login password of target"},
&cli.StringFlag{Name: "mode", Aliases: []string{"m"}, Value: "fast", Usage: "mode:[fast|slow|count]\n fast: fast check,the database must surport crc32 functionn\n slow: high compatibility, to work on doris/tidb\n count: only check row count"},
&cli.StringFlag{Name: "db", Aliases: []string{"d"}, Required: true, Usage: "dbname,e.g., db1,db2 or db1:db01,db2:db02(use a colon separate these diferent database names of the source and target)"},
&cli.StringFlag{Name: "tables", Aliases: []string{"t"}, Usage: "These full names to check table, e.g., public.users,public.orders"},
&cli.StringFlag{Name: "where", Aliases: []string{"w"}, Usage: "filter condition, e.g., update_time<curdate()"},
&cli.StringFlag{Name: "keys", Aliases: []string{"k"}, Usage: "These keys using to check, must be unique"},
&cli.StringFlag{Name: "skip-tables", Usage: "These tables to skip check"},
&cli.StringFlag{Name: "skip-cols", Usage: "These columns to skip check, to skip some big columns become faster"},
&cli.IntFlag{Name: "parallel", Aliases: []string{"P"}, Value: 2, Usage: "Parallel"},
&cli.IntFlag{Name: "max-recheck-times", Value: 3, Usage: "The number of recheck times"},
&cli.IntFlag{Name: "max-recheck-rows", Value: 1000, Usage: "No recheck while the number of the found different during check greater than the max-recheck-rows valuse"},
},
Action: func(ctx *cli.Context) error {
//初始化参数
opt := GetOptions(ctx)
//执行主任务
opt.DbType = "mssql"
check.Start(opt)
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}