-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
311 lines (270 loc) · 6.29 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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package main
import (
"bufio"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
)
var tableFlag string
var fileName string
var filePath string
var fileType string
var configFile string
var dbUsername string
var dbPassword string
var dbDatabase string
var dbHostname string
var dbPortNumber string
var backupDate string
var bashCommand string
var importFile string
var includeDate bool
type LaravelEnvFile map[string]string
/**
read value from env file
*/
func ReadPropertiesFile(filename string) (LaravelEnvFile, error) {
config := LaravelEnvFile{}
if len(filename) == 0 {
return config, nil
}
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if equal := strings.Index(line, "="); equal >= 0 {
if key := strings.TrimSpace(line[:equal]); len(key) > 0 {
value := ""
if len(line) > equal {
value = strings.TrimSpace(line[equal+1:])
}
config[key] = value
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil, err
}
return config, nil
}
/**
check if array contains an string value or not
*/
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}
// getValidFileTypes returns an array of acceptable file extensions
func getValidFileTypes() []string {
return []string{"sql", "zip", "gz"}
}
/**
get the output file path
*/
func getOutputFilePath() string {
if includeDate {
return filePath + fileName + backupDate
} else {
return filePath + fileName
}
}
/**
get the sql output file path
*/
func getSqlOutput() string {
return getOutputFilePath() + ".sql"
}
/**
parse input flags
*/
func parseFlags() {
flag.StringVar(&tableFlag, "t", "*", "name of table(s) you want to dump comma separated.")
flag.StringVar(&fileName, "n", "dump", "output file name")
flag.StringVar(&filePath, "p", "", "output file path")
flag.StringVar(&fileType, "f", "sql", "output file type (sql|zip|gz)")
flag.BoolVar(&includeDate, "d", false, "add date to output files")
flag.StringVar(&importFile, "i", "", "input file path")
flag.StringVar(&dbUsername, "db_user", "", "database user name")
flag.StringVar(&dbPassword, "db_pass", "", "database user password")
flag.StringVar(&dbHostname, "db_host", "", "database host")
flag.StringVar(&dbDatabase, "db_name", "", "database name")
flag.StringVar(&dbPortNumber, "db_port", "3306", "database port number")
flag.StringVar(&configFile, "config", "", "database config file")
flag.Parse()
if !contains(getValidFileTypes(), fileType) {
console("invalid file type ")
}
}
func getTableNames() string {
if tableFlag == "*" {
return ""
}
return strings.Replace(tableFlag, ",", " ", 3)
}
/**
create bash command and store it
*/
func runDumpCommand() {
console("dumping database...")
bashCommand = "mysqldump -h" +
dbHostname +
" -P" + dbPortNumber +
" -u " + dbUsername +
" -p" + dbPassword + " " +
dbDatabase + " " +
getTableNames() + " > " +
getSqlOutput()
_, err := exec.Command("sh", "-c", bashCommand).Output()
if err != nil {
console("Error! Export Failed!")
removeMainFile()
os.Exit(0)
}
}
/**
create bash command and store it
*/
func runImportCommand() {
console("importing database...")
extractImportFile()
bashCommand = "mysql -h" +
dbHostname +
" -P" + dbPortNumber +
" -u " + dbUsername +
" -p" + dbPassword + " " +
dbDatabase + " < " +
importFile
executeCommand(bashCommand, "Import Failed!")
// remove the tmp file if exists
os.Remove("tmp.sql")
}
/**
extract the import file if it is gz or zip
*/
func extractImportFile() {
contentType := getFileContentType()
if contentType == "application/x-gzip" || contentType == "application/zip" {
bashCommand = "gunzip -c " + importFile + " > tmp.sql"
executeCommand(bashCommand, "Cannot decompress the file")
importFile = "tmp.sql"
}
}
/**
get importFile content type
*/
func getFileContentType() string {
f, err := os.Open(importFile)
if err != nil {
console("Error! " + importFile + " not found!")
os.Exit(0)
}
defer f.Close()
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err = f.Read(buffer)
if err != nil {
console("Error! Cannot read " + importFile)
os.Exit(0)
}
// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
return contentType
}
/**
compress output file
*/
func runCompressCommand() {
console("compressing dump file... ")
if fileType == "zip" {
bashCommand = "zip " + getOutputFilePath() + ".zip " + getSqlOutput()
executeCommand(bashCommand, "Cannot compress file.")
} else if fileType == "gz" {
bashCommand = "gzip -c " + getSqlOutput() + " > " + getOutputFilePath() + ".gz"
executeCommand(bashCommand, "Cannot compress file.")
}
// remove .sql file
if fileType != "sql" {
removeMainFile()
}
}
/**
remove main file
*/
func removeMainFile() {
err := os.Remove(getSqlOutput())
if err != nil {
console("Error! Cannot Delete .sql file")
os.Exit(0)
}
}
/**
Execute the command and console the error if it has
*/
func executeCommand(bashCommand string, message string) {
_, err := exec.Command("sh", "-c", bashCommand).Output()
if err != nil {
console("Error! " + message)
os.Exit(0)
}
}
/**
read environment file
*/
func readEnv() {
var config, _ = ReadPropertiesFile(configFile)
dbDatabase = config["DB_DATABASE"]
dbHostname = config["DB_HOST"]
dbUsername = config["DB_USERNAME"]
dbPassword = config["DB_PASSWORD"]
dbPortNumber = config["DB_PORT"]
}
/**
get iso Date & time
*/
func isoDate(kebabcase bool) string {
dt := time.Now()
if kebabcase {
var kc = strings.Replace(dt.Format("01-02-2006 15:04:05"), " ", "-", -1)
kc = strings.Replace(kc, ":", "-", -1)
return kc
}
return dt.Format("01-02-2006 15:04:05")
}
/**
console output
*/
func console(message string) {
fmt.Println(isoDate(false) + " :: " + message)
}
func main() {
backupDate = isoDate(true)
parseFlags()
if configFile != "" {
readEnv()
}
if importFile != "" {
runImportCommand()
} else {
runDumpCommand()
if fileType == "zip" || fileType == "gz" {
runCompressCommand()
}
}
console("done!")
}