Skip to content

Commit

Permalink
'restore-schema' and 'restore-data' united to one command
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAkulov committed Oct 27, 2019
1 parent 493a512 commit 0655043
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 97 deletions.
11 changes: 5 additions & 6 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@ DESCRIPTION:
COMMANDS:
tables Print list of tables
list Print list of backups
delete Delete specific backup
freeze Freeze tables
create Create new backup
upload Upload backup to remote storage
list Print list of backups
download Download backup from remote storage
restore-schema Create databases and tables from backup metadata
restore-data Copy data to 'detached' folder and execute ATTACH
restore Create schema and restore data from backup
delete Delete specific backup
default-config Print default config
freeze Freeze tables
clean Remove data in 'shadow' folder
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--config FILE, -c FILE Config FILE name. (default: "/etc/clickhouse-backup/config.yml")
--config FILE, -c FILE Config FILE name. (default: "/etc/clickhouse-backup/config.yml") [$CLICKHOUSE_BACKUP_CONFIG]
--help, -h show help
--version, -v print the version
```
Expand Down
22 changes: 8 additions & 14 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,8 @@ func testRestoreLegacyBackupFormat(t *testing.T) {
fmt.Println("Download")
r.NoError(dockerExec("clickhouse-backup", "download", "old_format"))

fmt.Println("Restore schema")
r.NoError(dockerExec("clickhouse-backup", "restore-schema", "-t", dbName+".*", "old_format"))

fmt.Println("Restore data")
r.NoError(dockerExec("clickhouse-backup", "restore-data", "-t", dbName+".*", "old_format"))
fmt.Println("Restore")
r.NoError(dockerExec("clickhouse-backup", "restore", "-t", dbName+".*", "old_format"))

fmt.Println("Check data")
for i := range testData {
Expand Down Expand Up @@ -257,10 +254,10 @@ func testCommon(t *testing.T) {
r.NoError(dockerExec("clickhouse-backup", "download", "test_backup"))

fmt.Println("Restore schema")
r.NoError(dockerExec("clickhouse-backup", "restore-schema", "test_backup"))
r.NoError(dockerExec("clickhouse-backup", "restore", "--schema", "test_backup"))

fmt.Println("Restore data")
r.NoError(dockerExec("clickhouse-backup", "restore-data", "test_backup"))
r.NoError(dockerExec("clickhouse-backup", "restore", "--data", "test_backup"))

fmt.Println("Check data")
for i := range testData {
Expand All @@ -278,11 +275,8 @@ func testCommon(t *testing.T) {
fmt.Println("Download increment")
r.NoError(dockerExec("clickhouse-backup", "download", "increment"))

fmt.Println("Restore schema")
r.NoError(dockerExec("clickhouse-backup", "restore-schema", "increment"))

fmt.Println("Restore data")
r.NoError(dockerExec("clickhouse-backup", "restore-data", "increment"))
fmt.Println("Restore")
r.NoError(dockerExec("clickhouse-backup", "restore", "--schema", "--data", "increment"))

fmt.Println("Check increment data")
for i := range testData {
Expand Down Expand Up @@ -359,7 +353,7 @@ func (ch *ClickHouse) checkData(t *testing.T, data TestDataStuct) error {

func dockerExec(cmd ...string) error {
out, err := dockerExecOut(cmd...)
fmt.Println(string(out))
fmt.Print(string(out))
return err
}

Expand All @@ -374,7 +368,7 @@ func dockerExecOut(cmd ...string) (string, error) {

func dockerCP(src, dst string) error {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
dcmd := []string{"cp", src, "clickhouse:"+dst}
dcmd := []string{"cp", src, "clickhouse:" + dst}
out, err := exec.CommandContext(ctx, "docker", dcmd...).CombinedOutput()
fmt.Println(string(out))
cancel()
Expand Down
166 changes: 89 additions & 77 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ func main() {

cliapp.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
Value: defaultConfigPath,
Usage: "Config `FILE` name.",
Name: "config, c",
Value: defaultConfigPath,
Usage: "Config `FILE` name.",
EnvVar: "CLICKHOUSE_BACKUP_CONFIG",
},
}
Expand All @@ -68,6 +68,35 @@ func main() {
},
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 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 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",
Expand Down Expand Up @@ -96,6 +125,39 @@ func main() {
},
Flags: cliapp.Flags,
},
{
Name: "download",
Usage: "Download backup from remote storage",
UsageText: "clickhouse-backup download <backup_name>",
Action: func(c *cli.Context) error {
return 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 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",
Expand All @@ -119,6 +181,14 @@ func main() {
},
Flags: cliapp.Flags,
},
{
Name: "default-config",
Usage: "Print default config",
Action: func(*cli.Context) {
PrintDefaultConfig()
},
Flags: cliapp.Flags,
},
{
Name: "freeze",
Usage: "Freeze tables",
Expand All @@ -134,80 +204,6 @@ func main() {
},
),
},
{
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 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 upload(*getConfig(c), c.Args().First(), c.String("diff-from"))
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "diff-from",
Hidden: false,
},
),
},
{
Name: "download",
Usage: "Download backup from remote storage",
UsageText: "clickhouse-backup download <backup_name>",
Action: func(c *cli.Context) error {
return download(*getConfig(c), c.Args().First())
},
Flags: cliapp.Flags,
},
{
Name: "restore-schema",
Usage: "Create databases and tables from backup metadata",
UsageText: "clickhouse-backup restore-schema [-t, --tables=<db>.<table>] <backup_name>",
Action: func(c *cli.Context) error {
return restoreSchema(*getConfig(c), c.Args().First(), c.String("t"))
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "table, tables, t",
Hidden: false,
},
),
},
{
Name: "restore-data",
Usage: "Copy data to 'detached' folder and execute ATTACH",
UsageText: "clickhouse-backup restore-data [-t, --tables=<db>.<table>] <backup_name>",
Action: func(c *cli.Context) error {
return restoreData(*getConfig(c), c.Args().First(), c.String("t"))
},
Flags: append(cliapp.Flags,
cli.StringFlag{
Name: "table, tables, t",
Hidden: false,
},
),
},
{
Name: "default-config",
Usage: "Print default config",
Action: func(*cli.Context) {
PrintDefaultConfig()
},
Flags: cliapp.Flags,
},
{
Name: "clean",
Usage: "Remove data in 'shadow' folder",
Expand Down Expand Up @@ -598,6 +594,22 @@ func createBackup(config Config, backupName, tablePattern string) error {
return nil
}

func restore(config Config, backupName string, tablePattern string, schemaOnly bool, dataOnly bool) error {
if schemaOnly || (schemaOnly == dataOnly) {
err := restoreSchema(config, backupName, tablePattern)
if err != nil {
return err
}
}
if dataOnly || (schemaOnly == dataOnly) {
err := restoreData(config, backupName, tablePattern)
if err != nil {
return err
}
}
return nil
}

func restoreData(config Config, backupName string, tablePattern string) error {
if backupName == "" {
fmt.Println("Select backup for restore:")
Expand Down

0 comments on commit 0655043

Please sign in to comment.