-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudbackup-prepare.go
More file actions
250 lines (196 loc) · 5.74 KB
/
cloudbackup-prepare.go
File metadata and controls
250 lines (196 loc) · 5.74 KB
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
package main
import (
"compress/zlib"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"fmt"
"github.com/golang/snappy"
goversion "github.com/hashicorp/go-version"
flag "github.com/spf13/pflag"
"io"
"os"
)
var (
version string
showVersion bool
inputFile string
outputFile string
encryptionKey string
agentVersion string
decompress bool
decrypt bool
)
func init() {
flag.BoolVarP(&showVersion, "version", "v", false, "Output version information and exit")
flag.StringVarP(&inputFile, "backup-file", "i", "", "Binlogic CloudBackup file path")
flag.StringVarP(&outputFile, "output-file", "o", "", "Outfile to save backup decrypted and uncompressed")
flag.StringVarP(&encryptionKey, "encryption-key", "e", "", "Encryption key to decrypt backup file")
flag.StringVarP(&agentVersion, "agent-version", "", "", "Agent version used to take the backup (if empty it's assumed <= 1.2.0)")
flag.BoolVarP(&decompress, "decompress", "z", false, "If agent version >= 1.10 then you need to specify "+
"wether the backup needs to be decompressed or not")
flag.BoolVarP(&decrypt, "decrypt", "y", false, "If agent version >= 1.10 then you need to specify "+
"wether the backup needs to be decrypted or not")
}
func showError(message error) {
fmt.Fprintf(os.Stderr, "%s\n", message)
os.Exit(1)
}
func parseArgs(args []string) error {
flag.CommandLine.Parse(args[1:])
if showVersion {
fmt.Fprintf(os.Stderr, "%s\n", version)
os.Exit(0)
}
if len(inputFile) == 0 {
return fmt.Errorf("Backup file is mandatory. Use -i option to pass it")
}
if len(outputFile) == 0 {
return fmt.Errorf("Output file is mandatory. Use -o option to pass it")
}
if decrypt && len(encryptionKey) == 0 {
return fmt.Errorf("Encryption key is mandatory. Use -e option to pass it")
}
return nil
}
func main() {
err := parseArgs(os.Args)
if err != nil {
showError(err)
}
err = prepareBackupFile(inputFile, encryptionKey, outputFile)
if err != nil {
showError(err)
}
fmt.Fprintf(os.Stderr, "Process completed successfully\n")
os.Exit(0)
}
func validateInputFile(filename string) error {
finfo, err := os.Stat(filename)
if err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("Backup file doesn't exists")
}
}
if finfo.IsDir() {
return fmt.Errorf("Expecting a file but %s is a directory", filename)
}
return nil
}
func validateOutputFile(filename string) error {
_, err := os.Stat(filename)
if err == nil {
return fmt.Errorf("Output file exists %s. Please remove it", filename)
}
return nil
}
func getCipherStream(key string) (cipher.Stream, error) {
keyBytes, err := base64.URLEncoding.DecodeString(key)
if err != nil {
return nil, err
}
block, err := aes.NewCipher(keyBytes)
if err != nil {
return nil, err
}
var iv [aes.BlockSize]byte
return cipher.NewOFB(block, iv[:]), nil
}
func getCipherReader(key string, wrappedReader io.Reader) (io.Reader, error) {
if key == "" {
return wrappedReader, nil
}
stream, err := getCipherStream(key)
if err != nil {
return nil, err
}
return &cipher.StreamReader{S: stream, R: wrappedReader}, nil
}
func prepareBackupFile(filename, encryptionKey, output string) error {
err := validateInputFile(filename)
if err != nil {
return fmt.Errorf("%s", err)
}
err = validateOutputFile(output)
if err != nil {
return fmt.Errorf("%s", err)
}
f, err := os.Open(filename)
if err != nil {
return fmt.Errorf("Fail to open %s: %s", filename, err)
}
defer f.Close()
is120OrLess, err := versionConstraint(agentVersion, "<= 1.2.0")
if err != nil {
return fmt.Errorf("%s creating version constraint", err)
}
is110OrLess, err := versionConstraint(agentVersion, "<= 1.10.0")
if err != nil {
return fmt.Errorf("%s creating version constraint", err)
}
var reader io.Reader
if agentVersion == "" || is120OrLess {
fmt.Fprint(os.Stderr, "Using old zlib reader\n")
zipReader, err := zlib.NewReader(f)
if err != nil {
return fmt.Errorf("Fail opening zlib reader: %s. Check the agent version you took "+
"the backup with and pass it with the --agent-version option", err)
}
reader, err = getCipherReader(encryptionKey, zipReader)
if err != nil {
return fmt.Errorf("Fail to decrypt file %s using %s. Be sure encryption key is correct",
filename, encryptionKey)
}
} else if is110OrLess {
fmt.Fprint(os.Stderr, "Using snappy reader\n")
cipherReader, err := getCipherReader(encryptionKey, f)
if err != nil {
return fmt.Errorf("%s while creating encrypted stream", err)
}
reader = snappy.NewReader(cipherReader)
} else {
reader = f
if decrypt {
fmt.Fprint(os.Stderr, "Using cipher reader\n")
cipherReader, err := getCipherReader(encryptionKey, f)
if err != nil {
return fmt.Errorf("%s while creating encrypted stream", err)
}
reader = cipherReader
} else {
fmt.Fprint(os.Stderr, "Not decrypting backup stream, if your file was "+
"encrypted, the output file may be corrupt\n")
}
if decompress {
fmt.Fprint(os.Stderr, "Using snappy reader\n")
reader = snappy.NewReader(reader)
} else {
fmt.Fprint(os.Stderr, "Not decompressing backup stream, if your file was "+
"compressed, the output file may be corrupt\n")
}
}
of, err := os.Create(output)
if err != nil {
return fmt.Errorf("Failed to create %s: %s", output, err)
}
defer of.Close()
_, err = io.Copy(of, reader)
if err != nil {
return fmt.Errorf("Fail while preparing backup file: %s", err)
}
return nil
}
func versionConstraint(ver, constraint string) (bool, error) {
if ver == "0.0.0" || ver == "develop" || ver == "" {
return false, nil
}
agentV, err := goversion.NewVersion(ver)
if err != nil {
return false, err
}
if c, err := goversion.NewConstraint(constraint); err != nil {
return false, err
} else {
return c.Check(agentV), nil
}
}