-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathconfig.go
730 lines (643 loc) · 24 KB
/
config.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
Copyright 2021 RadonDB.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sidecar
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strconv"
"strings"
"github.com/blang/semver"
"github.com/go-ini/ini"
"github.com/radondb/radondb-mysql-kubernetes/utils"
)
// Config of the sidecar.
type Config struct {
// The hostname of the pod.
HostName string
// The namespace where the pod is in.
NameSpace string
// The name of the headless service.
ServiceName string
// The name of the statefulset.
StatefulSetName string
// The password of the root user.
RootPassword string
// Username of new user to create.
User string
// Password for the new user.
Password string
// Name for new database to create.
Database string
// The name of replication user.
ReplicationUser string
// The password of the replication user.
ReplicationPassword string
// The name of metrics user.
MetricsUser string
// The password of metrics user.
MetricsPassword string
// The name of operator user.
OperatorUser string
// The password of operator user.
OperatorPassword string
// The password of the mysql root user, for operator use only.
InternalRootPassword string
// InitTokuDB represents if install tokudb engine.
InitTokuDB bool
// MySQLVersion represents the MySQL version that will be run.
MySQLVersion semver.Version
// The parameter in xenon means admit defeat count for hearbeat.
AdmitDefeatHearbeatCount int32
// The parameter in xenon means election timeout(ms).
ElectionTimeout int32
// Whether the MySQL data exists.
existMySQLData bool
// for mysql backup
// backup user and password for http endpoint
ClusterName string
// Job name if is backup Job
JobName string
// Backup user name to http Server
BackupUser string
// Backup Password to htpp Server
BackupPassword string
// XbstreamExtraArgs is a list of extra command line arguments to pass to xbstream.
XbstreamExtraArgs []string
// XtrabackupExtraArgs is a list of extra command line arguments to pass to xtrabackup.
XtrabackupExtraArgs []string
// XtrabackupPrepareExtraArgs is a list of extra command line arguments to pass to xtrabackup
// during --prepare.
XtrabackupPrepareExtraArgs []string
// XtrabackupTargetDir is a backup destination directory for xtrabackup.
XtrabackupTargetDir string
// S3 endpoint domain name
XCloudS3EndPoint string
// S3 access key
XCloudS3AccessKey string
// S3 secrete key
XCloudS3SecretKey string
// S3 Bucket names
XCloudS3Bucket string
// directory in S3 bucket for cluster restore from
XRestoreFrom string
// Clone flag
CloneFlag bool
// GtidPurged is the gtid set of the slave cluster to purged.
GtidPurged string
// NFS server which Restore from
XRestoreFromNFS string
}
// NewInitConfig returns a pointer to Config.
func NewInitConfig() *Config {
// check mysql version is supported or not and then get parse mysql semver version
var mysqlSemVer semver.Version
if ver := getEnvValue("MYSQL_VERSION"); ver == utils.InvalidMySQLVersion {
panic("invalid mysql version, currently we only support 5.7 or 8.0")
} else {
var err error
// Do not use := here, it will alloc a new semver.Version every time.
mysqlSemVer, err = semver.Parse(ver)
if err != nil {
log.Info("semver get from MYSQL_VERSION is invalid", "semver: ", mysqlSemVer)
panic(err)
}
}
initTokuDB := false
if len(getEnvValue("INIT_TOKUDB")) > 0 {
initTokuDB = true
}
admitDefeatHearbeatCount, err := strconv.ParseInt(getEnvValue("ADMIT_DEFEAT_HEARBEAT_COUNT"), 10, 32)
if err != nil {
admitDefeatHearbeatCount = 5
}
electionTimeout, err := strconv.ParseInt(getEnvValue("ELECTION_TIMEOUT"), 10, 32)
if err != nil {
electionTimeout = 10000
}
existMySQLData, _ := checkIfPathExists(fmt.Sprintf("%s/mysql", dataPath))
return &Config{
HostName: getEnvValue("POD_HOSTNAME"),
NameSpace: getEnvValue("NAMESPACE"),
ServiceName: getEnvValue("SERVICE_NAME"),
StatefulSetName: getEnvValue("STATEFULSET_NAME"),
RootPassword: getEnvValue("MYSQL_ROOT_PASSWORD"),
InternalRootPassword: getEnvValue("INTERNAL_ROOT_PASSWORD"),
Database: getEnvValue("MYSQL_DATABASE"),
User: getEnvValue("MYSQL_USER"),
Password: getEnvValue("MYSQL_PASSWORD"),
ReplicationUser: getEnvValue("MYSQL_REPL_USER"),
ReplicationPassword: getEnvValue("MYSQL_REPL_PASSWORD"),
MetricsUser: getEnvValue("METRICS_USER"),
MetricsPassword: getEnvValue("METRICS_PASSWORD"),
OperatorUser: getEnvValue("OPERATOR_USER"),
OperatorPassword: getEnvValue("OPERATOR_PASSWORD"),
InitTokuDB: initTokuDB,
MySQLVersion: mysqlSemVer,
AdmitDefeatHearbeatCount: int32(admitDefeatHearbeatCount),
ElectionTimeout: int32(electionTimeout),
existMySQLData: existMySQLData,
XRestoreFrom: getEnvValue("RESTORE_FROM"),
XRestoreFromNFS: getEnvValue("RESTORE_FROM_NFS"),
XCloudS3EndPoint: getEnvValue("S3_ENDPOINT"),
XCloudS3AccessKey: getEnvValue("S3_ACCESSKEY"),
XCloudS3SecretKey: getEnvValue("S3_SECRETKEY"),
XCloudS3Bucket: getEnvValue("S3_BUCKET"),
ClusterName: getEnvValue("CLUSTER_NAME"),
CloneFlag: false,
GtidPurged: "",
}
}
// NewBackupConfig returns the configuration file needed for backup container.
func NewBackupConfig() *Config {
return &Config{
NameSpace: getEnvValue("NAMESPACE"),
ServiceName: getEnvValue("SERVICE_NAME"),
ClusterName: getEnvValue("CLUSTER_NAME"),
RootPassword: getEnvValue("MYSQL_ROOT_PASSWORD"),
BackupUser: getEnvValue("BACKUP_USER"),
BackupPassword: getEnvValue("BACKUP_PASSWORD"),
XCloudS3EndPoint: getEnvValue("S3_ENDPOINT"),
XCloudS3AccessKey: getEnvValue("S3_ACCESSKEY"),
XCloudS3SecretKey: getEnvValue("S3_SECRETKEY"),
XCloudS3Bucket: getEnvValue("S3_BUCKET"),
}
}
// NewReqBackupConfig returns the configuration file needed for backup job.
func NewReqBackupConfig() *Config {
return &Config{
NameSpace: getEnvValue("NAMESPACE"),
ServiceName: getEnvValue("SERVICE_NAME"),
BackupUser: getEnvValue("BACKUP_USER"),
BackupPassword: getEnvValue("BACKUP_PASSWORD"),
JobName: getEnvValue("JOB_NAME"),
}
}
// GetContainerType returns the CONTAINER_TYPE of the currently running container.
// CONTAINER_TYPE used to mark the container type.
func GetContainerType() string {
return getEnvValue("CONTAINER_TYPE")
}
// build Xtrabackup arguments
func (cfg *Config) XtrabackupArgs() []string {
// xtrabackup --backup <args> --target-dir=<backup-dir> <extra-args>
tmpdir := "/root/backup/"
if len(cfg.XtrabackupTargetDir) != 0 {
tmpdir = cfg.XtrabackupTargetDir
}
xtrabackupArgs := []string{
"--backup",
"--stream=xbstream",
"--host=127.0.0.1",
fmt.Sprintf("--user=%s", utils.RootUser),
fmt.Sprintf("--password=%s", cfg.RootPassword),
fmt.Sprintf("--target-dir=%s", tmpdir),
}
return append(xtrabackupArgs, cfg.XtrabackupExtraArgs...)
}
// Build xbcloud arguments
func (cfg *Config) XCloudArgs(backupName string) []string {
xcloudArgs := []string{
"put",
"--storage=S3",
fmt.Sprintf("--s3-endpoint=%s", cfg.XCloudS3EndPoint),
fmt.Sprintf("--s3-access-key=%s", cfg.XCloudS3AccessKey),
fmt.Sprintf("--s3-secret-key=%s", cfg.XCloudS3SecretKey),
fmt.Sprintf("--s3-bucket=%s", cfg.XCloudS3Bucket),
"--parallel=10",
// utils.BuildBackupName(cfg.ClusterName),
backupName,
"--insecure",
}
return xcloudArgs
}
func (cfg *Config) XBackupName() (string, string) {
return utils.BuildBackupName(cfg.ClusterName)
}
// buildExtraConfig build a ini file for mysql.
func (cfg *Config) buildExtraConfig(filePath string) (*ini.File, error) {
conf := ini.Empty()
sec := conf.Section("mysqld")
ordinal, err := utils.GetOrdinal(cfg.HostName)
if err != nil {
return nil, err
}
if _, err := sec.NewKey("server-id", strconv.Itoa(mysqlServerIDOffset+ordinal)); err != nil {
return nil, err
}
if _, err := sec.NewKey("init-file", filePath); err != nil {
return nil, err
}
return conf, nil
}
// buildXenonConf build a config file for xenon.
func (cfg *Config) buildXenonConf() []byte {
pingTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
heartbeatTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
requestTimeout := cfg.ElectionTimeout / cfg.AdmitDefeatHearbeatCount
version := "mysql80"
if cfg.MySQLVersion.Major == 5 {
version = "mysql57"
}
var srcSysVars, replicaSysVars string
if cfg.InitTokuDB {
srcSysVars = "tokudb_fsync_log_period=default;sync_binlog=default;innodb_flush_log_at_trx_commit=default"
replicaSysVars = "tokudb_fsync_log_period=1000;sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
} else {
srcSysVars = "sync_binlog=default;innodb_flush_log_at_trx_commit=default"
replicaSysVars = "sync_binlog=1000;innodb_flush_log_at_trx_commit=1"
}
hostName := fmt.Sprintf("%s.%s.%s", cfg.HostName, cfg.ServiceName, cfg.NameSpace)
// Because go-sql-driver will translate localhost to 127.0.0.1 or ::1, but never set the hostname
// so the host is set to "127.0.0.1" in config file.
str := fmt.Sprintf(`{
"log": {
"level": "INFO"
},
"server": {
"endpoint": "%s:%d",
"peer-address": "%s:%d",
"enable-apis": true
},
"replication": {
"passwd": "%s",
"user": "%s",
"gtid-purged": "%s"
},
"rpc": {
"request-timeout": %d
},
"mysql": {
"admit-defeat-ping-count": 3,
"admin": "root",
"ping-timeout": %d,
"passwd": "%s",
"host": "127.0.0.1",
"version": "%s",
"master-sysvars": "%s",
"slave-sysvars": "%s",
"port": 3306,
"monitor-disabled": true
},
"raft": {
"election-timeout": %d,
"admit-defeat-hearbeat-count": %d,
"heartbeat-timeout": %d,
"meta-datadir": "%s",
"semi-sync-degrade": true,
"purge-binlog-disabled": true,
"super-idle": false,
"leader-start-command": "/scripts/leader-stop.sh",
"leader-stop-command": "/scripts/leader-stop.sh"
}
}
`, hostName, utils.XenonPort, hostName, utils.XenonPeerPort, cfg.ReplicationPassword, cfg.ReplicationUser,
cfg.GtidPurged, requestTimeout,
pingTimeout, cfg.RootPassword, version, srcSysVars, replicaSysVars, cfg.ElectionTimeout,
cfg.AdmitDefeatHearbeatCount, heartbeatTimeout, xenonConfigPath)
return utils.StringToBytes(str)
}
// buildInitSql used to build init.sql. The file run after the mysql init.
func (cfg *Config) buildInitSql(hasInit bool) []byte {
sql := fmt.Sprintf(`SET @@SESSION.SQL_LOG_BIN=0;
CREATE DATABASE IF NOT EXISTS %s;
DROP user IF EXISTS 'root'@'127.0.0.1';
CREATE USER 'root'@'127.0.0.1' IDENTIFIED BY '%s';
GRANT ALL ON *.* TO 'root'@'127.0.0.1' with grant option;
DROP user IF EXISTS 'root'@'%%';
CREATE USER 'root'@'%%' IDENTIFIED BY '%s';
GRANT ALL ON *.* TO 'root'@'%%' with grant option;
DROP user IF EXISTS '%s'@'%%';
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO '%s'@'%%';
DROP user IF EXISTS '%s'@'%%';
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
GRANT SELECT, PROCESS, REPLICATION CLIENT ON *.* TO '%s'@'%%';
DROP user IF EXISTS '%s'@'%%';
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
GRANT SUPER, PROCESS, RELOAD, CREATE, SELECT ON *.* TO '%s'@'%%';
DROP user IF EXISTS '%s'@'%%';
CREATE USER '%s'@'%%' IDENTIFIED BY '%s';
GRANT ALL ON %s.* TO '%s'@'%%' ;
FLUSH PRIVILEGES;
`,
cfg.Database, //database
cfg.RootPassword,
cfg.InternalRootPassword,
cfg.ReplicationUser, //drop user
cfg.ReplicationUser, cfg.ReplicationPassword, //create user
cfg.ReplicationUser, //grant REPLICATION
cfg.MetricsUser, //drop user MetricsUser
cfg.MetricsUser, cfg.MetricsPassword, //create user
cfg.MetricsUser, //grant
cfg.OperatorUser, //drop user
cfg.OperatorUser, cfg.OperatorPassword, //create
cfg.OperatorUser, //grant
cfg.User, //drop user
cfg.User, cfg.Password, //create user
cfg.Database, cfg.User) //grant
if hasInit {
sql += "\nRESET SLAVE ALL;\n"
}
return utils.StringToBytes(sql)
}
// buildClientConfig used to build client.conf.
func (cfg *Config) buildClientConfig() (*ini.File, error) {
conf := ini.Empty()
sec := conf.Section("client")
if _, err := sec.NewKey("host", "127.0.0.1"); err != nil {
return nil, err
}
if _, err := sec.NewKey("port", fmt.Sprintf("%d", utils.MysqlPort)); err != nil {
return nil, err
}
if _, err := sec.NewKey("user", cfg.OperatorUser); err != nil {
return nil, err
}
if _, err := sec.NewKey("password", cfg.OperatorPassword); err != nil {
return nil, err
}
return conf, nil
}
// // buildLeaderStart build the leader-start.sh.
// func (cfg *Config) buildLeaderStart() []byte {
// str := fmt.Sprintf(`#!/usr/bin/env bash
// curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json-patch+json" \
// --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/%s/pods/$HOSTNAME \
// -d '[{"op": "replace", "path": "/metadata/labels/role", "value": "leader"}]'
// `, cfg.NameSpace)
// return utils.StringToBytes(str)
// }
// buildLeaderStop build the leader-stop.sh.
func (cfg *Config) buildLeaderStop() []byte {
str := fmt.Sprintf(`#!/usr/bin/env bash
curl -X DELETE -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" -H "Content-Type: application/json" --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_PORT_443_TCP_PORT/api/v1/namespaces/$NAMESPACE/services/%s-leader
`, cfg.ClusterName)
return utils.StringToBytes(str)
}
/* The function is equivalent to the following shell script template:
#!/bin/sh
if [ ! -d {{.DataDir}} ] ; then
echo "is not exist the var lib mysql"
mkdir {{.DataDir}}
chown -R mysql.mysql {{.DataDir}}
fi
mkdir /root/backup
xbcloud get --storage=S3 \
--s3-endpoint="{{.XCloudS3EndPoint}}" \
--s3-access-key="{{.XCloudS3AccessKey}}" \
--s3-secret-key="{{.XCloudS3SecretKey}}" \
--s3-bucket="{{.XCloudS3Bucket}}" \
--parallel=10 {{.XRestoreFrom}} \
--insecure |xbstream -xv -C /root/backup
# prepare redolog
xtrabackup --defaults-file={{.MyCnfMountPath}} --use-memory=3072M --prepare --apply-log-only --target-dir=/root/backup
# prepare data
xtrabackup --defaults-file={{.MyCnfMountPath}} --use-memory=3072M --prepare --target-dir=/root/backup
chown -R mysql.mysql /root/backup
xtrabackup --defaults-file={{.MyCnfMountPath}} --datadir={{.DataDir}} --copy-back --target-dir=/root/backup
chown -R mysql.mysql {{.DataDir}}
rm -rf /root/backup
*/
func (cfg *Config) executeS3Restore(path string) error {
if len(cfg.XRestoreFrom) == 0 {
return fmt.Errorf("do not have restore from")
}
if len(cfg.XCloudS3EndPoint) == 0 ||
len(cfg.XCloudS3AccessKey) == 0 ||
len(cfg.XCloudS3SecretKey) == 0 ||
len(cfg.XCloudS3Bucket) == 0 {
return fmt.Errorf("do not have S3 information")
}
// Check has directory, and create it.
if _, err := os.Stat(utils.DataVolumeMountPath); os.IsNotExist(err) {
if err := os.MkdirAll(utils.DataVolumeMountPath, 0755); err != nil {
return fmt.Errorf("failed to create data directory : %s", err)
}
}
// mkdir /root/backup.
if err := os.MkdirAll("/root/backup", 0755); err != nil {
return fmt.Errorf("failed to create backup directory : %s", err)
}
// Execute xbcloud get.
args := []string{
"get",
"--storage=S3",
"--s3-endpoint=" + cfg.XCloudS3EndPoint,
"--s3-access-key=" + cfg.XCloudS3AccessKey,
"--s3-secret-key=" + cfg.XCloudS3SecretKey,
"--s3-bucket=" + cfg.XCloudS3Bucket,
"--parallel=10",
cfg.XRestoreFrom,
"--insecure",
}
xcloud := exec.Command(xcloudCommand, args...) //nolint
xbstream := exec.Command("xbstream", "-xv", "-C", "/root/backup") //nolint
var err error
if xbstream.Stdin, err = xcloud.StdoutPipe(); err != nil {
return fmt.Errorf("failed to xbstream and xcloud piped")
}
xbstream.Stderr = os.Stderr
xcloud.Stderr = os.Stderr
if err := xcloud.Start(); err != nil {
return fmt.Errorf("failed to xcloud start : %s", err)
}
if err := xbstream.Start(); err != nil {
return fmt.Errorf("failed to xbstream start : %s", err)
}
// Make error channels.
errCh := make(chan error, 2)
go func() {
errCh <- xcloud.Wait()
}()
go func() {
errCh <- xbstream.Wait()
}()
// Wait for error.
for i := 0; i < 2; i++ {
if err = <-errCh; err != nil {
return err
}
}
// Xtrabackup prepare and apply-log-only.
cmd := exec.Command(xtrabackupCommand, "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--prepare", "--apply-log-only", "--target-dir=/root/backup")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup prepare and apply-log-only : %s", err)
}
// Xtrabackup prepare.
cmd = exec.Command(xtrabackupCommand, "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--prepare", "--target-dir=/root/backup")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup prepare : %s", err)
}
// Xtrabackup copy-back to /var/lib/mysql.
cmd = exec.Command(xtrabackupCommand, "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--datadir="+utils.DataVolumeMountPath, "--copy-back", "--copy-back", "--target-dir=/root/backup")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup copy-back : %s", err)
}
// Execute chown -R mysql.mysql /var/lib/mysql.
if err := exec.Command("chown", "-R", "mysql.mysql", utils.DataVolumeMountPath).Run(); err != nil {
return fmt.Errorf("failed to chown mysql.mysql %s : %s", utils.DataVolumeMountPath, err)
}
// Remove /root/backup.
if err := os.RemoveAll("/root/backup"); err != nil {
return fmt.Errorf("failed to remove backup directory : %s", err)
}
return nil
}
// Do Restore after clone.
func (cfg *Config) executeCloneRestore() error {
// Check directory exist, create if not exist.
if _, err := os.Stat(utils.DataVolumeMountPath); os.IsNotExist(err) {
os.Mkdir(utils.DataVolumeMountPath, 0755)
}
// Empty the directory.
dir, err := ioutil.ReadDir(utils.DataVolumeMountPath)
if err != nil {
return fmt.Errorf("failed to read datadir %s", err)
}
for _, d := range dir {
os.RemoveAll(path.Join([]string{utils.DataVolumeMountPath, d.Name()}...))
}
// Xtrabackup prepare and apply-log-only.
cmd := exec.Command(xtrabackupCommand, "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--use-memory=3072M", "--prepare", "--apply-log-only", "--target-dir=/backup/"+cfg.XRestoreFrom)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup prepare apply-log-only : %s", err)
}
// Xtrabackup Prepare.
cmd = exec.Command(xtrabackupCommand, "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--use-memory=3072M", "--prepare", "--target-dir=/backup/"+cfg.XRestoreFrom)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup prepare : %s", err)
}
// Get the backup binlong info.
gtid, err := GetXtrabackupGTIDPurged("/backup/" + cfg.XRestoreFrom)
if err == nil {
cfg.GtidPurged = gtid
}
log.Info("get master gtid purged :", "gtid purged", cfg.GtidPurged)
// Xtrabackup copy-back.
cmd = exec.Command(xtrabackupCommand, "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--use-memory=3072M", "--datadir="+utils.DataVolumeMountPath, "--copy-back", "--target-dir=/backup/"+cfg.XRestoreFrom)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup copy-back : %s", err)
}
// Remove Relaybin.
// Because the relaybin is not used in the restore process,
// we can remove it to prevent it to be used by salve in the future.
cmd = exec.Command("rm", "-rf", utils.DataVolumeMountPath+"mysql-relay*")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to remove relay-bin : %s", err)
}
// Run chown -R mysql.mysql /var/lib/mysql
cmd = exec.Command("chown", "-R", "mysql.mysql", utils.DataVolumeMountPath)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to chown -R mysql.mysql : %s", err)
}
log.Info("execute clone restore success")
return nil
}
// Parse the xtrabackup_binlog_info, the format is filename \t position \t gitid1 \ngitid2 ...
// or filename \t position\n
// Get the gtid when it is existed, or return empty string.
// It used to purged the gtid when start the mysqld slave.
func GetXtrabackupGTIDPurged(backuppath string) (string, error) {
byteStream, err := ioutil.ReadFile(fmt.Sprintf("%s/xtrabackup_binlog_info", backuppath))
if err != nil {
return "", err
}
line := strings.TrimSuffix(string(byteStream), "\n")
ss := strings.Split(line, "\t")
if len(ss) != 3 {
return "", fmt.Errorf("info.file.content.invalid[%v]", string(byteStream))
}
// Replace multi gtidset \n
return strings.Replace(ss[2], "\n", "", -1), nil
}
/*
`#!/bin/sh
if [ ! -d {{.DataDir}} ]; then
echo "is not exist the var lib mysql"
mkdir {{.DataDir}}
chown -R mysql.mysql {{.DataDir}}
fi
rm -rf {{.DataDir}}/*
xtrabackup --defaults-file={{.MyCnfMountPath}} --use-memory=3072M --prepare --apply-log-only --target-dir=/backup/{{.XRestoreFrom}}
xtrabackup --defaults-file={{.MyCnfMountPath}} --use-memory=3072M --prepare --target-dir=/backup/{{.XRestoreFrom}}
chown -R mysql.mysql /backup/{{.XRestoreFromNFS}}
xtrabackup --defaults-file={{.MyCnfMountPath}} --datadir={{.DataDir}} --copy-back --target-dir=/backup/{{.XRestoreFrom}}
exit_code=$?
chown -R mysql.mysql {{.DataDir}}
exit $exit_code
*/
func (cfg *Config) ExecuteNFSRestore() error {
if len(cfg.XRestoreFromNFS) == 0 {
return fmt.Errorf("parameter XRestoreFromNFS empty, do next step")
}
if len(cfg.XRestoreFrom) == 0 {
return fmt.Errorf("xrestore from is empty, do next step")
}
// Restore from NFS
// Check /var/lib/mysql exists or not.
if _, err := os.Stat(utils.DataVolumeMountPath); os.IsNotExist(err) {
err = os.MkdirAll(utils.DataVolumeMountPath, 0755)
if err != nil {
return fmt.Errorf("create /var/lib/mysql fail : %s", err)
}
// Change the owner of /var/lib/mysql
cmd := exec.Command("chown", "mysql.mysql", utils.DataVolumeMountPath)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to chown -R mysql.mysql : %s", err)
}
}
// Remove the data directory
cmd := exec.Command("rm", "-rf", utils.DataVolumeMountPath+"/*")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to rm -rf %s : %s", utils.DataVolumeMountPath, err)
}
// Prepare the append-only file
cmd = exec.Command("xtrabackup", "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--use-memory=3072M", "--prepare", "--apply-log-only", "--target-dir=/backup/"+cfg.XRestoreFrom)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup prepare append-only: %s", err)
}
// Prepare the data directory
cmd = exec.Command("xtrabackup", "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--use-memory=3072M", "--prepare", "--target-dir=/backup/"+cfg.XRestoreFrom)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup prepare: %s", err)
}
// Copy the data directory.
cmd = exec.Command("xtrabackup", "--defaults-file="+utils.MysqlConfVolumeMountPath+"/my.cnf", "--datadir="+utils.DataVolumeMountPath, "--copy-back", "--target-dir=/backup/"+cfg.XRestoreFrom)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to xtrabackup copy-back: %s", err)
}
// Change owner of data directory
log.Info(fmt.Sprintf("change owner of data directory %s", utils.DataVolumeMountPath))
cmd = exec.Command("chown", "-R", "mysql.mysql", utils.DataVolumeMountPath)
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to chown -R mysql.mysql : %s", err)
}
return nil
}