forked from percona/percona-backup-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore.go
129 lines (110 loc) · 3.14 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
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
package snapshot
import (
"io"
"github.com/mongodb/mongo-tools/common/options"
"github.com/mongodb/mongo-tools/mongorestore"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/mongo/writeconcern"
"github.com/percona/percona-backup-mongodb/pbm"
)
const (
preserveUUID = true
batchSizeDefault = 100
numInsertionWorkersDefault = 5
)
var ExcludeFromRestore = []string{
pbm.DB + "." + pbm.CmdStreamCollection,
pbm.DB + "." + pbm.LogCollection,
pbm.DB + "." + pbm.ConfigCollection,
pbm.DB + "." + pbm.BcpCollection,
pbm.DB + "." + pbm.RestoresCollection,
pbm.DB + "." + pbm.LockCollection,
pbm.DB + "." + pbm.LockOpCollection,
pbm.DB + "." + pbm.PITRChunksCollection,
pbm.DB + "." + pbm.AgentsStatusCollection,
pbm.DB + "." + pbm.PBMOpLogCollection,
"config.version",
"config.mongos",
"config.lockpings",
"config.locks",
"config.system.sessions",
"config.cache.*",
"config.shards",
"config.transactions",
"config.transaction_coordinators",
"config.changelog",
"config.image_collection",
"admin.system.version",
"config.system.indexBuilds",
// deprecated PBM collections, keep it here not to bring back from old backups
pbm.DB + ".pbmBackups.old",
pbm.DB + ".pbmPITRChunks.old",
}
type restorer struct{ *mongorestore.MongoRestore }
func NewRestore(uri string, cfg *pbm.Config) (io.ReaderFrom, error) {
topts := options.New("mongorestore",
"0.0.1",
"none",
"",
true,
options.EnabledOptions{
Auth: true,
Connection: true,
Namespace: true,
URI: true,
})
var err error
topts.URI, err = options.NewURI(uri)
if err != nil {
return nil, errors.Wrap(err, "parse connection string")
}
err = topts.NormalizeOptionsAndURI()
if err != nil {
return nil, errors.Wrap(err, "parse opts")
}
topts.Direct = true
topts.WriteConcern = writeconcern.New(writeconcern.WMajority())
batchSize := batchSizeDefault
if cfg.Restore.BatchSize > 0 {
batchSize = cfg.Restore.BatchSize
}
numInsertionWorkers := numInsertionWorkersDefault
if cfg.Restore.NumInsertionWorkers > 0 {
numInsertionWorkers = cfg.Restore.NumInsertionWorkers
}
mopts := mongorestore.Options{}
mopts.ToolOptions = topts
mopts.InputOptions = &mongorestore.InputOptions{
Archive: "-",
}
mopts.OutputOptions = &mongorestore.OutputOptions{
BulkBufferSize: batchSize,
BypassDocumentValidation: true,
Drop: true,
NumInsertionWorkers: numInsertionWorkers,
NumParallelCollections: 1,
PreserveUUID: preserveUUID,
StopOnError: true,
WriteConcern: "majority",
NoIndexRestore: true,
}
mopts.NSOptions = &mongorestore.NSOptions{
NSExclude: ExcludeFromRestore,
}
mr, err := mongorestore.New(mopts)
if err != nil {
return nil, errors.Wrap(err, "create mongorestore obj")
}
mr.SkipUsersAndRoles = true
return &restorer{mr}, nil
}
func (r *restorer) ReadFrom(from io.Reader) (int64, error) {
defer r.Close()
r.InputReader = from
rdumpResult := r.Restore()
if rdumpResult.Err != nil {
return 0, errors.Wrapf(rdumpResult.Err, "restore mongo dump (successes: %d / fails: %d)",
rdumpResult.Successes, rdumpResult.Failures)
}
return 0, nil
}