-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
465 lines (456 loc) · 12.4 KB
/
main.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
package main
import (
"encoding/hex"
"errors"
"log"
"os"
"strconv"
"time"
"github.com/leijurv/gb/compression"
"github.com/leijurv/gb/share"
"github.com/leijurv/gb/proxy"
"github.com/araddon/dateparse"
"github.com/leijurv/gb/backup"
"github.com/leijurv/gb/config"
"github.com/leijurv/gb/db"
"github.com/leijurv/gb/download"
"github.com/leijurv/gb/dupes"
"github.com/leijurv/gb/gbfs"
"github.com/leijurv/gb/history"
"github.com/leijurv/gb/paranoia"
"github.com/leijurv/gb/relay"
"github.com/leijurv/gb/replicate"
"github.com/leijurv/gb/storage"
"github.com/leijurv/gb/utils"
"github.com/urfave/cli"
)
func main() {
defer db.ShutdownDatabase()
app := cli.NewApp()
app.Name = "gb"
app.Usage = "backup your files"
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "config-file",
Value: config.HomeDir + "/.gb.conf",
Usage: "path to where you want your config file",
Destination: &config.ConfigLocation,
},
&cli.StringFlag{
Name: "database-file",
Usage: "path to where the database file is (overrides path from config file)",
Destination: &config.DatabaseLocation,
},
&cli.BoolFlag{
Name: "no-log-timestamps",
Usage: "do not include timestamps in logs",
},
}
app.Before = func(c *cli.Context) error {
if c.Bool("no-log-timestamps") {
log.SetFlags(0)
}
// we don't know where the database should be read from until after the "config-file" flag is parsed
db.SetupDatabase()
compression.CheckCompression()
return nil
}
app.Commands = []cli.Command{
{
Name: "backup",
Usage: "backup a directory (or file)",
Flags: []cli.Flag{&cli.BoolFlag{
Name: "no-backup-database",
Usage: "do not upload the database",
}},
Action: func(c *cli.Context) error {
if len(storage.GetAll()) == 0 {
return errors.New("make a storage first")
}
ch, ok := relay.RemoteSplitter()
if !ok {
ch = backup.MakeDefaultServiceFactory()
}
paths := append([]string{c.Args().First()}, c.Args().Tail()...) // even if no argument (like: "gb backup"), backup current directory by passing one empty string arg
backup.Backup(paths, ch)
if !c.Bool("no-backup-database") {
backup.BackupDB()
}
return nil
},
},
{
Name: "cat",
Usage: "dump a file to stdout by its sha256. always fetches from storage, never uses your filesystem",
Action: func(c *cli.Context) error {
data, err := hex.DecodeString(c.Args().First())
if err != nil {
return err
}
if len(data) != 32 {
return errors.New("wrong length")
}
utils.Copy(os.Stdout, download.CatEz(data))
return nil
},
},
{
Name: "paranoia",
Usage: "yeah you SAY you backed up the files but how do i KNOW (you can also directly put a path/to/file instead of files/storage/db)",
Subcommands: []cli.Command{
{
Name: "files",
Usage: "download files and calculate their hashes",
Action: func(c *cli.Context) error {
if len(storage.GetAll()) == 0 {
return errors.New("make a storage first")
}
paranoia.TestAllFiles()
return nil
},
},
{
Name: "storage",
Usage: "fetch all metadata (aka: list all blobs) in storage and ensure their size and checksum is what we expect",
Action: func(c *cli.Context) error {
if len(storage.GetAll()) == 0 {
return errors.New("make a storage first")
}
paranoia.StorageParanoia()
return nil
},
},
{
Name: "db",
Usage: "make sure the db is internally consistent",
Action: func(c *cli.Context) error {
paranoia.DBParanoia()
return nil
},
},
{
Name: "blob",
Usage: "fetch blobs from storage and ensure that all contents are correct",
Flags: []cli.Flag{
cli.StringFlag{
Name: "label",
Usage: "storage label",
},
},
Action: func(c *cli.Context) error {
paranoia.BlobParanoia(c.String("label"))
return nil
},
},
},
Action: func(c *cli.Context) error {
path := c.Args().First()
if path == "" {
return errors.New("Must give me a path to paranoia. Use \".\" for current directory. You can also `paranoia db` or `paranoia storage` or `paranoia files`.")
}
paranoia.ParanoiaFile(path)
return nil
},
},
{
Name: "storage",
Usage: "where do i store the data",
Subcommands: []cli.Command{
{
Name: "add",
Subcommands: []cli.Command{
{
Name: "s3",
Flags: []cli.Flag{
cli.StringFlag{
Name: "label, l",
Usage: "human readable label, can be anything",
},
cli.StringFlag{
Name: "bucket, b",
Usage: "s3 bucket",
},
cli.StringFlag{
Name: "path, p",
Usage: "path in the bucket, just put / if you want gb to write to the root",
},
cli.StringFlag{
Name: "region, r",
Usage: "AWS region of your bucket, e.g. us-east-1",
},
cli.StringFlag{
Name: "keyid",
Usage: "AWS key id (the shorter one)",
},
cli.StringFlag{
Name: "secretkey",
Usage: "AWS secret key (the longer one)",
},
cli.StringFlag{
Name: "endpoint",
Usage: "Override the s3 endpoint to another, for example you could put: backblazeb2.com",
},
},
Action: func(c *cli.Context) error {
for _, thing := range []string{"label", "bucket", "path", "region", "keyid", "secretkey"} {
if c.String(thing) == "" {
return errors.New("give me a " + thing)
}
}
storage.NewS3Storage(c.String("label"), c.String("bucket"), c.String("path"), c.String("region"), c.String("keyid"), c.String("secretkey"), c.String("endpoint"))
return nil
},
},
{
Name: "gdrive",
Flags: []cli.Flag{
cli.StringFlag{
Name: "label, l",
Usage: "human readable label, can be anything",
},
},
Action: func(c *cli.Context) error {
if c.String("label") == "" {
return errors.New("give me a label")
}
storage.NewGDriveStorage(c.String("label"))
return nil
},
},
},
},
},
},
{
Name: "history",
Usage: "give revision history of a specific file (not a directory)",
Action: func(c *cli.Context) error {
history.FileHistory(c.Args().First())
return nil
},
},
{
Name: "search",
Usage: "search for any path containing the given argument",
Action: func(c *cli.Context) error {
history.Search(c.Args().First())
return nil
},
},
{
Name: "ls",
Usage: "list backup info about files in a directory",
Action: func(c *cli.Context) error {
history.DirHistory(c.Args().First())
return nil
},
},
{
Name: "mnemonic",
Usage: "print out database encryption key mnemonic",
Action: func(c *cli.Context) error {
backup.Mnemonic(backup.DBKey())
return nil
},
},
{
Name: "fdupes",
Usage: "print out duplicated file paths in fdupes format, for consumption by duperemove",
Flags: []cli.Flag{
cli.StringFlag{
Name: "since",
Usage: "timestamp of the most recent successful and completed deduplication, so that the output can be filtered to only groups that contain files that were updated since then",
},
},
Action: func(c *cli.Context) error {
timestamp, err := parseTimestamp(c.String("at"))
if err != nil {
return err
}
if c.String("since") != "" {
log.Println("Interpreting provided date as:", time.Unix(timestamp, 0).Format(time.RFC3339)) // so as to not misrepresent what will happen, this conversion intentionally rounds to nearest second
}
dupes.PrintDupes(timestamp)
return nil
},
},
{
Name: "restore",
Usage: "restore your files =O",
Flags: []cli.Flag{
cli.StringFlag{
Name: "at, to, timestamp",
Usage: "timestamp to which this should be restored",
},
},
Action: func(c *cli.Context) error {
timestamp, err := parseTimestamp(c.String("at"))
if err != nil {
return err
}
// restore prints out the timestamp for confirmation, no need to do it twice
download.Restore(c.Args().Get(0), c.Args().Get(1), timestamp)
return nil
},
},
{
Name: "sha256",
Usage: "sha256 something",
Action: func(c *cli.Context) error {
log.SetFlags(0)
hs := utils.NewSHA256HasherSizer()
utils.Copy(&hs, os.Stdin)
log.Println(hex.EncodeToString(hs.Hash()))
return nil
},
},
{
Name: "proxy",
Usage: "proxy",
Flags: []cli.Flag{
cli.StringFlag{
Name: "label",
Usage: "storage label",
},
cli.StringFlag{
Name: "base",
Usage: "base path",
},
cli.StringFlag{
Name: "listen",
Usage: "ip and port to listen on",
Value: "127.0.0.1:7893",
},
cli.BoolFlag{
Name: "iunderstandthisisnotauthenticated",
Usage: "confirm this is notauthenticated",
},
},
Action: func(c *cli.Context) error {
if !c.Bool("iunderstandthisisnotauthenticated") {
log.Println("This command is NOT authenticated. It allows ANYONE who can connect to " + c.String("listen") + " access to browse and download your files. Confirm this by adding the option `--iunderstandthisisnotauthenticated`")
log.Println("To share individual files in an authenticated public-facing way, consider `gb share` and `gb shared` instead")
return nil
}
proxy.Proxy(c.String("label"), c.String("base"), c.String("listen"))
return nil
},
},
{
Name: "restoredb",
Usage: "restore an encrypted and compressed database backup",
Action: func(c *cli.Context) error {
download.RestoreDB(c.Args().First())
return nil
},
},
{
Name: "replicate",
Usage: "replicate",
Flags: []cli.Flag{
cli.StringFlag{
Name: "label",
Usage: "storage label",
},
},
Action: func(c *cli.Context) error {
replicate.ReplicateBlobs(c.String("label"))
return nil
},
},
{
Name: "mount",
Usage: "mount a readonly FUSE filesystem",
Flags: []cli.Flag{
cli.StringFlag{
Name: "at, timestamp",
Usage: "timestamp files should be chosen from",
},
cli.StringFlag{
Name: "path",
Usage: "source path where files come from",
Value: "/",
},
},
Action: func(c *cli.Context) error {
timestamp, err := parseTimestamp(c.String("at"))
if err != nil {
return err
}
if timestamp == 0 {
timestamp = time.Now().Unix()
}
gbfs.Mount(c.Args().First(), c.String("path"), timestamp)
return nil
},
},
{
Name: "stat",
Usage: "stat existing files and count how many files are not backed up",
Action: func(c *cli.Context) error {
paths := append([]string{c.Args().First()}, c.Args().Tail()...) // even if no argument (like: "gb backup"), backup current directory by passing one empty string arg
backup.DryBackup(paths)
return nil
},
},
{
Name: "shared",
Usage: "run a server that fulfills requests for files shared with `gb share`. files are served proxied from storage, not locally",
Flags: []cli.Flag{
cli.StringFlag{
Name: "label",
Usage: "storage label",
},
cli.StringFlag{
Name: "listen",
Usage: "ip and port to listen on",
Value: ":7894",
},
},
Action: func(c *cli.Context) error {
share.Shared(c.String("label"), c.String("listen"))
return nil
},
},
{
Name: "share",
Usage: "create a shareable url for a file or hash",
Flags: []cli.Flag{
cli.StringFlag{
Name: "name",
Usage: "override the filename",
Value: "",
},
},
Action: func(c *cli.Context) error {
share.CreateShareURL(c.Args().First(), c.String("name"))
return nil
},
},
}
// relay must bypass all of this, because it has no config file nor database, so we should not harass the user about setting up those things
if len(os.Args) == 3 && os.Args[1] == "relay" {
port, err := strconv.Atoi(os.Args[2])
if err != nil {
panic(err)
}
log.Println("Going to run a relay server listening on port", port)
relay.Listen(port)
return
}
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func parseTimestamp(timestamp string) (int64, error) {
if timestamp != "" {
t, err := dateparse.ParseLocal(timestamp)
if err != nil {
log.Println("Hint: make sure you are providing a year")
return 0, err
}
return t.Unix(), nil
}
return 0, nil
}