-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
261 lines (209 loc) · 6.07 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
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
package main
import (
"bufio"
"database/sql"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/fatih/color"
"github.com/go-sql-driver/mysql"
"github.com/greg0/go-mysqldump"
"gopkg.in/yaml.v2"
)
const (
// Info messages
Info = 1 << iota // a == 1 (iota has been reset)
// Warning Messages
Warning = 1 << iota // b == 2
// Error Messages
Error = 1 << iota // c == 4
cName = "dump"
cAddr = "127.0.0.1:3306"
cUsr = "root"
cPwd = "root"
)
type ConnectionConfig struct {
ConnName string `yaml:"name"`
Address string `yaml:"address"`
UserName string `yaml:"username"`
Password string `yaml:"password"`
Database string `yaml:"dbname"`
}
// Options model for commandline arguments
type Options struct {
ConnName string
Address string
UserName string
Password string
Database string
IgnoredTables []string
StructureOnlyTables []string
OutputDirectory string
PacketSize int
}
func main() {
options := GetOptions()
// Open connection to database
config := mysql.NewConfig()
config.User = options.UserName
config.Passwd = options.Password
config.DBName = options.Database
config.Net = "tcp"
config.Addr = options.Address
db, err := sql.Open("mysql", config.FormatDSN())
if err != nil {
fmt.Println("Error opening database: ", err)
return
}
dumpFilenameFormat := fmt.Sprintf("%s-%s-2006-01-02T15:04:05", options.ConnName, config.DBName) // accepts time layout string and add .sql at the end of file
// Register database with mysqldump
dumper, err := mysqldump.Register(
db,
options.OutputDirectory,
dumpFilenameFormat,
options.StructureOnlyTables,
options.IgnoredTables,
options.PacketSize,
)
if err != nil {
fmt.Println("Error registering databse:", err)
return
}
// Dump database to file
err = dumper.Dump()
if err != nil {
fmt.Println("Error dumping:", err)
fmt.Println("Removing dump file")
os.Remove(dumper.File.Name())
return
}
fmt.Printf("File is saved to %s/%s", options.OutputDirectory, dumpFilenameFormat)
// Close dumper, connected database and file stream.
dumper.Close()
}
func GetOptions() *Options {
var connection string
flag.StringVar(&connection, "connection", "", "Yaml config with connection parameters. Overrides flag arguments. Optional")
var connName string
flag.StringVar(&connName, "name", cName, "Dump name. Default 'dump' ")
var address string
flag.StringVar(&address, "addr", cAddr, "Database address host:port")
var username string
flag.StringVar(&username, "user", cUsr, "Database username")
var password string
flag.StringVar(&password, "pass", cPwd, "Database password")
var database string
flag.StringVar(&database, "dbname", "", "Database name")
var ignoredTables string
flag.StringVar(&ignoredTables, "ignore", "", "File path containing list of ignored tables. Optional. File can contains regex expressions. Each expresion in new line")
var structOnlyTables string
flag.StringVar(&structOnlyTables, "structOnly", "", "File path containing list of ignored tables. Optional. File can contains regex expressions. Each expresion in new line")
var outputdir string
flag.StringVar(&outputdir, "output", "", "Dump output dir. Default is current working directory")
var packetSize int
flag.IntVar(&packetSize, "packetSize", 1048576, "Max allowed packet size. Default 1048576 (1MB)")
if len(os.Args) <= 1 || os.Args[1] == "--help" {
flag.PrintDefaults()
os.Exit(1)
}
flag.Parse()
if connection != "" {
var c ConnectionConfig
c.loadFile(connection)
connName = c.ConnName
address = c.Address
username = c.UserName
password = c.Password
database = c.Database
}
if outputdir == "" {
dir, err := os.Getwd()
checkErr(err)
outputdir = dir
}
if database == "" {
printMessage("Database name can't be empty", Error)
os.Exit(1)
}
ignoredTablesArray := []string{}
if ignoredTables != "" {
ignoredTablesArray = FillArrayWithFileLines(ignoredTables)
}
structOnlyTablesArray := []string{}
if structOnlyTables != "" {
structOnlyTablesArray = FillArrayWithFileLines(structOnlyTables)
}
var o Options
opts := o.create(
connName,
address,
username,
password,
database,
ignoredTablesArray,
structOnlyTablesArray,
outputdir,
packetSize)
stropts, _ := json.MarshalIndent(opts, "", "\t")
printMessage("Running with parameters", Info)
printMessage(string(stropts), Info)
printMessage("Running on operating system : "+runtime.GOOS, Info)
return opts
}
func FillArrayWithFileLines(filePath string) []string {
file, err := os.Open(filePath)
checkErr(err)
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var array []string
for scanner.Scan() {
array = append(array, scanner.Text())
}
return array
}
func (o *Options) create(name string, address string, username string, password string, database string, ignoredTables []string, structOnlyTables []string, outputDir string, packetSize int) *Options {
database = strings.Replace(database, " ", "", -1)
database = strings.Replace(database, " , ", ",", -1)
database = strings.Replace(database, ", ", ",", -1)
database = strings.Replace(database, " ,", ",", -1)
return &Options{
ConnName: name,
Address: address,
UserName: username,
Password: password,
Database: database,
IgnoredTables: ignoredTables,
StructureOnlyTables: structOnlyTables,
OutputDirectory: outputDir,
PacketSize: packetSize,
}
}
func printMessage(message string, messageType int) {
colors := map[int]color.Attribute{Info: color.FgGreen, Warning: color.FgHiYellow, Error: color.FgHiRed}
color.Set(colors[messageType])
fmt.Println(message)
color.Unset()
}
func checkErr(err error) {
if err != nil {
color.Set(color.FgHiRed)
panic(err)
color.Unset()
}
}
func (c *ConnectionConfig) loadFile(filePath string) *ConnectionConfig {
c.ConnName = cName
c.Address = cAddr
c.UserName = cUsr
c.Password = cPwd
yamlFile, err := ioutil.ReadFile(filePath)
checkErr(err)
err = yaml.Unmarshal(yamlFile, c)
checkErr(err)
return c
}