forked from swisscom/backman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
124 lines (103 loc) · 3.19 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
package main
import (
"flag"
"time"
"github.com/swisscom/backman/config"
"github.com/swisscom/backman/log"
"github.com/swisscom/backman/notifications"
"github.com/swisscom/backman/router"
"github.com/swisscom/backman/scheduler"
"github.com/swisscom/backman/service"
)
var (
configFile string
serviceToBackup string
serviceToRestore string
filenameToRestore string
)
func init() {
flag.StringVar(&configFile, "config", "config.json", "configuration file for backman")
flag.StringVar(&serviceToBackup, "backup", "", "service to backup now")
flag.StringVar(&serviceToRestore, "restore", "", "service to restore now")
flag.StringVar(&filenameToRestore, "filename", "", "filename to use for service restore")
flag.Parse()
// specify config file to load
if len(configFile) > 0 {
config.SetConfigFile(configFile)
}
// initialize config
config.Init()
// setup logger
log.Init()
// setup service instances
service.Init()
// init notifications manager
notifications.Init()
}
//go:generate swagger generate spec
func main() {
time.Sleep(1 * time.Second) // wait for init
log.Infoln("starting up backman ...")
// check if an immediate backup/restore should run, in non-background mode. otherwise continue and start scheduler
if runNow() {
return
}
// schedule backups
scheduler.RegisterBackups()
// serve API & UI
r := router.New()
log.Fatalf("failed to start HTTP router: %v", r.Start())
}
func runNow() bool {
// check if backup should run
if len(serviceToBackup) > 0 {
log.Infof("backup flag provided with value [%s], running backup now", serviceToBackup)
// setting config to non-background mode, to avoid background goroutines during backups
config.Get().Foreground = true
// find service to backup
var found bool
for _, s := range config.Get().Services {
if s.Name == serviceToBackup {
// running backup
log.Infof("running service backup for [%s/%s]", s.Binding.Type, s.Name)
if err := service.CreateBackup(s); err != nil {
log.Fatalf("service backup failed: %v", err)
}
found = true
// running S3 cleanup
if err := service.RetentionCleanup(s); err != nil {
log.Errorf("could not cleanup S3 storage for service [%s]: %v", s.Name, err)
}
break
}
}
if !found {
log.Fatalf("could not find any service named [%s]", serviceToBackup)
}
log.Infoln("backup successfully completed")
return true
}
// check if restore should run
if len(serviceToRestore) > 0 && len(filenameToRestore) > 0 {
log.Infof("restore flags provided with values [%s/%s], running restore now", serviceToRestore, filenameToRestore)
// find service to restore
var found bool
for _, s := range config.Get().Services {
if s.Name == serviceToRestore {
// running restore
log.Infof("running service restore for [%s/%s] with filename [%s]", s.Binding.Type, s.Name, filenameToRestore)
if err := service.RestoreBackup(s, config.Service{}, filenameToRestore); err != nil {
log.Fatalf("service restore failed: %v", err)
}
found = true
break
}
}
if !found {
log.Fatalf("could not find any service named [%s]", serviceToRestore)
}
log.Infoln("restore successfully completed")
return true
}
return false
}