forked from Altinity/clickhouse-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.go
672 lines (630 loc) · 17.7 KB
/
backup.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package chbackup
import (
"errors"
"fmt"
"io/ioutil"
"log"
"net/url"
"os"
"path"
"path/filepath"
"sort"
"strings"
"time"
)
const (
// BackupTimeFormat - default backup name format
BackupTimeFormat = "2006-01-02T15-04-05"
)
var (
// ErrUnknownClickhouseDataPath -
ErrUnknownClickhouseDataPath = errors.New("clickhouse data path is unknown, you can set data_path in config file")
)
func addTable(tables []Table, table Table) []Table {
for _, t := range tables {
if (t.Database == table.Database) && (t.Name == table.Name) {
return tables
}
}
return append(tables, table)
}
func addBackupTable(tables BackupTables, table BackupTable) BackupTables {
for _, t := range tables {
if (t.Database == table.Database) && (t.Name == table.Name) {
return tables
}
}
return append(tables, table)
}
func addRestoreTable(tables RestoreTables, table RestoreTable) RestoreTables {
for _, t := range tables {
if (t.Database == table.Database) && (t.Table == table.Table) {
return tables
}
}
return append(tables, table)
}
func parseTablePatternForFreeze(tables []Table, tablePattern string) []Table {
if tablePattern == "" {
return tables
}
tablePatterns := strings.Split(tablePattern, ",")
var result []Table
for _, t := range tables {
for _, pattern := range tablePatterns {
if matched, _ := filepath.Match(pattern, fmt.Sprintf("%s.%s", t.Database, t.Name)); matched {
result = addTable(result, t)
}
}
}
return result
}
func parseTablePatternForRestoreData(tables map[string]BackupTable, tablePattern string) []BackupTable {
tablePatterns := []string{"*"}
if tablePattern != "" {
tablePatterns = strings.Split(tablePattern, ",")
}
result := BackupTables{}
for _, t := range tables {
for _, pattern := range tablePatterns {
tableName := fmt.Sprintf("%s.%s", t.Database, t.Name)
if matched, _ := filepath.Match(pattern, tableName); matched {
result = addBackupTable(result, t)
}
}
}
result.Sort()
return result
}
func parseSchemaPattern(metadataPath string, tablePattern string) (RestoreTables, error) {
regularTables := RestoreTables{}
distributedTables := RestoreTables{}
viewTables := RestoreTables{}
tablePatterns := []string{"*"}
if tablePattern != "" {
tablePatterns = strings.Split(tablePattern, ",")
}
if err := filepath.Walk(metadataPath, func(filePath string, info os.FileInfo, err error) error {
if !strings.HasSuffix(filePath, ".sql") || !info.Mode().IsRegular() {
return nil
}
p := filepath.ToSlash(filePath)
p = strings.Trim(strings.TrimPrefix(strings.TrimSuffix(p, ".sql"), metadataPath), "/")
parts := strings.Split(p, "/")
if len(parts) != 2 {
return nil
}
database, _ := url.PathUnescape(parts[0])
table, _ := url.PathUnescape(parts[1])
tableName := fmt.Sprintf("%s.%s", database, table)
for _, p := range tablePatterns {
if matched, _ := filepath.Match(p, tableName); matched {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
restoreTable := RestoreTable{
Database: database,
Table: table,
Query: strings.Replace(string(data), "ATTACH", "CREATE", 1),
Path: filePath,
}
if strings.Contains(restoreTable.Query, "ENGINE = Distributed") {
distributedTables = addRestoreTable(distributedTables, restoreTable)
return nil
}
if strings.HasPrefix(restoreTable.Query, "CREATE VIEW") ||
strings.HasPrefix(restoreTable.Query, "CREATE MATERIALIZED VIEW") {
viewTables = addRestoreTable(viewTables, restoreTable)
return nil
}
regularTables = addRestoreTable(regularTables, restoreTable)
return nil
}
}
return nil
}); err != nil {
return nil, err
}
regularTables.Sort()
distributedTables.Sort()
viewTables.Sort()
result := append(regularTables, distributedTables...)
result = append(result, viewTables...)
return result, nil
}
// PrintTables - print all tables suitable for backup
func PrintTables(config Config) error {
ch := &ClickHouse{
Config: &config.ClickHouse,
}
if err := ch.Connect(); err != nil {
return fmt.Errorf("can't connect to clickouse with: %v", err)
}
defer ch.Close()
allTables, err := ch.GetTables()
if err != nil {
return fmt.Errorf("can't get tables with: %v", err)
}
for _, table := range allTables {
if table.Skip {
fmt.Printf("%s.%s\t(ignored)\n", table.Database, table.Name)
} else {
fmt.Printf("%s.%s\n", table.Database, table.Name)
}
}
return nil
}
func restoreSchema(config Config, backupName string, tablePattern string) error {
if backupName == "" {
fmt.Println("Select backup for restore:")
PrintLocalBackups(config, "all")
os.Exit(1)
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
metadataPath := path.Join(dataPath, "backup", backupName, "metadata")
info, err := os.Stat(metadataPath)
if err != nil {
return err
}
if !info.IsDir() {
return fmt.Errorf("%s is not a dir", metadataPath)
}
tablesForRestore, err := parseSchemaPattern(metadataPath, tablePattern)
if err != nil {
return err
}
if len(tablesForRestore) == 0 {
return fmt.Errorf("no have found schemas by %s in %s", tablePattern, backupName)
}
ch := &ClickHouse{
Config: &config.ClickHouse,
}
if err := ch.Connect(); err != nil {
return fmt.Errorf("can't connect to clickouse with %v", err)
}
defer ch.Close()
for _, schema := range tablesForRestore {
if err := ch.CreateDatabase(schema.Database); err != nil {
return fmt.Errorf("can't create database `%s` %v", schema.Database, err)
}
if err := ch.CreateTable(schema); err != nil {
return fmt.Errorf("can't create table `%s`.`%s` %v", schema.Database, schema.Table, err)
}
}
return nil
}
func printBackups(backupList []Backup, format string, printSize bool) error {
switch format {
case "latest", "last", "l":
if len(backupList) < 1 {
return fmt.Errorf("no backups found")
}
fmt.Println(backupList[len(backupList)-1].Name)
case "penult", "prev", "previous", "p":
if len(backupList) < 2 {
return fmt.Errorf("no penult backup is found")
}
fmt.Println(backupList[len(backupList)-2].Name)
case "all", "":
if len(backupList) == 0 {
fmt.Println("no backups found")
}
for _, backup := range backupList {
if printSize {
fmt.Printf("- '%s'\t%s\t(created at %s)\n", backup.Name, FormatBytes(backup.Size), backup.Date.Format("02-01-2006 15:04:05"))
} else {
fmt.Printf("- '%s'\t(created at %s)\n", backup.Name, backup.Date.Format("02-01-2006 15:04:05"))
}
}
default:
return fmt.Errorf("'%s' undefined", format)
}
return nil
}
// PrintLocalBackups - print all backups stored locally
func PrintLocalBackups(config Config, format string) error {
backupList, err := ListLocalBackups(config)
if err != nil && !os.IsNotExist(err) {
return err
}
return printBackups(backupList, format, false)
}
// ListLocalBackups - return slice of all backups stored locally
func ListLocalBackups(config Config) ([]Backup, error) {
dataPath := getDataPath(config)
if dataPath == "" {
return nil, ErrUnknownClickhouseDataPath
}
backupsPath := path.Join(dataPath, "backup")
d, err := os.Open(backupsPath)
if err != nil {
return nil, err
}
defer d.Close()
result := []Backup{}
names, err := d.Readdirnames(-1)
if err != nil {
return nil, err
}
for _, name := range names {
info, err := os.Stat(path.Join(backupsPath, name))
if err != nil {
continue
}
if !info.IsDir() {
continue
}
result = append(result, Backup{
Name: name,
Date: info.ModTime(),
})
}
sort.SliceStable(result, func(i, j int) bool {
return result[i].Date.Before(result[j].Date)
})
return result, nil
}
// PrintRemoteBackups - print all backups stored on remote storage
func PrintRemoteBackups(config Config, format string) error {
bd, err := NewBackupDestination(config)
if err != nil {
return err
}
err = bd.Connect()
if err != nil {
return err
}
backupList, err := bd.BackupList()
if err != nil {
return err
}
return printBackups(backupList, format, true)
}
// Freeze - freeze tables by tablePattern
func Freeze(config Config, tablePattern string) error {
ch := &ClickHouse{
Config: &config.ClickHouse,
}
if err := ch.Connect(); err != nil {
return fmt.Errorf("can't connect to clickouse with: %v", err)
}
defer ch.Close()
dataPath, err := ch.GetDataPath()
if err != nil || dataPath == "" {
return fmt.Errorf("can't get data path from clickhouse with: %v\nyou can set data_path in config file", err)
}
shadowPath := filepath.Join(dataPath, "shadow")
files, err := ioutil.ReadDir(shadowPath)
if err != nil {
if !os.IsNotExist(err) {
return fmt.Errorf("can't read %s directory: %v", shadowPath, err)
}
} else if len(files) > 0 {
return fmt.Errorf("'%s' is not empty, execute 'clean' command first", shadowPath)
}
allTables, err := ch.GetTables()
if err != nil {
return fmt.Errorf("can't get Clickhouse tables with: %v", err)
}
backupTables := parseTablePatternForFreeze(allTables, tablePattern)
if len(backupTables) == 0 {
return fmt.Errorf("there are no tables in Clickhouse, create something to freeze")
}
for _, table := range backupTables {
if table.Skip {
log.Printf("Skip `%s`.`%s`", table.Database, table.Name)
continue
}
if err := ch.FreezeTable(table); err != nil {
return err
}
}
return nil
}
// NewBackupName - return default backup name
func NewBackupName() string {
return time.Now().UTC().Format(BackupTimeFormat)
}
// CreateBackup - create new backup of all tables matched by tablePattern
// If backupName is empty string will use default backup name
func CreateBackup(config Config, backupName, tablePattern string) error {
if backupName == "" {
backupName = NewBackupName()
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
backupPath := path.Join(dataPath, "backup", backupName)
if _, err := os.Stat(backupPath); err == nil || !os.IsNotExist(err) {
return fmt.Errorf("can't create backup '%s': %v", backupPath, err)
}
if err := os.MkdirAll(backupPath, os.ModePerm); err != nil {
return fmt.Errorf("can't create '%s': %v", backupPath, err)
}
log.Printf("Create backup '%s'", backupName)
if err := Freeze(config, tablePattern); err != nil {
return err
}
log.Println("Copy metadata")
schemaList, err := parseSchemaPattern(path.Join(dataPath, "metadata"), tablePattern)
if err != nil {
return err
}
for _, schema := range schemaList {
skip := false
for _, filter := range config.ClickHouse.SkipTables {
if matched, _ := filepath.Match(filter, fmt.Sprintf("%s.%s", schema.Database, schema.Table)); matched {
skip = true
break
}
}
if skip {
continue
}
relativePath := strings.Trim(strings.TrimPrefix(schema.Path, path.Join(dataPath, "metadata")), "/")
newPath := path.Join(backupPath, "metadata", relativePath)
if err := copyFile(schema.Path, newPath); err != nil {
return fmt.Errorf("can't backup metadata with %v", err)
}
}
log.Println(" Done.")
log.Println("Move shadow")
backupShadowDir := path.Join(backupPath, "shadow")
if err := os.MkdirAll(backupShadowDir, os.ModePerm); err != nil {
return err
}
shadowDir := path.Join(dataPath, "shadow")
if err := moveShadow(shadowDir, backupShadowDir); err != nil {
return err
}
if err := RemoveOldBackupsLocal(config); err != nil {
return err
}
log.Println(" Done.")
return nil
}
// Restore - restore tables matched by tablePattern from backupName
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
}
// RestoreData - restore data for tables matched by tablePattern from backupName
func RestoreData(config Config, backupName string, tablePattern string) error {
if backupName == "" {
fmt.Println("Select backup for restore:")
PrintLocalBackups(config, "all")
os.Exit(1)
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
ch := &ClickHouse{
Config: &config.ClickHouse,
}
if err := ch.Connect(); err != nil {
return fmt.Errorf("can't connect to clickouse with: %v", err)
}
defer ch.Close()
allBackupTables, err := ch.GetBackupTables(backupName)
if err != nil {
return err
}
restoreTables := parseTablePatternForRestoreData(allBackupTables, tablePattern)
chTables, err := ch.GetTables()
if err != nil {
return err
}
if len(restoreTables) == 0 {
return fmt.Errorf("backup doesn't have tables to restore")
}
missingTables := []string{}
for _, restoreTable := range restoreTables {
found := false
for _, chTable := range chTables {
if (restoreTable.Database == chTable.Database) && (restoreTable.Name == chTable.Name) {
found = true
break
}
}
if !found {
missingTables = append(missingTables, fmt.Sprintf("'%s.%s'", restoreTable.Database, restoreTable.Name))
}
}
if len(missingTables) > 0 {
return fmt.Errorf("%s is not created. Restore schema first or create missing tables manually", strings.Join(missingTables, ", "))
}
for _, table := range restoreTables {
if err := ch.CopyData(table); err != nil {
return fmt.Errorf("can't restore `%s`.`%s` with %v", table.Database, table.Name, err)
}
if err := ch.AttachPatritions(table); err != nil {
return fmt.Errorf("can't attach partitions for table '%s.%s' with %v", table.Database, table.Name, err)
}
}
return nil
}
func getDataPath(config Config) string {
if config.ClickHouse.DataPath != "" {
return config.ClickHouse.DataPath
}
ch := &ClickHouse{Config: &config.ClickHouse}
if err := ch.Connect(); err != nil {
return ""
}
defer ch.Close()
dataPath, err := ch.GetDataPath()
if err != nil {
return ""
}
return dataPath
}
func GetLocalBackup(config Config, backupName string) error {
if backupName == "" {
return fmt.Errorf("backup name is required")
}
backupList, err := ListLocalBackups(config)
if err != nil {
return err
}
for _, backup := range backupList {
if backup.Name == backupName {
return nil
}
}
return fmt.Errorf("backup '%s' not found", backupName)
}
func Upload(config Config, backupName string, diffFrom string) error {
if backupName == "" {
fmt.Println("Select backup for upload:")
PrintLocalBackups(config, "all")
os.Exit(1)
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
bd, err := NewBackupDestination(config)
if err != nil {
return err
}
err = bd.Connect()
if err != nil {
return fmt.Errorf("can't connect to %s with : %v", bd.Kind(), err)
}
if err := GetLocalBackup(config, backupName); err != nil {
return fmt.Errorf("can't upload with %s", err)
}
backupPath := path.Join(dataPath, "backup", backupName)
log.Printf("Upload backup '%s'", backupName)
diffFromPath := ""
if diffFrom != "" {
diffFromPath = path.Join(dataPath, "backup", diffFrom)
}
if err := bd.CompressedStreamUpload(backupPath, backupName, diffFromPath); err != nil {
return fmt.Errorf("can't upload with %v", err)
}
if err := bd.RemoveOldBackups(bd.BackupsToKeep()); err != nil {
return fmt.Errorf("can't remove old backups: %v", err)
}
log.Println(" Done.")
return nil
}
func Download(config Config, backupName string) error {
if backupName == "" {
fmt.Println("Select backup for download:")
PrintRemoteBackups(config, "all")
os.Exit(1)
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
bd, err := NewBackupDestination(config)
if err != nil {
return err
}
err = bd.Connect()
if err != nil {
return err
}
err = bd.CompressedStreamDownload(backupName, path.Join(dataPath, "backup", backupName))
if err != nil {
return err
}
log.Println(" Done.")
return nil
}
// Clean - removed all data in shadow folder
func Clean(config Config) error {
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
shadowDir := path.Join(dataPath, "shadow")
if _, err := os.Stat(shadowDir); os.IsNotExist(err) {
log.Printf("%s directory does not exist, nothing to do", shadowDir)
return nil
}
log.Printf("Clean %s", shadowDir)
if err := cleanDir(shadowDir); err != nil {
return fmt.Errorf("can't remove contents from directory %v: %v", shadowDir, err)
}
return nil
}
//
func RemoveOldBackupsLocal(config Config) error {
if config.General.BackupsToKeepLocal < 1 {
return nil
}
backupList, err := ListLocalBackups(config)
if err != nil {
return err
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
backupsToDelete := GetBackupsToDelete(backupList, config.General.BackupsToKeepLocal)
for _, backup := range backupsToDelete {
backupPath := path.Join(dataPath, "backup", backup.Name)
os.RemoveAll(backupPath)
}
return nil
}
func RemoveBackupLocal(config Config, backupName string) error {
backupList, err := ListLocalBackups(config)
if err != nil {
return err
}
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
for _, backup := range backupList {
if backup.Name == backupName {
return os.RemoveAll(path.Join(dataPath, "backup", backupName))
}
}
return fmt.Errorf("backup '%s' not found", backupName)
}
func RemoveBackupRemote(config Config, backupName string) error {
dataPath := getDataPath(config)
if dataPath == "" {
return ErrUnknownClickhouseDataPath
}
bd, err := NewBackupDestination(config)
if err != nil {
return err
}
err = bd.Connect()
if err != nil {
return fmt.Errorf("can't connect to remote storage with: %v", err)
}
backupList, err := bd.BackupList()
if err != nil {
return err
}
for _, backup := range backupList {
if backup.Name == backupName {
return bd.RemoveBackup(backupName)
}
}
return fmt.Errorf("backup '%s' not found on remote storage", backupName)
}