-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_export.go
370 lines (350 loc) · 9.86 KB
/
mysql_export.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package mysql
import (
"context"
"database/sql"
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/admpub/archiver"
loga "github.com/admpub/log"
"github.com/webx-top/com"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/nging/v5/application/library/background"
"github.com/admpub/nging/v5/application/library/notice"
"github.com/nging-plugins/dbmanager/application/library/dbmanager/driver"
"github.com/nging-plugins/dbmanager/application/library/dbmanager/driver/mysql/utils"
)
// SQLTempDir sql文件缓存目录获取函数(用于导入导出SQL)
var SQLTempDir = os.TempDir
func TempDir(op string) string {
dir := filepath.Join(SQLTempDir(), `dbmanager/cache`, op)
err := com.MkdirAll(dir, os.ModePerm)
if err != nil {
loga.Error(err)
}
return dir
}
func (m *mySQL) getCharsetList() []string {
e, _ := m.getCharsets()
cs := make([]string, 0)
for k := range e {
cs = append(cs, k)
}
sort.Strings(cs)
return cs
}
func (m *mySQL) exporting() error {
return m.bgExecManage(utils.OpExport)
}
func (m *mySQL) ImportAndOutputOpName(op string) string {
return `dbmanager.` + m.DbAuth.Driver + `.` + op
}
func (m *mySQL) bgExecManage(op string) error {
var err error
if m.IsPost() {
data := m.Data()
keys := m.FormValues(`key`)
background.Cancel(m.ImportAndOutputOpName(op), keys...)
data.SetInfo(m.T(`操作成功`))
m.ok(m.T(`操作成功`))
return m.returnTo(m.GenURL(op) + `&process=1`)
}
m.Set(`op`, op)
group := background.ListBy(m.ImportAndOutputOpName(op))
bgs := map[string]background.Background{}
if group != nil {
bgs = group.Map()
}
m.Set(`list`, bgs)
var title string
if op == utils.OpExport {
title = m.T(`导出SQL`)
} else {
title = m.T(`导入SQL`)
}
m.Set(`title`, title)
m.Set(`cacheDir`, handler.URLFor(`/download/file?path=dbmanager/cache/`+op))
return m.Render(`db/mysql/process_store`, m.checkErr(err))
}
func (m *mySQL) Export() error {
if len(m.Form(`docType`)) > 0 {
return m.ExportDoc()
}
//fmt.Printf("%#v\n", m.getCharsetList())
process := m.Queryx(`process`).Bool()
if process {
return m.exporting()
}
var err error
if m.IsPost() {
var tables []string
if len(m.dbName) == 0 {
m.fail(m.T(`请选择数据库`))
return m.returnTo(m.GenURL(`listDb`))
}
if m.Formx(`all`).Bool() {
var ok bool
tables, ok = m.Get(`tableList`).([]string)
if !ok {
tables, err = m.getTables()
if err != nil {
m.fail(err.Error())
return m.returnTo(m.GenURL(`export`))
}
}
} else {
tables = m.FormValues(`table`)
if len(tables) == 1 && len(tables[0]) > 0 {
tables = strings.Split(tables[0], `,`)
}
views := m.FormValues(`view`)
if len(views) == 1 && len(views[0]) > 0 {
views = strings.Split(views[0], `,`)
}
if len(views) > 0 {
tables = append(tables, views...)
}
}
output := m.Form(`output`)
types := m.FormValues(`type`)
cacheKey := com.Md5(com.Dump([]interface{}{tables, output, types}, false))
var (
structWriter, dataWriter interface{}
sqlFiles []string
dbSaveDir string
async bool
bgExec = background.New(context.TODO(), echo.H{
`database`: m.dbName,
`tables`: tables,
`output`: output,
`types`: types,
})
fileInfos = &utils.FileInfos{}
)
exports, err := background.Register(m.Context, m.ImportAndOutputOpName(utils.OpExport), cacheKey, bgExec)
if err != nil {
return err
}
nowTime := time.Now().Format("20060102150405.000")
saveDir := TempDir(utils.OpExport)
switch output {
case `down`:
m.Response().Header().Set(echo.HeaderContentType, echo.MIMEOctetStream)
m.Response().Header().Set(echo.HeaderContentDisposition, fmt.Sprintf("attachment; filename=%q", m.dbName+"-sql-"+nowTime+".sql"))
fallthrough
case `open`:
m.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
if com.InSlice(`struct`, types) {
structWriter = m.Response()
}
if com.InSlice(`data`, types) {
dataWriter = m.Response()
}
default:
async = true
dbSaveDir = filepath.Join(saveDir, m.dbName)
com.MkdirAll(dbSaveDir, os.ModePerm)
if com.InSlice(`struct`, types) {
structFile := filepath.Join(dbSaveDir, `struct-`+nowTime+`.sql`)
sqlFiles = append(sqlFiles, structFile)
structWriter = structFile
fi := &utils.FileInfo{
Start: time.Now(),
Path: structFile,
}
*fileInfos = append(*fileInfos, fi)
}
if com.InSlice(`data`, types) {
dataFile := filepath.Join(dbSaveDir, `data-`+nowTime+`.sql`)
sqlFiles = append(sqlFiles, dataFile)
dataWriter = dataFile
fi := &utils.FileInfo{
Start: time.Now(),
Path: dataFile,
}
*fileInfos = append(*fileInfos, fi)
}
}
cfg := *m.DbAuth
cfg.Db = m.dbName
coll, err := m.getCollation(m.dbName, nil)
if err != nil {
return err
}
cfg.Charset = strings.SplitN(coll, `_`, 2)[0]
user := handler.User(m.Context)
var username string
if user != nil {
username = user.Username
}
noticer := notice.New(m.Context, `databaseExport`, username, bgExec.Context())
gtidMode, _ := m.showVariables(`gtid_mode`)
var hasGTID bool
if len(gtidMode) > 0 && len(gtidMode[0]) > 0 {
if k, y := gtidMode[0][`k`]; y && len(k) > 0 {
hasGTID = true
}
}
worker := func(c context.Context, cfg driver.DbAuth) error {
defer func() {
exports.Cancel(cacheKey)
if r := recover(); r != nil {
err = fmt.Errorf(`RECOVER: %v`, r)
}
}()
if utils.SupportedExport() { // 采用 mysqldump 命令导出
err = utils.Export(c, noticer, &cfg, tables, structWriter, dataWriter, m.getVersion(), hasGTID, true)
} else {
if structWriter != nil {
err = m.exportDBStruct(c, noticer, &cfg, tables, structWriter, m.getVersion(), true)
}
if err == nil && dataWriter != nil {
err = m.exportDBData(c, noticer, &cfg, tables, dataWriter, m.getVersion())
}
}
if err != nil {
loga.Error(err)
return err
}
if len(sqlFiles) > 0 {
now := time.Now()
for _, fi := range *fileInfos {
fi.End = now
fi.Size, err = com.FileSize(fi.Path)
if err != nil {
fi.Error = err.Error()
}
fi.Elapsed = fi.End.Sub(fi.Start)
}
zipFile := filepath.Join(dbSaveDir, "sql-"+nowTime+".zip")
fi := &utils.FileInfo{
Start: now,
Path: zipFile,
Compressed: true,
}
err = archiver.Archive(sqlFiles, zipFile)
if err != nil {
loga.Error(err)
return err
}
for _, sqlFile := range sqlFiles {
os.Remove(sqlFile)
}
fi.Size, err = com.FileSize(zipFile)
if err != nil {
fi.Error = err.Error()
}
fi.End = time.Now()
fi.Elapsed = fi.End.Sub(fi.Start)
fileInfos.Add(fi)
os.WriteFile(zipFile+`.txt`, com.Str2bytes(com.Dump(fileInfos, false)), os.ModePerm)
}
return nil
}
if !async {
done := make(chan struct{})
ctx := m.StdContext()
go func() {
defer exports.Cancel(cacheKey)
for {
select {
case <-ctx.Done():
return
case <-done:
return
}
}
}()
err = worker(bgExec.Context(), cfg)
if err != nil {
noticer(m.T(`导出失败`)+`: `+err.Error(), 0)
}
done <- struct{}{}
return err
}
data := m.Data()
data.SetInfo(m.T(`任务已经在后台成功启动`))
data.SetURL(handler.URLFor(`/download/file?path=dbmanager/cache/` + utils.OpExport + `/` + m.dbName))
go worker(bgExec.Context(), cfg)
return m.JSON(data)
}
return m.Redirect(m.GenURL(`listTable`, m.dbName))
}
func (m *mySQL) ExportDoc() error {
if m.IsPost() {
var tables []string
if len(m.dbName) == 0 {
m.fail(m.T(`请选择数据库`))
return m.returnTo(m.GenURL(`listDb`))
}
tables = m.FormValues(`table`)
if len(tables) == 1 && len(tables[0]) > 0 {
tables = strings.Split(tables[0], `,`)
}
docType := m.Form(`docType`)
newExportorDoc, ok := docExportors[docType]
if !ok {
return m.NewError(code.InvalidParameter, `不支持导出文档类型: %s`, docType)
}
exportor := newExportorDoc(m.dbName)
err := exportor.Open(m.Context)
if err != nil {
return err
}
for _, table := range tables {
origFields, sortFields, err := m.tableFields(table)
if err != nil {
return err
}
stt, _, err := m.getTableStatus(m.dbName, table, false)
if err != nil {
return err
}
var tableStatus *TableStatus
if ts, ok := stt[table]; ok {
tableStatus = ts
} else {
tableStatus = &TableStatus{Name: sql.NullString{Valid: true, String: table}}
}
postFields := make([]*Field, len(sortFields))
for k, v := range sortFields {
postFields[k] = origFields[v]
}
err = exportor.Write(m.Context, tableStatus, postFields)
if err != nil {
return err
}
}
return exportor.Close(m.Context)
}
return m.Redirect(m.GenURL(`listTable`, m.dbName))
}
type DocExportor interface {
Open(echo.Context) error
Write(echo.Context, *TableStatus, []*Field) error
Close(echo.Context) error
}
var docExportors = map[string]func(dbName string) DocExportor{
`html`: newHTMLDocExportor,
`markdown`: newMarkdownDocExportor,
`csv`: newCSVDocExportor,
}