forked from gobackup/gobackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
184 lines (156 loc) · 3.92 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
package main
import (
"flag"
"fmt"
"os"
"syscall"
"github.com/sevlyar/go-daemon"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
"github.com/gobackup/gobackup/config"
"github.com/gobackup/gobackup/logger"
"github.com/gobackup/gobackup/model"
"github.com/gobackup/gobackup/scheduler"
"github.com/gobackup/gobackup/web"
)
const (
usage = "Backup your databases, files to FTP / SCP / S3 / GCS and other cloud storages."
)
var (
configFile string
version = "master"
signal = flag.String("s", "", `Send signal to the daemon:
quit — graceful shutdown
stop — fast shutdown
reload — reloading the configuration file`)
)
func buildFlags(flags []cli.Flag) []cli.Flag {
return append(flags, &cli.StringFlag{
Name: "config",
Aliases: []string{"c"},
Usage: "Special a config file",
Destination: &configFile,
})
}
func termHandler(sig os.Signal) error {
logger.Info("Received QUIT signal, exiting...")
scheduler.Stop()
os.Exit(0)
return nil
}
func reloadHandler(sig os.Signal) error {
logger.Info("Reloading config...")
err := config.Init(configFile)
if err != nil {
logger.Error(err)
}
return nil
}
func main() {
app := cli.NewApp()
app.Version = version
app.Name = "gobackup"
app.Usage = usage
daemon.AddCommand(daemon.StringFlag(signal, "quit"), syscall.SIGQUIT, termHandler)
daemon.AddCommand(daemon.StringFlag(signal, "stop"), syscall.SIGTERM, termHandler)
daemon.AddCommand(daemon.StringFlag(signal, "reload"), syscall.SIGHUP, reloadHandler)
app.Commands = []*cli.Command{
{
Name: "perform",
Flags: buildFlags([]cli.Flag{
&cli.StringSliceFlag{
Name: "model",
Aliases: []string{"m"},
Usage: "Model name that you want perform",
},
}),
Action: func(ctx *cli.Context) error {
var modelNames []string
err := initApplication()
if err != nil {
return err
}
modelNames = append(ctx.StringSlice("model"), ctx.Args().Slice()...)
return perform(modelNames)
},
},
{
Name: "start",
Usage: "Start as daemon",
Flags: buildFlags([]cli.Flag{}),
Action: func(ctx *cli.Context) error {
fmt.Println("GoBackup starting...")
args := []string{"gobackup", "run"}
if len(configFile) != 0 {
args = append(args, "--config", configFile)
}
dm := &daemon.Context{
PidFileName: config.PidFilePath,
PidFilePerm: 0644,
WorkDir: "./",
Args: args,
}
d, err := dm.Reborn()
if err != nil {
return fmt.Errorf("start failed, please check is there another instance running: %w", err)
}
if d != nil {
return nil
}
defer dm.Release() //nolint:errcheck
logger.SetLogger(config.LogFilePath)
err = initApplication()
if err != nil {
return err
}
if err := scheduler.Start(); err != nil {
return fmt.Errorf("failed to start scheduler: %w", err)
}
return nil
},
},
{
Name: "run",
Usage: "Run GoBackup",
Flags: buildFlags([]cli.Flag{}),
Action: func(ctx *cli.Context) error {
logger.SetLogger(config.LogFilePath)
err := initApplication()
if err != nil {
return err
}
if err := scheduler.Start(); err != nil {
return fmt.Errorf("failed to start scheduler: %w", err)
}
return web.StartHTTP(version)
},
},
}
if err := app.Run(os.Args); err != nil {
logger.Fatal(err.Error())
}
}
func initApplication() error {
return config.Init(configFile)
}
func perform(modelNames []string) error {
var models []*model.Model
if len(modelNames) == 0 {
// perform all
models = model.GetModels()
} else {
for _, name := range modelNames {
if m := model.GetModelByName(name); m == nil {
return fmt.Errorf("model %s not found in %s", name, viper.ConfigFileUsed())
} else {
models = append(models, m)
}
}
}
for _, m := range models {
if err := m.Perform(); err != nil {
logger.Tag(fmt.Sprintf("Model %s", m.Config.Name)).Error(err)
}
}
return nil
}