-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo.go
199 lines (151 loc) · 4.39 KB
/
mongo.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
package main
import (
"bytes"
"fmt"
"os/exec"
"sync"
"time"
_ "github.com/lib/pq"
log "github.com/sirupsen/logrus"
)
type mongoConfig struct {
host string
replicaSet string
port int
user string
authSource string
password string
database string
caCert string
tls bool
clientCert string
}
func (mongo mongoConfig) dump(sb s3Backend, publicKeyPath, database string) error {
log.Info("Mongo dump started")
today := time.Now().Format("20060102150405")
mongo.database = database
dumpCommand := buildDumpCommand(mongo)
log.Debugln(dumpCommand)
cmd := exec.Command("sh", "-c", dumpCommand)
var out bytes.Buffer
cmd.Stdout = &out
var errMsg bytes.Buffer
cmd.Stderr = &errMsg
err := cmd.Run()
if err != nil {
return err
}
log.Debug("Mongo dump command successfully executed")
wg := sync.WaitGroup{}
mongoArchive := today + "-" + database + ".archive"
wr, err := sb.NewFileWriter(mongoArchive, &wg)
if err != nil {
return fmt.Errorf("Could not open backup file for writing: %s", err)
}
log.Debugf("Mongo archive file %v ready for writing", mongoArchive)
privateKey, publicKeyList, err := getKeys(publicKeyPath)
if err != nil {
return fmt.Errorf("Could not retrieve public key or generate private key: %s", err)
}
log.Debug("Public key retrieved and private key successfully created")
e, err := newEncryptor(publicKeyList, privateKey, wr)
if err != nil {
return fmt.Errorf("Could not initialize encryptor: %s", err)
}
log.Debug("Encryption initialized")
c, err := newCompressor(e)
if err != nil {
return fmt.Errorf("Could not initialize compressor: %s", err)
}
log.Debug("Compression initialized")
_, err = c.Write(out.Bytes())
if err != nil {
log.Errorf("Could not encrypt/write: %s", err)
}
if err := c.Close(); err != nil {
log.Errorf("Could not close compressor: %v", err)
}
if err := e.Close(); err != nil {
log.Errorf("Could not close encryptor: %v", err)
}
if err := wr.Close(); err != nil {
log.Errorf("Could not close destination file: %v", err)
}
wg.Wait()
log.Info("Mongo archive is compressed and encrypted")
return nil
}
func (mongo mongoConfig) restore(sb s3Backend, privateKeyPath, archive, c4ghPassword string) error {
log.Info("Start restoration from mongo archive")
fr, err := sb.NewFileReader(archive)
if err != nil {
return err
}
defer fr.Close()
log.Debug("Read mongo file")
privateKey, err := getPrivateKey(privateKeyPath, c4ghPassword)
if err != nil {
return fmt.Errorf("Could not retrieve private key: %s", err)
}
log.Debug("Private key retrieved")
r, err := newDecryptor(privateKey, fr)
if err != nil {
return fmt.Errorf("Could not initialise decryptor: %s", err)
}
log.Debug("Decryption initialized")
d, err := newDecompressor(r)
if err != nil {
return fmt.Errorf("Could not initialise decompressor: %s", err)
}
log.Debug("Decompression initialized")
restoreCommand := buildRestoreCommand(mongo)
log.Debugln(restoreCommand)
cmd := exec.Command("sh", "-c", restoreCommand)
var in bytes.Buffer
cmd.Stdin = &in
_, err = in.ReadFrom(d)
if err != nil {
return fmt.Errorf("Could not read datastream: %s", err)
}
if err := d.Close(); err != nil {
log.Errorf("Could not close decompressor: %v", err)
}
if err := r.Close(); err != nil {
log.Errorf("Could not close decryptor: %v", err)
}
log.Debug("Data read successfully")
var errMsg bytes.Buffer
cmd.Stderr = &errMsg
err = cmd.Run()
if err != nil {
return err
}
log.Debug("Importing mongo data finished")
return nil
}
func buildRestoreCommand(mongo mongoConfig) string {
cmd := fmt.Sprintf("mongorestore --uri='mongodb://%s:%s@%s/?authSource=admin", mongo.user, mongo.password, mongo.host)
if mongo.replicaSet != "" {
cmd += fmt.Sprintf("&replicaSet=%s'", mongo.replicaSet)
} else {
cmd += "'"
}
if mongo.tls {
cmd += fmt.Sprintf(" --ssl --sslCAFile=%s --sslPEMKeyFile=%s", mongo.caCert, mongo.clientCert)
}
cmd += " --archive"
return cmd
}
func buildDumpCommand(mongo mongoConfig) string {
cmd := fmt.Sprintf("mongodump --uri='mongodb://%s:%s@%s/%s?authSource=admin", mongo.user, mongo.password, mongo.host, mongo.database)
if mongo.replicaSet != "" {
cmd += fmt.Sprintf("&replicaSet=%s&readPreference=secondary'", mongo.replicaSet)
} else {
cmd += "'"
}
if mongo.tls {
cmd += fmt.Sprintf(" --ssl --sslCAFile=%s --sslPEMKeyFile=%s", mongo.caCert, mongo.clientCert)
}
cmd += " --archive"
return cmd
}