forked from swisscom/backman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
99 lines (82 loc) · 2.67 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
package main
import (
"flag"
"github.com/swisscom/backman/config"
"github.com/swisscom/backman/log"
"github.com/swisscom/backman/router"
"github.com/swisscom/backman/scheduler"
"github.com/swisscom/backman/service"
"github.com/swisscom/backman/service/util"
)
//go:generate swagger generate spec
func main() {
log.Infoln("starting up backman ...")
// init services
service.Get()
// 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("%v", r.Start())
}
func runNow() bool {
serviceToBackup := flag.String("backup", "", "service to backup now")
serviceToRestore := flag.String("restore", "", "service to restore now")
filenameToRestore := flag.String("filename", "", "filename to use for service restore")
flag.Parse()
// 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 service.Get().Services {
if s.Name == *serviceToBackup {
// running backup
log.Infof("running service backup for [%s/%s]", s.Label, s.Name)
if err := service.Get().Backup(s); err != nil {
log.Fatalf("service backup failed: %v", err)
}
found = true
// running S3 cleanup
if err := service.Get().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 service.Get().Services {
if s.Name == *serviceToRestore {
// running restore
log.Infof("running service restore for [%s/%s] with filename [%s]", s.Label, s.Name, *filenameToRestore)
if err := service.Get().Restore(s, util.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
}