Skip to content

Commit

Permalink
sidecar: change restore.sh to go codes radondb#292
Browse files Browse the repository at this point in the history
  • Loading branch information
acekingke committed Nov 11, 2021
1 parent b271693 commit aaa999b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 60 deletions.
119 changes: 71 additions & 48 deletions sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package sidecar
import (
"fmt"
"os"
"os/exec"
"strconv"
"text/template"

"github.com/blang/semver"
"github.com/go-ini/ini"
Expand Down Expand Up @@ -541,8 +541,7 @@ curl -X PATCH -H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/ser
return utils.StringToBytes(str)
}

// build S3 restore shell script
func (cfg *Config) buildS3Restore(path string) error {
func (cfg *Config) executeS3Restore(path string) error {
if len(cfg.XRestoreFrom) == 0 {
return fmt.Errorf("do not have restore from")
}
Expand All @@ -552,53 +551,77 @@ func (cfg *Config) buildS3Restore(path string) error {
len(cfg.XCloudS3Bucket) == 0 {
return fmt.Errorf("do not have S3 information")
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("create restore.sh fail : %s", err)
// 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("create data directory fail : %s", err)
}
}
// mkdir /root/backup.
if err := os.MkdirAll("/root/backup", 0755); err != nil {
return fmt.Errorf("create backup directory fail : %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("xbstream and xcloud piped failure")
}
xbstream.Stderr = os.Stderr
xcloud.Stderr = os.Stderr
if err := xcloud.Start(); err != nil {
return fmt.Errorf("xcloud start failure : %s", err)
}
defer func() {
f.Close()
if err := xbstream.Start(); err != nil {
return fmt.Errorf("xbstream start failure : %s", err)
}
// Make error channels.
errCh := make(chan error, 2)
go func() {
errCh <- xcloud.Wait()
}()

restoresh := `#!/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
`
template_restore := template.New("restore.sh")
template_restore, err = template_restore.Parse(restoresh)
if err != nil {
return err
}
err2 := template_restore.Execute(f, struct {
Config
DataDir string
MyCnfMountPath string
}{
*cfg,
utils.DataVolumeMountPath,
utils.ConfVolumeMountPath + "/my.cnf",
})
if err2 != nil {
return err2
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.ConfVolumeMountPath+"/my.cnf", "--prepare", "--apply-log-only", "--target-dir=/root/backup")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("xtrabackup prepare and apply-log-only failure : %s", err)
}
// Xtrabackup prepare.
cmd = exec.Command(xtrabackupCommand, "--defaults-file="+utils.ConfVolumeMountPath+"/my.cnf", "--prepare", "--target-dir=/root/backup")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("xtrabackup prepare failure : %s", err)
}
// Xtrabackup copy-back to /var/lib/mysql.
cmd = exec.Command(xtrabackupCommand, "--defaults-file="+utils.ConfVolumeMountPath+"/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("xtrabackup copy-back failure : %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("chown mysql.mysql %s failure : %s", utils.DataVolumeMountPath, err)
}
return nil
}
14 changes: 2 additions & 12 deletions sidecar/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,8 @@ func runInitCommand(cfg *Config) error {

// run the restore
if len(cfg.XRestoreFrom) != 0 {
var restoreName string = "/restore.sh"
err_f := cfg.buildS3Restore(restoreName)
if err_f != nil {
return fmt.Errorf("build restore.sh fail : %s", err_f)
}
if err = os.Chmod(restoreName, os.FileMode(0755)); err != nil {
return fmt.Errorf("failed to chmod scripts: %s", err)
}
cmd := exec.Command("sh", "-c", restoreName)
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return fmt.Errorf("failed to disable the run restore: %s", err)
if err = cfg.executeS3Restore(cfg.XRestoreFrom); err != nil {
return fmt.Errorf("failed to restore from %s: %s", cfg.XRestoreFrom, err)
}
}

Expand Down

0 comments on commit aaa999b

Please sign in to comment.