/
mserver.go
172 lines (144 loc) · 4 KB
/
mserver.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
/*
Copyright (C) 2015 Enrico Bianchi (enrico.bianchi@gmail.com)
Project Memento
Description A backup system
License GPL version 2 (see GPL.txt for details)
*/
package main
import (
"fmt"
"github.com/gaal/go-options/options"
"github.com/go-ini/ini"
"github.com/op/go-logging"
"os"
"path/filepath"
"server"
"strings"
)
const VERSION = "1.1.0-dev"
const SPEC = `
Memento - A backup system
Usage: mserver [OPTIONS]
--
h,help Print this help
v,version Print version
b,backup= Perform backup operation
r,restore= Perform restore operation
H,hour Hourly backup
D,day Daily backup
W,week Weekly backup
M,month Monthly backup
R,reload-dataset Reload last dataset
`
func checkStructure(repository string) {
directories := []string{
"hour",
"day",
"week",
"month",
}
if _, err1 := os.Stat(repository); err1 != nil {
for _, dir := range directories {
if os.IsNotExist(err1) {
structure := repository + string(filepath.Separator) + dir
if err2 := os.MkdirAll(structure, 0755); err2 != nil {
fmt.Fprint(os.Stderr, "Could not create structure "+structure+"\n")
os.Exit(2)
}
}
}
}
}
func setLog(level logging.Level, filename string) *logging.Logger {
var backend *logging.LogBackend
var log = logging.MustGetLogger("Memento Server")
var format logging.Formatter
if strings.ToUpper(level.String()) != "DEBUG" {
format = logging.MustStringFormatter(
"%{time:2006-01-02 15:04:05.000} %{level} - Memento - %{message}",
)
} else {
format = logging.MustStringFormatter(
"%{time:2006-01-02 15:04:05.000} %{level} - %{shortfile} - Memento - %{message}",
)
}
if filename == "" {
backend = logging.NewLogBackend(os.Stderr, "", 0)
} else {
fo, _ := os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644)
backend = logging.NewLogBackend(fo, "", 0)
}
backendLeveled := logging.AddModuleLevel(backend)
backendLeveled.SetLevel(level, "")
logging.SetBackend(backendLeveled)
logging.SetFormatter(format)
return log
}
func main() {
var cfg *ini.File
var backup bool
var reload bool
var err error
s := options.NewOptions(SPEC)
// Check if options isn't passed
if len(os.Args[1:]) <= 0 {
s.PrintUsageAndExit("No option specified")
}
opts := s.Parse(os.Args[1:])
grace := ""
// Print version and exit
if opts.GetBool("version") {
fmt.Println("Memento server " + VERSION)
os.Exit(0)
}
// Print help and exit
if opts.GetBool("help") {
s.PrintUsageAndExit("Memento server " + VERSION)
}
// Check backup or restore operation (mandatory)
if opts.GetBool("backup") && opts.GetBool("restore") {
// If backup and restore options are passed in the same session, print help and exit
s.PrintUsageAndExit("Cannot perform a backup and restore operations in the same session")
}
// Read grace (mandatory)
if opts.GetBool("hour") {
grace = "hour"
} else if opts.GetBool("day") {
grace = "day"
} else if opts.GetBool("week") {
grace = "week"
} else if opts.GetBool("month") {
grace = "month"
} else {
// If grace is not selected, print help and exit
s.PrintUsageAndExit("No grace selected")
}
if opts.GetBool("reload-dataset") {
reload = true
} else {
reload = false
}
if opts.GetBool("backup") {
backup = true
cfg, err = ini.Load([]byte{}, opts.Get("backup"))
} else if opts.GetBool("restore") {
backup = false
cfg, err = ini.Load([]byte{}, opts.Get("restore"))
}
if err != nil {
fmt.Println("Error about reading config file:", err)
os.Exit(1)
}
repository := cfg.Section("general").Key("repository").String()
checkStructure(repository)
loglevel, _ := logging.LogLevel(cfg.Section("general").Key("log_level").String())
log := setLog(loglevel, cfg.Section("general").Key("log_file").String())
log.Info("Started version " + VERSION)
log.Debug("Grace selected: " + grace)
if backup {
server.Backup(log, cfg, grace, reload)
} else {
server.Restore(log, cfg, grace)
}
log.Info("Ended version " + VERSION)
}