/
backup-my-bucket.go
106 lines (94 loc) · 2.97 KB
/
backup-my-bucket.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
/*
Copyright 2015 ASM Clasificados de Mexico, SA de CV
This file is part of backup-my-bucket.
backup-my-bucket is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License Version 2 as published by
the Free Software Foundation.
backup-my-bucket is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with backup-my-bucket. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/SegundamanoMX/backup-my-bucket/common"
"github.com/SegundamanoMX/backup-my-bucket/gc"
"github.com/SegundamanoMX/backup-my-bucket/ls"
"github.com/SegundamanoMX/backup-my-bucket/log"
"github.com/SegundamanoMX/backup-my-bucket/restore"
"github.com/SegundamanoMX/backup-my-bucket/snapshot"
"io/ioutil"
"os"
"runtime"
)
var (
configFile *string
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
parseParams()
loadConfig()
log.Init(common.Cfg.Syslog, common.Cfg.LogLevel)
for i, param := range flag.Args() {
switch param {
case "snapshot":
snapshot.Snapshot()
return
case "list-snapshots":
ls.ListSnapshots()
return
case "restore":
snapshotName := flag.Args()[i+1:]
if len(snapshotName) == 1 {
restore.Restore(snapshotName[0])
return
} else {
log.Fatal("Too many or too few parameters for command restore: %s", snapshotName)
}
case "gc":
gc.GarbageCollect()
return
default:
log.Fatal("Found unhandled command '%s'.", param)
}
}
log.Fatal("No command was given.")
}
func parseParams() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "usage: backup-my-bucket [-help] [-config] {snapshot,list-snapshots,restore,gc}:\n")
fmt.Fprintf(os.Stderr, "commands:\n")
fmt.Fprintf(os.Stderr, " snapshot: Create a restoration point\n")
fmt.Fprintf(os.Stderr, " list-snapshots: List available restoration points\n")
fmt.Fprintf(os.Stderr, " restore: Restore master bucket at given restoration point\n")
fmt.Fprintf(os.Stderr, " gc: Garbage collect obsolete restoration points\n")
fmt.Fprintf(os.Stderr, "optional arguments:\n")
flag.PrintDefaults()
}
cwd, err := os.Getwd()
if err != nil {
fmt.Println("FATAL: Could not query current directory.")
os.Exit(1)
}
configFile = flag.String("config", cwd + "/backup-my-bucket.conf", "Path to configuration file")
flag.Parse()
}
func loadConfig() {
bytes, err := ioutil.ReadFile(*configFile)
if err != nil {
fmt.Println("ERROR in configuration file '%s'", *configFile)
fmt.Println(err)
os.Exit(1)
}
err = json.Unmarshal(bytes, &common.Cfg)
if err != nil {
fmt.Println("ERROR in configuration file '%s'", *configFile)
fmt.Println(err)
os.Exit(1)
}
}