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
| func ImportData(c *gin.Context) {
var config ImportConfig
if err := c.ShouldBindJSON(&config); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "Invalid request body: " + err.Error(),
})
return
}
if err := validateImportConfig(config); err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"success": false,
"message": "Invalid input: " + err.Error(),
})
return
}
logParams := map[string]string{
"RemoteHost": config.RemoteHost,
"RemoteUsername": config.RemoteUsername,
"RemoteDatabase": config.RemoteDatabase,
"LocalDatabase": config.LocalDatabase,
"Timestamp": time.Now().Format("2006-01-02 15:04:05"),
}
logBytes, _ := json.MarshalIndent(logParams, "", " ")
fmt.Printf("Import Parameters:\n%s\n", string(logBytes))
config.RemoteHost = sanitizeInput(config.RemoteHost)
config.RemoteUsername = sanitizeInput(config.RemoteUsername)
config.RemoteDatabase = sanitizeInput(config.RemoteDatabase)
config.LocalDatabase = sanitizeInput(config.LocalDatabase)
config.RemotePassword = sanitizeInput(config.RemotePassword)
// Connect Database
if manager.db == nil {
dsn := buildDSN(localConfig)
db, err := sql.Open("mysql", dsn)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to connect to local database: " + err.Error(),
})
return
}
if err := db.Ping(); err != nil {
db.Close()
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to ping local database: " + err.Error(),
})
return
}
manager.db = db
}
if err := createdb(config.LocalDatabase); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to create local database: " + err.Error(),
})
return
}
// 创建以时间戳命名的目录
timestamp := time.Now().Format("20060102_150405")
backupDir := filepath.Join("backups", timestamp)
if err := os.MkdirAll(backupDir, 0755); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to create backup directory: " + err.Error(),
})
return
}
// 创建SQL文件
sqlFileName := fmt.Sprintf("%s_%s.sql", config.RemoteDatabase, timestamp)
sqlFilePath := filepath.Join(backupDir, sqlFileName)
sqlFile, err := os.Create(sqlFilePath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to create SQL file: " + err.Error(),
})
return
}
defer sqlFile.Close()
// 创建参数日志文件
logFileName := fmt.Sprintf("%s_%s_params.json", config.RemoteDatabase, timestamp)
logFilePath := filepath.Join(backupDir, logFileName)
if err := os.WriteFile(logFilePath, logBytes, 0644); err != nil {
fmt.Printf("Warning: Failed to save parameters log: %v\n", err)
}
dumpCmd := exec.Command("mysqldump",
"-h", config.RemoteHost,
"-u", config.RemoteUsername,
"-p"+config.RemotePassword,
config.RemoteDatabase)
tmpfile, err := os.CreateTemp("", "mysqldump-*.sql")
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to create temporary file: " + err.Error(),
})
return
}
defer os.Remove(tmpfile.Name())
defer tmpfile.Close()
writer := io.MultiWriter(sqlFile, tmpfile)
dumpCmd.Stdout = writer
dumpCmd.Stderr = os.Stderr
if err := dumpCmd.Run(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to export database: " + err.Error(),
})
return
}
tmpfile.Sync()
tmpfile.Seek(0, 0)
importCmd := exec.Command("mysql",
"-h", "127.0.0.1",
"-u", localConfig.Username,
"-p"+localConfig.Password,
config.LocalDatabase)
importCmd.Stdin = tmpfile
importCmd.Stderr = os.Stderr
if err := importCmd.Run(); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"message": "Failed to import data: " + err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"message": fmt.Sprintf("Data imported successfully. SQL file saved at: %s", sqlFilePath),
})
}
|