Skip to content

Commit a65041a

Browse files
committed
add support \ and / special characters in table name and database name, fix #1091
Signed-off-by: Slach <bloodjazman@gmail.com>
1 parent 05613b4 commit a65041a

File tree

5 files changed

+26
-9
lines changed

5 files changed

+26
-9
lines changed

pkg/backup/list.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,9 @@ func (b *Backuper) GetTablesRemote(ctx context.Context, backupName string, table
497497

498498
for _, remoteBackup := range backupList {
499499
if remoteBackup.BackupName == backupName {
500+
// https://github.com/Altinity/clickhouse-backup/issues/1091
501+
replacer := strings.NewReplacer("/", "_", `\`, "_")
502+
500503
for _, t := range remoteBackup.Tables {
501504
isInformationSchema := IsInformationSchema(t.Database)
502505
tableName := fmt.Sprintf("%s.%s", t.Database, t.Table)
@@ -508,7 +511,8 @@ func (b *Backuper) GetTablesRemote(ctx context.Context, backupName string, table
508511
matched = true
509512
break
510513
}
511-
if matched, _ = filepath.Match(strings.Trim(pattern, " \t\r\n"), tableName); matched {
514+
// https://github.com/Altinity/clickhouse-backup/issues/1091
515+
if matched, _ = filepath.Match(replacer.Replace(strings.Trim(pattern, " \t\r\n")), replacer.Replace(tableName)); matched {
512516
break
513517
}
514518
}

pkg/backup/restore.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,7 @@ func (b *Backuper) Restore(backupName, tablePattern string, databaseMapping, tab
209209
var tablesForRestore ListOfTables
210210
var partitionsNames map[metadata.TableTitle][]string
211211
if tablePattern == "" {
212-
// https://github.com/Altinity/clickhouse-backup/issues/1091
213-
tablePattern = "*,*/*"
212+
tablePattern = "*"
214213
}
215214
metadataPath := path.Join(b.DefaultDataPath, "backup", backupName, "metadata")
216215
if b.isEmbedded && b.cfg.ClickHouse.EmbeddedBackupDisk != "" {

pkg/backup/table_pattern.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ func (b *Backuper) getTableListByPatternLocal(ctx context.Context, metadataPath
6161
return nil, nil, err
6262
}
6363
}
64+
// https://github.com/Altinity/clickhouse-backup/issues/1091
65+
replacer := strings.NewReplacer(`/`, "_", `\`, "_")
66+
6467
if err := filepath.Walk(metadataPath, func(filePath string, info os.FileInfo, err error) error {
6568
if err != nil {
6669
return err
@@ -84,7 +87,7 @@ func (b *Backuper) getTableListByPatternLocal(ctx context.Context, metadataPath
8487
}
8588
for _, pattern := range tablePatterns {
8689
// https://github.com/Altinity/clickhouse-backup/issues/1091
87-
if matched, _ := filepath.Match(strings.Trim(pattern, " \t\r\n"), strings.Replace(tableName, "/", "_", -1)); !matched {
90+
if matched, _ := filepath.Match(replacer.Replace(strings.Trim(pattern, " \t\r\n")), replacer.Replace(tableName)); !matched {
8891
continue
8992
}
9093
data, err := os.ReadFile(filePath)
@@ -233,6 +236,9 @@ func prepareTableMetadataFromSQL(data []byte, metadataPath string, names []strin
233236

234237
func (b *Backuper) enrichTablePatternsByInnerDependencies(metadataPath string, tablePatterns []string) ([]string, error) {
235238
innerTablePatterns := make([]string, 0)
239+
// https://github.com/Altinity/clickhouse-backup/issues/1091
240+
replacer := strings.NewReplacer("/", "_", `\`, "_")
241+
236242
if err := filepath.Walk(metadataPath, func(filePath string, info os.FileInfo, err error) error {
237243
if err != nil {
238244
return err
@@ -246,7 +252,7 @@ func (b *Backuper) enrichTablePatternsByInnerDependencies(metadataPath string, t
246252
}
247253
for _, pattern := range tablePatterns {
248254
// https://github.com/Altinity/clickhouse-backup/issues/1091
249-
if matched, _ := filepath.Match(strings.Trim(pattern, " \t\r\n"), strings.Replace(tableName, "/", "_", -1)); !matched {
255+
if matched, _ := filepath.Match(replacer.Replace(strings.Trim(pattern, " \t\r\n")), replacer.Replace(tableName)); !matched {
250256
continue
251257
}
252258
data, err := os.ReadFile(filePath)
@@ -493,6 +499,8 @@ func getTableListByPatternRemote(ctx context.Context, b *Backuper, remoteBackupM
493499
tablePatterns = strings.Split(tablePattern, ",")
494500
}
495501
metadataPath := path.Join(remoteBackupMetadata.BackupName, "metadata")
502+
// https://github.com/Altinity/clickhouse-backup/issues/1091
503+
replacer := strings.NewReplacer(`/`, "_", `\`, "_")
496504
for _, t := range remoteBackupMetadata.Tables {
497505
if IsInformationSchema(t.Database) {
498506
continue
@@ -508,7 +516,7 @@ func getTableListByPatternRemote(ctx context.Context, b *Backuper, remoteBackupM
508516
return nil, ctx.Err()
509517
default:
510518
// https://github.com/Altinity/clickhouse-backup/issues/1091
511-
if matched, _ := filepath.Match(strings.Trim(pattern, " \t\r\n"), strings.Replace(tableName, "/", "_", -1)); !matched {
519+
if matched, _ := filepath.Match(replacer.Replace(strings.Trim(pattern, " \t\r\n")), replacer.Replace(tableName)); !matched {
512520
continue
513521
}
514522
tmReader, err := b.dst.GetFileReader(ctx, path.Join(metadataPath, common.TablePathEncode(t.Database), fmt.Sprintf("%s.json", common.TablePathEncode(t.Table))))
@@ -580,11 +588,14 @@ func parseTablePatternForDownload(tables []metadata.TableTitle, tablePattern str
580588
tablePatterns = strings.Split(tablePattern, ",")
581589
}
582590
var result []metadata.TableTitle
591+
// https://github.com/Altinity/clickhouse-backup/issues/1091
592+
replacer := strings.NewReplacer("/", "_", `\`, "_")
593+
583594
for _, t := range tables {
584595
for _, pattern := range tablePatterns {
585596
tableName := fmt.Sprintf("%s.%s", t.Database, t.Table)
586597
// https://github.com/Altinity/clickhouse-backup/issues/1091
587-
if matched, _ := filepath.Match(strings.Trim(pattern, " \t\r\n"), strings.Replace(tableName, "/", "_", -1)); matched {
598+
if matched, _ := filepath.Match(replacer.Replace(strings.Trim(pattern, " \t\r\n")), replacer.Replace(tableName)); matched {
588599
result = append(result, t)
589600
break
590601
}

pkg/clickhouse/clickhouse.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ func (ch *ClickHouse) GetTables(ctx context.Context, tablePattern string) ([]Tab
375375
return nil, err
376376
}
377377
for i := range tables {
378+
// https://github.com/Altinity/clickhouse-backup/issues/1091
378379
tables[i].CreateTableQuery = strings.ReplaceAll(tables[i].CreateTableQuery, `\\`, `\`)
379380
}
380381
metadataPath, err := ch.getMetadataPath(ctx)
@@ -486,7 +487,8 @@ func (ch *ClickHouse) prepareGetTablesSQL(tablePattern string, skipDatabases, sk
486487

487488
allTablesSQL += " FROM system.tables WHERE is_temporary = 0"
488489
if tablePattern != "" {
489-
replacer := strings.NewReplacer(".", "\\.", "$", ".", ",", "$|^", "*", ".*", "?", ".", " ", "", "`", "", `"`, "", "-", "\\-")
490+
// https://github.com/Altinity/clickhouse-backup/issues/1091
491+
replacer := strings.NewReplacer(`\`, `\\\`, ".", "\\.", "$", ".", ",", "$|^", "*", ".*", "?", ".", " ", "", "`", "", `"`, "", "-", "\\-")
490492
allTablesSQL += fmt.Sprintf(" AND match(concat(database,'.',name),'^%s$') ", replacer.Replace(tablePattern))
491493
}
492494
if len(skipDatabases) > 0 {

pkg/partition/partition.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ func ConvertPartitionsToIdsMapAndNamesList(ctx context.Context, ch *clickhouse.C
250250

251251
func addItemToIdMapAndNameListIfNotExists(partitionId, partitionName, database, table string, partitionsIdMap map[metadata.TableTitle]common.EmptyMap, partitionsNameList map[metadata.TableTitle][]string, tablePattern string) {
252252
// https://github.com/Altinity/clickhouse-backup/issues/1091
253-
if matched, err := filepath.Match(tablePattern, database+"."+table); err == nil && matched || tablePattern == "*" {
253+
replacer := strings.NewReplacer(`\`, "_", `/`, "_")
254+
if matched, err := filepath.Match(replacer.Replace(tablePattern), replacer.Replace(database+"."+table)); err == nil && matched || tablePattern == "*" {
254255
if partitionId != "" {
255256
partitionsIdMap[metadata.TableTitle{
256257
Database: database, Table: table,

0 commit comments

Comments
 (0)