-
Notifications
You must be signed in to change notification settings - Fork 489
/
restore.go
87 lines (80 loc) · 2.83 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
// 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"
backupUtil "github.com/pingcap/tidb-operator/cmd/backup-manager/app/util"
"github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
"github.com/pingcap/tidb-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/klog"
)
type Options struct {
backupUtil.GenericOptions
}
func (ro *Options) restoreData(restore *v1alpha1.Restore) error {
clusterNamespace := restore.Spec.BR.ClusterNamespace
if restore.Spec.BR.ClusterNamespace == "" {
clusterNamespace = restore.Namespace
}
args, err := constructBROptions(restore)
if err != nil {
return err
}
args = append(args, fmt.Sprintf("--pd=%s-pd.%s:2379", restore.Spec.BR.Cluster, clusterNamespace))
if restore.Spec.BR.TLSCluster != nil && restore.Spec.BR.TLSCluster.Enabled {
args = append(args, fmt.Sprintf("--ca=%s", path.Join(util.ClusterClientTLSPath, corev1.ServiceAccountRootCAKey)))
args = append(args, fmt.Sprintf("--cert=%s", path.Join(util.ClusterClientTLSPath, corev1.TLSCertKey)))
args = append(args, fmt.Sprintf("--key=%s", path.Join(util.ClusterClientTLSPath, corev1.TLSPrivateKeyKey)))
}
var restoreType string
if restore.Spec.Type == "" {
restoreType = string(v1alpha1.BackupTypeFull)
} else {
restoreType = string(restore.Spec.Type)
}
fullArgs := []string{
"restore",
restoreType,
}
fullArgs = append(fullArgs, args...)
klog.Infof("Running br command with args: %v", fullArgs)
output, err := exec.Command("br", fullArgs...).CombinedOutput()
if err != nil {
return fmt.Errorf("cluster %s, execute br command %v failed, output: %s, err: %v", ro, fullArgs, string(output), err)
}
klog.Infof("Restore data for cluster %s successfully, output: %s", ro, string(output))
return nil
}
func constructBROptions(restore *v1alpha1.Restore) ([]string, error) {
args, err := backupUtil.ConstructBRGlobalOptionsForRestore(restore)
if err != nil {
return nil, err
}
config := restore.Spec.BR
if config.Concurrency != nil {
args = append(args, fmt.Sprintf("--concurrency=%d", *config.Concurrency))
}
if config.Checksum != nil {
args = append(args, fmt.Sprintf("--checksum=%t", *config.Checksum))
}
if config.RateLimit != nil {
args = append(args, fmt.Sprintf("--ratelimit=%d", *config.RateLimit))
}
if config.OnLine != nil {
args = append(args, fmt.Sprintf("--online=%t", *config.OnLine))
}
return args, nil
}