-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.go
60 lines (50 loc) · 2.03 KB
/
dump.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
package main
import (
"fmt"
"migratetool/models"
"migratetool/utils"
"os"
"os/exec"
"time"
)
func RunMysqldump(command string) error {
err := utils.EnsureDirExists(models.FormData.Directory)
if err != nil {
return err
}
// Generate timestamp in the format YYYYMMDD_HHMMSS
timestamp := time.Now().Format("20060102_150405")
// Build the file name based on the timestamp and migration type
var fileName string
switch models.FormData.MigrationType {
case "schema":
fileName = fmt.Sprintf("%s_db_schema.sql", timestamp)
case "data":
fileName = fmt.Sprintf("%s_db_data.sql", timestamp)
case "both":
fileName = fmt.Sprintf("%s_db_full.sql", timestamp)
default:
return fmt.Errorf("invalid migration type: %s", models.FormData.MigrationType)
}
// Full path of the dump file
filePath := fmt.Sprintf("%s%s", models.FormData.Directory, fileName)
// Construct the mysqldump command based on the migration type
var dumpCommand string
switch models.FormData.MigrationType {
case "schema":
dumpCommand = fmt.Sprintf("%s -u %s --password='%s' -h %s -P %s --no-data %s > %s", command, models.FormData.Username, models.FormData.Password, models.FormData.Host, models.FormData.Port, models.FormData.Db, filePath)
case "data":
dumpCommand = fmt.Sprintf("%s -u %s --password='%s' -h %s -P %s --no-create-info %s > %s", command, models.FormData.Username, models.FormData.Password, models.FormData.Host, models.FormData.Port, models.FormData.Db, filePath)
case "both":
dumpCommand = fmt.Sprintf("%s -u %s --password='%s' -h %s -P %s %s > %s", command, models.FormData.Username, models.FormData.Password, models.FormData.Host, models.FormData.Port, models.FormData.Db, filePath)
}
fmt.Print("Running...\n")
cmd := exec.Command(utils.SHELL_CMD, utils.SHELL_CMD_ARG, dumpCommand)
output, err := cmd.CombinedOutput()
if err != nil {
os.Remove(fmt.Sprintf("%s%s", models.FormData.Directory, fileName))
return fmt.Errorf("command execution failed: %v, output: %s", err, output)
}
fmt.Printf("Dump file created at: %s\n", filePath)
return nil
}