forked from Altinity/clickhouse-backup
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
223 lines (211 loc) · 5.81 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
package main
import (
"fmt"
"log"
"os"
"github.com/AlexAkulov/clickhouse-backup/pkg/chbackup"
"github.com/urfave/cli"
)
const (
defaultConfigPath = "/etc/clickhouse-backup/config.yml"
)
var (
version = "unknown"
gitCommit = "unknown"
buildDate = "unknown"
)
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))
},
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"))
},
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 {
config := getConfig(c)
switch c.Args().Get(0) {
case "local":
return chbackup.PrintLocalBackups(*config, c.Args().Get(1))
case "remote":
return chbackup.PrintRemoteBackups(*config, c.Args().Get(1))
case "all", "":
fmt.Println("Local backups:")
if err := chbackup.PrintLocalBackups(*config, c.Args().Get(1)); err != nil {
return err
}
fmt.Println("Remote backups:")
if err := chbackup.PrintRemoteBackups(*config, c.Args().Get(1)); err != nil {
return err
}
default:
fmt.Fprintf(os.Stderr, "Unknown command '%s'\n", c.Args().Get(0))
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
}
return nil
},
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 {
config := getConfig(c)
if c.Args().Get(1) == "" {
fmt.Fprintln(os.Stderr, "Backup name must be defined")
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
}
switch c.Args().Get(0) {
case "local":
return chbackup.RemoveBackupLocal(*config, c.Args().Get(1))
case "remote":
return chbackup.RemoveBackupRemote(*config, c.Args().Get(1))
default:
fmt.Fprintf(os.Stderr, "Unknown command '%s'\n", c.Args().Get(0))
cli.ShowCommandHelpAndExit(c, c.Command.Name, 1)
}
return nil
},
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,
},
}
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
}