forked from Altinity/clickhouse-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
386 lines (348 loc) · 12.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package main
import (
"time"
"fmt"
"log"
"os"
"io"
"github.com/AlexAkulov/clickhouse-backup/pkg/chbackup"
"github.com/urfave/cli"
"net/http"
"github.com/julienschmidt/httprouter"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
defaultConfigPath = "/etc/clickhouse-backup/config.yml"
)
var (
version = "unknown"
gitCommit = "unknown"
buildDate = "unknown"
)
var (
httpRequestsSeconds = promauto.NewHistogramVec(prometheus.HistogramOpts{
Name: "clickhouse_backup_http_request_duration_seconds",
Help: "Execution time of each http request",
Buckets: []float64{1,10,30,60,120,240,300,600,1200,2400,3600,7200,14400},
},
[]string{"method", "path", "status"})
)
func attachConfig(h func(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error, c *cli.Context, name string, method string) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
start := time.Now()
err := h(c, w, r, ps)
t := time.Now()
elapsed := t.Sub(start)
fmt.Println(err)
if err != nil {
str := err.Error()
http.Error(w, str, 500)
fmt.Println(str)
httpRequestsSeconds.With(prometheus.Labels{"status": "500", "method": method, "path": name}).Observe(elapsed.Seconds())
return
}
httpRequestsSeconds.With(prometheus.Labels{"status": "200", "method": method, "path": name}).Observe(elapsed.Seconds())
}
}
func create(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var backupName = ps.ByName("backupName")
return chbackup.CreateBackup(*getConfig(c), backupName, c.String("t"), false)
}
func restore(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var backupName = ps.ByName("backupName")
return chbackup.Restore(*getConfig(c), backupName, c.String("t"), c.Bool("s"), c.Bool("d"))
}
func delete(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var serverType = ps.ByName("serverType")
var backupName = ps.ByName("backupName")
return deleteBackup(c, serverType, backupName)
}
func upload(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var backupName = ps.ByName("backupName")
return chbackup.Upload(*getConfig(c), backupName, c.String("diff-from"))
}
func download(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var backupName = ps.ByName("backupName")
fmt.Println(backupName)
return chbackup.Download(*getConfig(c), backupName)
}
func uploadWithDiff(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var backupName = ps.ByName("backupName")
var diffFrom = ps.ByName("diffFrom")
return chbackup.Upload(*getConfig(c), backupName, diffFrom)
}
func freeze(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
return chbackup.Freeze(*getConfig(c), c.String("t"))
}
func tables(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
return chbackup.PrintTables(*getConfig(c), w)
}
func clean(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
return chbackup.Clean(*getConfig(c))
}
func isClean(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
return chbackup.IsClean(*getConfig(c), w)
}
func list(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var serverType = ps.ByName("serverType")
var format = ps.ByName("format")
return listBackups(c, serverType, format, w)
}
func listAll(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error {
var serverType = ps.ByName("serverType")
return listBackups(c, serverType, "", w)
}
func metrics(h http.Handler) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
h.ServeHTTP(w, r)
}
}
func listBackups(c *cli.Context, serverType string, format string, w io.Writer) error {
config := getConfig(c)
switch serverType {
case "local":
return chbackup.PrintLocalBackups(*config, format, w)
case "remote":
return chbackup.PrintRemoteBackups(*config, format, w)
case "all", "":
fmt.Println("Local backups:")
if err := chbackup.PrintLocalBackups(*config, format, w); err != nil {
return err
}
fmt.Println("Remote backups:")
if err := chbackup.PrintRemoteBackups(*config, format, w); err != nil {
return err
}
default:
fmt.Fprintf(os.Stderr, "Unknown command '%s'\n", serverType)
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
}
return nil
}
func deleteBackup(c *cli.Context, serverType string, backupName string) error {
config := getConfig(c)
switch serverType {
case "local":
return chbackup.RemoveBackupLocal(*config, backupName)
case "remote":
return chbackup.RemoveBackupRemote(*config, backupName)
default:
fmt.Fprintf(os.Stderr, "Unknown command '%s'\n", serverType)
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
}
return nil
}
func bindGet(router *httprouter.Router, c *cli.Context, name string, h func(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error) {
// initialise the historgram to 0 count
httpRequestsSeconds.With(prometheus.Labels{"status": "200", "method": "GET", "path": name})
httpRequestsSeconds.With(prometheus.Labels{"status": "500", "method": "GET", "path": name})
router.GET(name, attachConfig(h, c, name, "GET"))
}
func bindPost(router *httprouter.Router, c *cli.Context, name string, h func(c *cli.Context, w http.ResponseWriter, r *http.Request, ps httprouter.Params) error) {
httpRequestsSeconds.With(prometheus.Labels{"status": "200", "method": "POST", "path": name})
httpRequestsSeconds.With(prometheus.Labels{"status": "500", "method": "POST", "path": name})
router.POST(name, attachConfig(h, c, name, "POST"))
}
func getConfigAndRun(c *cli.Context) error {
router := httprouter.New()
bindPost(router, c, "/create/:backupName", create)
bindPost(router, c, "/upload/:backupName", upload)
bindPost(router, c, "/download/:backupName", download)
bindPost(router, c, "/upload/:backupName/:diffFrom", uploadWithDiff)
bindPost(router, c, "/freeze", freeze)
bindGet(router, c, "/tables", tables)
bindGet(router, c, "/list/:serverType/:format", list)
bindGet(router, c, "/list/:serverType", listAll)
bindPost(router, c, "/restore/:backupName", restore)
bindPost(router, c, "/delete/:serverType/:backupName", delete)
bindPost(router, c, "/clean", clean)
bindGet(router, c, "/is-clean", isClean)
router.GET("/metrics", metrics(promhttp.Handler()))
// todo check for empty shadow dir so we can check that the last backup ran fine, and someone else is not in teh middle of making one
port := getConfig(c).General.ShardBackupPort
return http.ListenAndServe(fmt.Sprintf(":%d", port), router)
}
func main() {
log.SetOutput(os.Stdout)
cliapp := cli.NewApp()
cliapp.Name = "clickhouse-backup"
cliapp.Usage = "Tool for easy backup of ClickHouse with cloud support"
cliapp.UsageText = "clickhouse-backup <command> [-t, --tables=<db>.<table>] <backup_name>"
cliapp.Description = "Run as 'root' or 'clickhouse' user"
cliapp.Version = version
cliapp.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: defaultConfigPath,
Usage: "Config `FILE` name.",
EnvVar: "CLICKHOUSE_BACKUP_CONFIG",
},
}
cliapp.CommandNotFound = func(c *cli.Context, command string) {
fmt.Printf("Error. Unknown command: '%s'\n\n", command)
cli.ShowAppHelpAndExit(c, 1)
}
cli.VersionPrinter = func(c *cli.Context) {
fmt.Println("Version:\t", c.App.Version)
fmt.Println("Git Commit:\t", gitCommit)
fmt.Println("Build Date:\t", buildDate)
}
cliapp.Commands = []cli.Command{
{
Name: "tables",
Usage: "Print list of tables",
UsageText: "clickhouse-backup tables",
Action: func(c *cli.Context) error {
return chbackup.PrintTables(*getConfig(c), os.Stdout)
},
Flags: cliapp.Flags,
},
{
Name: "create",
Usage: "Create new backup",
UsageText: "clickhouse-backup create [-t, --tables=<db>.<table>] <backup_name>",
Description: "Create new backup",
Action: func(c *cli.Context) error {
return chbackup.CreateBackup(*getConfig(c), c.Args().First(), c.String("t"), false)
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "table, tables, t",
Hidden: false,
},
),
},
{
Name: "upload",
Usage: "Upload backup to remote storage",
UsageText: "clickhouse-backup upload [--diff-from=<backup_name>] <backup_name>",
Action: func(c *cli.Context) error {
return chbackup.Upload(*getConfig(c), c.Args().First(), c.String("diff-from"))
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "diff-from",
Hidden: false,
},
),
},
{
Name: "list",
Usage: "Print list of backups",
UsageText: "clickhouse-backup list [all|local|remote] [latest|penult]",
Action: func(c *cli.Context) error {
return listBackups(c, c.Args().Get(0), c.Args().Get(1), os.Stdout)
},
Flags: cliapp.Flags,
},
{
Name: "download",
Usage: "Download backup from remote storage",
UsageText: "clickhouse-backup download <backup_name>",
Action: func(c *cli.Context) error {
return chbackup.Download(*getConfig(c), c.Args().First())
},
Flags: cliapp.Flags,
},
{
Name: "restore",
Usage: "Create schema and restore data from backup",
UsageText: "clickhouse-backup restore [--schema] [--data] [-t, --tables=<db>.<table>] <backup_name>",
Action: func(c *cli.Context) error {
return chbackup.Restore(*getConfig(c), c.Args().First(), c.String("t"), c.Bool("s"), c.Bool("d"))
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "table, tables, t",
Hidden: false,
},
cli.BoolFlag{
Name: "schema, s",
Hidden: false,
Usage: "Restore schema only",
},
cli.BoolFlag{
Name: "data, d",
Hidden: false,
Usage: "Restore data only",
},
),
},
{
Name: "delete",
Usage: "Delete specific backup",
UsageText: "clickhouse-backup delete <local|remote> <backup_name>",
Action: func(c *cli.Context) error {
if c.Args().Get(1) == "" {
fmt.Fprintln(os.Stderr, "Backup name must be defined")
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
}
return deleteBackup(c, c.Args().Get(0), c.Args().Get(1))
},
Flags: cliapp.Flags,
},
{
Name: "default-config",
Usage: "Print default config",
Action: func(*cli.Context) {
chbackup.PrintDefaultConfig()
},
Flags: cliapp.Flags,
},
{
Name: "freeze",
Usage: "Freeze tables",
UsageText: "clickhouse-backup freeze [-t, --tables=<db>.<table>] <backup_name>",
Description: "Freeze tables",
Action: func(c *cli.Context) error {
return chbackup.Freeze(*getConfig(c), c.String("t"))
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "table, tables, t",
Hidden: false,
},
),
},
{
Name: "clean",
Usage: "Remove data in 'shadow' folder",
Action: func(c *cli.Context) error {
return chbackup.Clean(*getConfig(c))
},
Flags: cliapp.Flags,
},
{
Name: "isclean",
Usage: "Checks if the shadow dir is clean",
Action: func(c *cli.Context) error {
return chbackup.IsClean(*getConfig(c), os.Stdout)
},
Flags: cliapp.Flags,
},
{
Name: "serve",
Usage: "Starts http server for handling backup commands",
Action: func(c *cli.Context) error {
return getConfigAndRun(c)
},
Flags: cliapp.Flags,
},
}
if err := cliapp.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func getConfig(ctx *cli.Context) *chbackup.Config {
configPath := ctx.String("config")
if configPath == defaultConfigPath {
configPath = ctx.GlobalString("config")
}
config, err := chbackup.LoadConfig(configPath)
if err != nil {
log.Fatal(err)
}
return config
}