-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathbackup.go
72 lines (63 loc) · 1.7 KB
/
backup.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
/*
* Copyright 2018 Dgraph Labs, Inc. All rights reserved.
*
*/
package backup
import (
"context"
"github.com/dgraph-io/badger"
"github.com/dgraph-io/dgraph/posting"
"github.com/dgraph-io/dgraph/protos/pb"
"github.com/dgraph-io/dgraph/stream"
"github.com/dgraph-io/dgraph/x"
"github.com/golang/glog"
)
// Request has all the information needed to perform a backup.
type Request struct {
DB *badger.DB // Badger pstore managed by this node.
Sizex uint64 // approximate upload size
Backup *pb.BackupRequest
}
// Process uses the request values to create a stream writer then hand off the data
// retrieval to stream.Orchestrate. The writer will create all the fd's needed to
// collect the data and later move to the target.
// Returns errors on failure, nil on success.
func (r *Request) Process(ctx context.Context) error {
w, err := r.newWriter()
if err != nil {
return err
}
sl := stream.Lists{Stream: w, DB: r.DB}
sl.ChooseKeyFunc = nil
sl.ItemToKVFunc = func(key []byte, itr *badger.Iterator) (*pb.KV, error) {
item := itr.Item()
pk := x.Parse(key)
if pk.IsSchema() {
val, err := item.ValueCopy(nil)
if err != nil {
return nil, err
}
kv := &pb.KV{
Key: key,
Val: val,
UserMeta: []byte{item.UserMeta()},
Version: item.Version(),
}
return kv, nil
}
l, err := posting.ReadPostingList(key, itr)
if err != nil {
return nil, err
}
return l.MarshalToKv()
}
glog.V(2).Infof("Backup started ...")
if err = sl.Orchestrate(ctx, "Backup", r.Backup.ReadTs); err != nil {
return err
}
if err = w.cleanup(); err != nil {
return err
}
glog.Infof("Backup complete: group %d at %d", r.Backup.GroupId, r.Backup.ReadTs)
return nil
}