/
main.go
73 lines (63 loc) · 1.83 KB
/
main.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
// 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 main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/pingcap/pd/v4/tools/pd-backup/pdbackup"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/pkg/transport"
)
var (
pdAddr = flag.String("pd", "http://127.0.0.1:2379", "pd address")
filePath = flag.String("file", "backup.json", "backup file path and name")
caPath = flag.String("cacert", "", "path of file that contains list of trusted SSL CAs")
certPath = flag.String("cert", "", "path of file that contains X509 certificate in PEM format")
keyPath = flag.String("key", "", "path of file that contains X509 key in PEM format")
)
const (
etcdTimeout = 3 * time.Second
)
func main() {
flag.Parse()
f, err := os.Create(*filePath)
checkErr(err)
defer f.Close()
urls := strings.Split(*pdAddr, ",")
tlsInfo := transport.TLSInfo{
CertFile: *certPath,
KeyFile: *keyPath,
TrustedCAFile: *caPath,
}
tlsConfig, err := tlsInfo.ClientConfig()
checkErr(err)
client, err := clientv3.New(clientv3.Config{
Endpoints: urls,
DialTimeout: etcdTimeout,
TLS: tlsConfig,
})
checkErr(err)
backInfo, err := pdbackup.GetBackupInfo(client, *pdAddr)
checkErr(err)
pdbackup.OutputToFile(backInfo, f)
fmt.Println("pd backup successful! dump file is:", *filePath)
}
func checkErr(err error) {
if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}