-
Notifications
You must be signed in to change notification settings - Fork 492
/
restore.go
101 lines (89 loc) · 3.28 KB
/
restore.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
// Copyright 2019 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package restore
import (
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/mholt/archiver"
"github.com/pingcap/tidb-operator/cmd/backup-manager/app/constants"
"github.com/pingcap/tidb-operator/cmd/backup-manager/app/util"
)
// RestoreOpts contains the input arguments to the restore command
type RestoreOpts struct {
Namespace string
TcName string
Password string
TidbSvc string
User string
RestoreName string
BackupPath string
BackupName string
}
func (ro *RestoreOpts) String() string {
return fmt.Sprintf("%s/%s", ro.Namespace, ro.TcName)
}
func (ro *RestoreOpts) getRestoreDataPath() string {
backupName := filepath.Base(ro.BackupPath)
NsClusterName := fmt.Sprintf("%s_%s", ro.Namespace, ro.TcName)
return filepath.Join(constants.BackupRootPath, NsClusterName, backupName)
}
func (ro *RestoreOpts) downloadBackupData(localPath string) error {
if err := util.EnsureDirectoryExist(filepath.Dir(localPath)); err != nil {
return err
}
remoteBucket := util.NormalizeBucketURI(ro.BackupPath)
rcCopy := exec.Command("rclone", constants.RcloneConfigArg, "copyto", remoteBucket, localPath)
if err := rcCopy.Start(); err != nil {
return fmt.Errorf("cluster %s, start rclone copyto command for download backup data %s falied, err: %v", ro, ro.BackupPath, err)
}
if err := rcCopy.Wait(); err != nil {
return fmt.Errorf("cluster %s, execute rclone copyto command for download backup data %s failed, err: %v", ro, ro.BackupPath, err)
}
return nil
}
func (ro *RestoreOpts) loadTidbClusterData(restorePath string) error {
if exist := util.IsDirExist(restorePath); !exist {
return fmt.Errorf("dir %s does not exist or is not a dir", restorePath)
}
args := []string{
fmt.Sprintf("-d=%s", restorePath),
fmt.Sprintf("-h=%s", ro.TidbSvc),
"-P=4000",
fmt.Sprintf("-u=%s", ro.User),
fmt.Sprintf("-p=%s", ro.Password),
}
loader := exec.Command("/loader", args...)
if err := loader.Start(); err != nil {
return fmt.Errorf("cluster %s, start loader command %v falied, err: %v", ro, args, err)
}
if err := loader.Wait(); err != nil {
return fmt.Errorf("cluster %s, execute loader command %v failed, err: %v", ro, args, err)
}
return nil
}
// unarchiveBackupData unarchive backup data to dest dir
func unarchiveBackupData(backupFile, destDir string) (string, error) {
var unarchiveBackupPath string
if err := util.EnsureDirectoryExist(destDir); err != nil {
return unarchiveBackupPath, err
}
backupName := strings.TrimSuffix(filepath.Base(backupFile), constants.DefaultArchiveExtention)
err := archiver.Unarchive(backupFile, destDir)
if err != nil {
return unarchiveBackupPath, fmt.Errorf("unarchive backup data %s to %s failed, err: %v", backupFile, destDir, err)
}
unarchiveBackupPath = filepath.Join(destDir, backupName)
return unarchiveBackupPath, nil
}