-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathrun.go
259 lines (217 loc) · 7.24 KB
/
run.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
// +build !oss
/*
* Copyright 2018 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Dgraph Community License (the "License"); you
* may not use this file except in compliance with the License. You
* may obtain a copy of the License at
*
* https://github.com/dgraph-io/dgraph/blob/master/licenses/DCL.txt
*/
package backup
import (
"context"
"fmt"
"io"
"math"
"os"
"path/filepath"
"time"
"github.com/dgraph-io/badger"
"github.com/dgraph-io/badger/options"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)
var Restore x.SubCommand
var LsBackup x.SubCommand
var opt struct {
location, pdir, zero string
}
func init() {
initRestore()
initBackupLs()
}
func initRestore() {
Restore.Cmd = &cobra.Command{
Use: "restore",
Short: "Run Dgraph (EE) Restore backup",
Long: `
Restore loads objects created with the backup feature in Dgraph Enterprise Edition (EE).
Backups are originated from HTTP at /admin/backup, then can be restored using CLI restore
command. Restore is intended to be used with new Dgraph clusters in offline state.
The --location flag indicates a source URI with Dgraph backup objects. This URI supports all
the schemes used for backup.
Source URI formats:
[scheme]://[host]/[path]?[args]
[scheme]:///[path]?[args]
/[path]?[args] (only for local or NFS)
Source URI parts:
scheme - service handler, one of: "s3", "minio", "file"
host - remote address. ex: "dgraph.s3.amazonaws.com"
path - directory, bucket or container at target. ex: "/dgraph/backups/"
args - specific arguments that are ok to appear in logs.
The --posting flag sets the posting list parent dir to store the loaded backup files.
Using the --zero flag will use a Dgraph Zero address to update the start timestamp using
the restored version. Otherwise, the timestamp must be manually updated through Zero's HTTP
'assign' command.
Dgraph backup creates a unique backup object for each node group, and restore will create
a posting directory 'p' matching the backup group ID. Such that a backup file
named '.../r32-g2.backup' will be loaded to posting dir 'p2'.
Usage examples:
# Restore from local dir or NFS mount:
$ dgraph restore -p . -l /var/backups/dgraph
# Restore from S3:
$ dgraph restore -p /var/db/dgraph -l s3://s3.us-west-2.amazonaws.com/srfrog/dgraph
# Restore from dir and update Ts:
$ dgraph restore -p . -l /var/backups/dgraph -z localhost:5080
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
defer x.StartProfile(Restore.Conf).Stop()
if err := runRestoreCmd(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}
flag := Restore.Cmd.Flags()
flag.StringVarP(&opt.location, "location", "l", "",
"Sets the source location URI (required).")
flag.StringVarP(&opt.pdir, "postings", "p", "",
"Directory where posting lists are stored (required).")
flag.StringVarP(&opt.zero, "zero", "z", "", "gRPC address for Dgraph zero. ex: localhost:5080")
_ = Restore.Cmd.MarkFlagRequired("postings")
_ = Restore.Cmd.MarkFlagRequired("location")
}
func initBackupLs() {
LsBackup.Cmd = &cobra.Command{
Use: "lsbackup",
Short: "List info on backups in given location",
Long: `
lsbackup looks at a location where backups are stored and prints information about them.
Backups are originated from HTTP at /admin/backup, then can be restored using CLI restore
command. Restore is intended to be used with new Dgraph clusters in offline state.
The --location flag indicates a source URI with Dgraph backup objects. This URI supports all
the schemes used for backup.
Source URI formats:
[scheme]://[host]/[path]?[args]
[scheme]:///[path]?[args]
/[path]?[args] (only for local or NFS)
Source URI parts:
scheme - service handler, one of: "s3", "minio", "file"
host - remote address. ex: "dgraph.s3.amazonaws.com"
path - directory, bucket or container at target. ex: "/dgraph/backups/"
args - specific arguments that are ok to appear in logs.
Dgraph backup creates a unique backup object for each node group, and restore will create
a posting directory 'p' matching the backup group ID. Such that a backup file
named '.../r32-g2.backup' will be loaded to posting dir 'p2'.
Usage examples:
# Run using location in S3:
$ dgraph lsbackup -l s3://s3.us-west-2.amazonaws.com/srfrog/dgraph
`,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
defer x.StartProfile(Restore.Conf).Stop()
if err := runLsbackupCmd(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}
flag := LsBackup.Cmd.Flags()
flag.StringVarP(&opt.location, "location", "l", "",
"Sets the source location URI (required).")
_ = LsBackup.Cmd.MarkFlagRequired("location")
}
func runRestoreCmd() error {
var (
start time.Time
zc pb.ZeroClient
)
fmt.Println("Restoring backups from:", opt.location)
fmt.Println("Writing postings to:", opt.pdir)
// TODO: Remove this dependency on Zero. It complicates restore for the end
// user.
if opt.zero != "" {
fmt.Println("Updating Zero timestamp at:", opt.zero)
zero, err := grpc.Dial(opt.zero,
grpc.WithBlock(),
grpc.WithInsecure(),
grpc.WithTimeout(10*time.Second))
if err != nil {
return x.Wrapf(err, "Unable to connect to %s", opt.zero)
}
zc = pb.NewZeroClient(zero)
}
start = time.Now()
version, err := runRestore(opt.pdir, opt.location)
if err != nil {
return err
}
if version == 0 {
return x.Errorf("Failed to obtain a restore version")
}
if glog.V(2) {
fmt.Printf("Restore version: %d\n", version)
}
if zc != nil {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
_, err = zc.Timestamps(ctx, &pb.Num{Val: version})
if err != nil {
// Let the user know so they can do this manually.
fmt.Printf("Failed to assign timestamp %d in Zero: %v", version, err)
}
}
fmt.Printf("Restore: Time elapsed: %s\n", time.Since(start).Round(time.Second))
return nil
}
// runRestore calls badger.Load and tries to load data into a new DB.
func runRestore(pdir, location string) (uint64, error) {
bo := badger.DefaultOptions
bo.SyncWrites = true
bo.TableLoadingMode = options.MemoryMap
bo.ValueThreshold = 1 << 10
bo.NumVersionsToKeep = math.MaxInt32
if !glog.V(2) {
bo.Logger = nil
}
// Scan location for backup files and load them. Each file represents a node group,
// and we create a new p dir for each.
return Load(location, func(r io.Reader, groupId int) error {
bo := bo
bo.Dir = filepath.Join(pdir, fmt.Sprintf("p%d", groupId))
bo.ValueDir = bo.Dir
db, err := badger.OpenManaged(bo)
if err != nil {
return err
}
defer db.Close()
if glog.V(2) {
fmt.Printf("Restoring groupId: %d\n", groupId)
if !pathExist(bo.Dir) {
fmt.Println("Creating new db:", bo.Dir)
}
}
return db.Load(r)
})
}
func runLsbackupCmd() error {
fmt.Println("Listing backups from:", opt.location)
manifests, err := ListManifests(opt.location)
if err != nil {
return x.Errorf("Error while listing manifests: %v", err.Error())
}
fmt.Printf("Name\tVersion\tReadTs\tGroups\n")
for _, manifest := range manifests {
fmt.Printf("%v\t%v\t%v\t%v\n",
manifest.FileName,
manifest.Manifest.Version,
manifest.Manifest.ReadTs,
manifest.Manifest.Groups)
}
return nil
}