forked from Altinity/clickhouse-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
192 lines (180 loc) · 4.21 KB
/
utils.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
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
"github.com/mholt/archiver"
)
type Backup struct {
Name string
Date time.Time
}
func cleanDir(dir string) error {
d, err := os.Open(dir)
if err != nil {
return err
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return err
}
for _, name := range names {
err = os.RemoveAll(filepath.Join(dir, name))
if err != nil {
return err
}
}
return nil
}
func isClickhouseShadow(path string) bool {
d, err := os.Open(path)
if err != nil {
return false
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return false
}
for _, name := range names {
if name == "increment.txt" {
continue
}
if _, err := strconv.Atoi(name); err != nil {
return false
}
}
return true
}
func moveShadow(shadowPath, backupPath string) error {
if err := filepath.Walk(shadowPath, func(filePath string, info os.FileInfo, err error) error {
relativePath := strings.Trim(strings.TrimPrefix(filePath, shadowPath), "/")
pathParts := strings.SplitN(relativePath, "/", 3)
if len(pathParts) != 3 {
return nil
}
dstFilePath := filepath.Join(backupPath, pathParts[2])
if info.IsDir() {
return os.MkdirAll(dstFilePath, os.ModePerm)
}
if !info.Mode().IsRegular() {
log.Printf("'%s' is not a regular file, skipping", filePath)
return nil
}
return os.Rename(filePath, dstFilePath)
}); err != nil {
return err
}
return cleanDir(shadowPath)
}
func copyPath(src, dst string, dryRun bool) error {
if _, err := os.Stat(src); err != nil {
return err
}
return filepath.Walk(src, func(filePath string, info os.FileInfo, err error) error {
filePath = filepath.ToSlash(filePath) // fix Windows slashes
filename := strings.Trim(strings.TrimPrefix(filePath, src), "/")
dstFilePath := filepath.Join(dst, filename)
if dryRun {
if info.IsDir() {
log.Printf("make path %s", dstFilePath)
return nil
}
if !info.Mode().IsRegular() {
log.Printf("'%s' is not a regular file, skipping", filePath)
return nil
}
log.Printf("copy %s -> %s", filePath, dstFilePath)
return nil
}
if info.IsDir() {
return os.MkdirAll(dstFilePath, os.ModePerm)
}
if !info.Mode().IsRegular() {
log.Printf("'%s' is not a regular file, skipping", filePath)
return nil
}
return copyFile(filePath, dstFilePath)
})
}
func copyFile(srcFile string, dstFile string) error {
src, err := os.Open(srcFile)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(dstFile)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
return err
}
func GetBackupsToDelete(backups []Backup, keep int) []Backup {
if len(backups) > keep {
sort.SliceStable(backups, func(i, j int) bool {
return backups[i].Date.After(backups[j].Date)
})
return backups[keep:]
}
return []Backup{}
}
func getArchiveWriter(format string, level int) (archiver.Writer, error) {
switch format {
case "tar":
return &archiver.Tar{}, nil
case "lz4":
return &archiver.TarLz4{CompressionLevel: level, Tar: archiver.NewTar()}, nil
case "bzip2":
return &archiver.TarBz2{CompressionLevel: level, Tar: archiver.NewTar()}, nil
case "gzip":
return &archiver.TarGz{CompressionLevel: level, Tar: archiver.NewTar()}, nil
case "sz":
return &archiver.TarSz{Tar: archiver.NewTar()}, nil
case "xz":
return &archiver.TarXz{Tar: archiver.NewTar()}, nil
}
return nil, fmt.Errorf("wrong compression_format, supported: 'lz4', 'bzip2', 'gzip', 'sz', 'xz'")
}
func getExtension(format string) string {
switch format {
case "tar":
return "tar"
case "lz4":
return "tar.lz4"
case "bzip2":
return "tar.bz2"
case "gzip":
return "tar.gz"
case "sz":
return "tar.sz"
case "xz":
return "tar.xz"
}
return ""
}
func getArchiveReader(format string) (archiver.Reader, error) {
switch format {
case "tar":
return archiver.NewTar(), nil
case "lz4":
return archiver.NewTarLz4(), nil
case "bzip2":
return archiver.NewTarBz2(), nil
case "gzip":
return archiver.NewTarGz(), nil
case "sz":
return archiver.NewTarSz(), nil
case "xz":
return archiver.NewTarXz(), nil
}
return nil, fmt.Errorf("wrong compression_format, supported: 'tar', 'lz4', 'bzip2', 'gzip', 'sz', 'xz'")
}