Skip to content

Commit 2bd1b59

Browse files
committed
feat: add required backupName parameter to dump/restore commands
1 parent d01276b commit 2bd1b59

File tree

5 files changed

+26
-12
lines changed

5 files changed

+26
-12
lines changed

config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ type Config struct {
1414
StorageConfig map[string]string
1515
CompressFormat string
1616
CompressLevel int
17+
BackupName string
1718
}

dumper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (d *Dumper) dumpSchema(table string) error {
124124
return err
125125
}
126126

127-
filename := fmt.Sprintf("%s/%s/%s.schema.sql", d.config.StorageConfig["path"], dbName, tableName)
127+
filename := fmt.Sprintf("%s/%s/%s/%s.schema.sql", d.config.StorageConfig["path"], d.config.BackupName, dbName, tableName)
128128
return d.storage.Upload(filename, bytes.NewReader(resp), d.config.CompressFormat, d.config.CompressLevel)
129129
}
130130

@@ -142,6 +142,6 @@ func (d *Dumper) dumpData(table string) error {
142142
}
143143
defer body.Close()
144144

145-
filename := fmt.Sprintf("%s/%s/%s.data.sql", d.config.StorageConfig["path"], dbName, tableName)
145+
filename := fmt.Sprintf("%s/%s/%s/%s.data.sql", d.config.StorageConfig["path"], d.config.BackupName, dbName, tableName)
146146
return d.storage.Upload(filename, body, d.config.CompressFormat, d.config.CompressLevel)
147147
}

e2e_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func testS3Storage(ctx context.Context, t *testing.T, clickhouseContainer testco
104104
})
105105
}
106106

107-
func runMainTestScenario(ctx context.Context, t *testing.T, clickhouseContainer testcontainers.Container, storageConfig map[string]string, testCase string) {
107+
func runMainTestScenario(ctx context.Context, t *testing.T, clickhouseContainer testcontainers.Container, storageConfig map[string]string, testCase string, backupName string) {
108108
// Clear any existing tables first
109109
require.NoError(t, clearTestTables(ctx, t, clickhouseContainer))
110110

@@ -258,6 +258,7 @@ func runMainTestScenario(ctx context.Context, t *testing.T, clickhouseContainer
258258

259259
// Create test config
260260
config := &Config{
261+
BackupName: backupName,
261262
Host: host,
262263
Port: port.Int(),
263264
User: "default",

main.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ func main() {
149149
},
150150
Commands: []*cli.Command{
151151
{
152-
Name: "dump",
153-
Usage: "Dump ClickHouse tables schema and data to remote storage",
154-
Action: RunDumper,
155-
// Add dump-specific flags here if preferred over global
152+
Name: "dump",
153+
Usage: "Dump ClickHouse tables schema and data to remote storage",
154+
Action: RunDumper,
155+
ArgsUsage: "BACKUP_NAME",
156156
},
157157
{
158-
Name: "restore",
159-
Usage: "Restore ClickHouse tables from remote storage",
160-
Action: RunRestorer,
161-
// Add restore-specific flags here if preferred over global
158+
Name: "restore",
159+
Usage: "Restore ClickHouse tables from remote storage",
160+
Action: RunRestorer,
161+
ArgsUsage: "BACKUP_NAME",
162162
},
163163
},
164164
}
@@ -174,10 +174,16 @@ func main() {
174174
}
175175

176176
func RunDumper(c *cli.Context) error {
177+
if c.Args().Len() == 0 {
178+
return fmt.Errorf("backup name is required as argument")
179+
}
180+
backupName := c.Args().First()
181+
177182
config, err := getConfig(c)
178183
if err != nil {
179184
return fmt.Errorf("configuration error: %w", err)
180185
}
186+
config.BackupName = backupName
181187
dumper, err := NewDumper(config)
182188
if err != nil {
183189
return fmt.Errorf("failed to initialize dumper: %w", err)
@@ -193,10 +199,16 @@ func RunDumper(c *cli.Context) error {
193199
}
194200

195201
func RunRestorer(c *cli.Context) error {
202+
if c.Args().Len() == 0 {
203+
return fmt.Errorf("backup name is required as argument")
204+
}
205+
backupName := c.Args().First()
206+
196207
config, err := getConfig(c)
197208
if err != nil {
198209
return fmt.Errorf("configuration error: %w", err)
199210
}
211+
config.BackupName = backupName
200212
restorer, err := NewRestorer(config)
201213
if err != nil {
202214
return fmt.Errorf("failed to initialize restorer: %w", err)

restorer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (r *Restorer) Restore() error {
7878
}()
7979

8080
// --- Restore Schema ---
81-
schemaPrefix := fmt.Sprintf("%s/", r.config.StorageConfig["path"])
81+
schemaPrefix := fmt.Sprintf("%s/%s/", r.config.StorageConfig["path"], r.config.BackupName)
8282
schemaSuffix := ".schema.sql"
8383
log.Printf("Listing schema files with prefix: %s*%s", schemaPrefix, schemaSuffix)
8484

0 commit comments

Comments
 (0)