-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathrun.go
254 lines (210 loc) · 7.47 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
// +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 (
"compress/gzip"
"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/pkg/errors"
"github.com/spf13/cobra"
"google.golang.org/grpc"
)
// Restore is the sub-command used to restore a backup.
var Restore x.SubCommand
// LsBackup is the sub-command used to list the backups in a folder.
var LsBackup x.SubCommand
var opt struct {
backupId, 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")
flag.StringVarP(&opt.backupId, "backup_id", "", "", "The ID of the backup series to "+
"restore. If empty, it will restore the latest series.")
_ = 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)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
zero, err := grpc.DialContext(ctx, opt.zero,
grpc.WithBlock(),
grpc.WithInsecure())
if err != nil {
return errors.Wrapf(err, "Unable to connect to %s", opt.zero)
}
zc = pb.NewZeroClient(zero)
}
start = time.Now()
version, err := RunRestore(opt.pdir, opt.location, opt.backupId)
if err != nil {
return err
}
if version == 0 {
return errors.Errorf("Failed to obtain a restore version")
}
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 {
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, backupId string) (uint64, error) {
// 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, backupId, func(r io.Reader, groupId int) error {
dir := filepath.Join(pdir, fmt.Sprintf("p%d", groupId))
bo := badger.DefaultOptions(dir).WithSyncWrites(true).WithTableLoadingMode(
options.MemoryMap).WithValueThreshold(1 << 10).WithNumVersionsToKeep(math.MaxInt32)
db, err := badger.OpenManaged(bo)
if err != nil {
return err
}
defer db.Close()
fmt.Printf("Restoring groupId: %d\n", groupId)
if !pathExist(bo.Dir) {
fmt.Println("Creating new db:", bo.Dir)
}
gzReader, err := gzip.NewReader(r)
if err != nil {
return nil
}
return db.Load(gzReader, 16)
})
}
func runLsbackupCmd() error {
fmt.Println("Listing backups from:", opt.location)
manifests, err := ListManifests(opt.location)
if err != nil {
return errors.Wrapf(err, "while listing manifests")
}
fmt.Printf("Name\tSince\tGroups\n")
for path, manifest := range manifests {
fmt.Printf("%v\t%v\t%v\n", path, manifest.Since, manifest.Groups)
}
return nil
}