-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
export.go
210 lines (199 loc) · 5.55 KB
/
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
/*
Nging is a toolbox for webmasters
Copyright (C) 2019-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 utils
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/webx-top/com"
"github.com/webx-top/db"
"github.com/admpub/errors"
"github.com/admpub/nging/v3/application/library/common"
writerPkg "github.com/admpub/nging/v3/application/library/cron/writer"
"github.com/admpub/nging/v3/application/library/dbmanager/driver"
"github.com/admpub/nging/v3/application/library/notice"
)
/*
mysqldump 参数说明:
-d 结构(--no-data:不导出任何数据,只导出数据库表结构)
-t 数据(--no-create-info:只导出数据,而不添加CREATE TABLE 语句)
-n (--no-create-db:只导出数据,而不添加CREATE DATABASE 语句)
-R (--routines:导出存储过程以及自定义函数)
-E (--events:导出事件)
--triggers (默认导出触发器,使用--skip-triggers屏蔽导出)
-B (--databases:导出数据库列表,单个库时可省略)
--tables 表列表(单个表时可省略)
*/
var (
cleanRegExp = regexp.MustCompile(` AUTO_INCREMENT=[0-9]*\s*`)
)
// Export 导出SQL文件
func Export(ctx context.Context, noticer notice.Noticer,
cfg *driver.DbAuth, tables []string, structWriter, dataWriter interface{},
mysqlVersion string, hasGTID bool, resetAutoIncrements ...bool) error {
if len(tables) == 0 {
return errors.New(`No table selected for export`)
}
if noticer == nil {
noticer = notice.DefaultNoticer
}
exportorPath, err := common.LookPath(Exportor, MySQLBinPaths...)
if err != nil {
return err
}
if len(exportorPath) == 0 {
exportorPath = Exportor
}
noticer(`开始备份: `+strings.Join(tables, ","), 1)
var (
port, host string
resetAutoIncrement bool
)
if len(resetAutoIncrements) > 0 {
resetAutoIncrement = resetAutoIncrements[0]
}
if p := strings.LastIndex(cfg.Host, `:`); p > 0 {
host = cfg.Host[0:p]
port = cfg.Host[p+1:]
} else {
host = cfg.Host
}
if len(port) == 0 {
port = `3306`
}
if !com.InSlice(cfg.Charset, Charsets) {
return errors.New(`字符集charset值无效`)
}
args := []string{
"--default-character-set=" + cfg.Charset,
"--single-transaction",
}
if com.VersionComparex(mysqlVersion, `8.0.0`, `>=`) {
args = append(args, "--column-statistics=0") // 低版本不支持
}
if hasGTID {
args = append(args, "--set-gtid-purged=OFF")
}
args = append(args, []string{
"--no-autocommit",
//"--ignore-table="+cfg.Db+".details",
//"--ignore-table="+cfg.Db+".large_table2",
"--opt",
"-d", //加上此参数代表只导出表结构,不导出数据
"-h" + host,
"-P" + port,
"-u" + cfg.Username,
"-p" + cfg.Password,
cfg.Db,
//"--result-file=/root/backup.sql",
}...)
clean := func(w io.Writer, r io.ReadCloser) {
r.Close()
if c, y := w.(io.Closer); y {
c.Close()
}
}
var typeOptIndex int
for index, value := range args {
if value == `-d` {
typeOptIndex = index
break
}
}
//args = append(args, `--tables`)
args = append(args, tables...)
rec := writerPkg.New(1000)
for index, writer := range []interface{}{structWriter, dataWriter} {
if writer == nil {
continue
}
if index > 0 {
args[typeOptIndex] = `-t` //导出数据
}
//log.Println(exportorPath, strings.Join(args, ` `))
cmd := exec.CommandContext(ctx, exportorPath, args...)
cmd.Stderr = rec
var (
w io.Writer
err error
onFinish func() error
)
switch v := writer.(type) {
case io.Writer:
w = v
case string:
dir := filepath.Dir(v)
err = com.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf(`failed to backup: %v`, err)
}
w, err = os.Create(v)
if err != nil {
return fmt.Errorf(`failed to backup: %v`, err)
}
onFinish = func() error {
if index > 0 {
return nil
}
if resetAutoIncrement {
return ResetAutoIncrement(v)
}
return nil
}
default:
return errors.Wrapf(db.ErrUnsupported, `SQL Writer Error: %T`, v)
}
stdout, err := cmd.StdoutPipe()
if err != nil {
clean(w, stdout)
return fmt.Errorf(`failed to backup (cmd.StdoutPipe): %v`, err)
}
if err := cmd.Start(); err != nil {
clean(w, stdout)
return fmt.Errorf(`failed to backup (cmd.Start): %v`, err)
}
if _, err := io.Copy(w, stdout); err != nil {
clean(w, stdout)
return fmt.Errorf(`failed to backup (io.Copy): %v`, err)
}
if err := cmd.Wait(); err != nil {
clean(w, stdout)
return errors.New(err.Error() + ` (cmd.Wait): ` + rec.String())
}
clean(w, stdout)
if onFinish != nil {
if err = onFinish(); err != nil {
return err
}
}
}
noticer(`结束备份`, 1)
return nil
}
// ResetAutoIncrement 重置AUTO_INCREMENT值为0
func ResetAutoIncrement(sqlStructFile string) error {
b, err := ioutil.ReadFile(sqlStructFile)
if err != nil {
return err
}
b = cleanRegExp.ReplaceAll(b, []byte(` `))
return ioutil.WriteFile(sqlStructFile, b, os.ModePerm)
}