-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup-rsync.go
307 lines (272 loc) · 7.52 KB
/
backup-rsync.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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"io/ioutil"
"sort"
"path"
"time"
"regexp"
"os/signal"
"syscall"
"path/filepath"
log "github.com/Sirupsen/logrus"
"gopkg.in/yaml.v2"
"github.com/nightlyone/lockfile"
)
const (
logFile string = "/var/log/backup-rsync.log"
lockFile string = "backup-rsync.lock"
rsyncLogFilePattern string = "/var/log/backup-rsync"
configFile string = "/etc/backup-rsync.yml"
dateLayout string = "2006-01-02"
defaultConcurrentRsync int = 3
defaultRetentionDays int = 7
)
type Command struct {
cmd string
args []string
}
type Config struct {
Root_dir interface{}
Concurrent_rsync interface{} `yaml:",omitempty"`
Retention_days interface{} `yaml:",omitempty"`
Hosts []struct {
Name string
Limit_concurrent_rsync interface{} `yaml:",omitempty"`
Retention_days interface{} `yaml:",omitempty"`
Login_user interface{} `yaml:",omitempty"`
Login_port interface{} `yaml:",omitempty"`
Dirs []struct {
Path string
Retention_days interface{} `yaml:",omitempty"`
Bandwidth_limit interface{} `yaml:",omitempty"`
}
}
}
type Path struct {
host string
path string
retentionDays int
concurrentRsyncLimit int
bandwidthLimit interface{}
loginUser interface{}
loginPort interface{}
}
func worker(id int, jobs <-chan Command, results chan<- error) {
for j := range jobs {
cmd := fmt.Sprintf("%s %s", j.cmd, strings.Join(j.args, " "))
log.Infof("Worker ID: %d; execute command: %s", id, cmd)
if err := exec.Command(j.cmd, j.args...).Run(); err == nil {
log.Infof("Worker ID: %d; command succesful: %s", id, cmd)
results <- nil
} else {
log.Errorf("Worker ID: %d; command fail: %s: %s", id, cmd, err)
results <- err
}
}
}
func parseYaml(file string) Config {
source, err := ioutil.ReadFile(file)
if err != nil {
log.Fatal(err)
}
var c Config
err = yaml.Unmarshal([]byte(source), &c)
if err != nil {
log.Fatal(err)
}
return c
}
func getPaths(config Config) []Path {
var paths []Path
for _, host := range config.Hosts {
for _, dir := range host.Dirs {
if dir.Path == "" {
continue
}
var p Path
p.host = host.Name
p.path = dir.Path
p.bandwidthLimit = dir.Bandwidth_limit
p.loginUser = host.Login_user
p.loginPort = host.Login_port
if dir.Retention_days != nil {
p.retentionDays = dir.Retention_days.(int)
} else if host.Retention_days != nil {
p.retentionDays = host.Retention_days.(int)
} else {
p.retentionDays = config.Retention_days.(int)
}
if host.Limit_concurrent_rsync != nil {
if host.Limit_concurrent_rsync.(int) > config.Concurrent_rsync.(int) {
p.concurrentRsyncLimit = config.Concurrent_rsync.(int)
} else {
p.concurrentRsyncLimit = host.Limit_concurrent_rsync.(int)
}
} else {
p.concurrentRsyncLimit = config.Concurrent_rsync.(int)
}
paths = append(paths, p)
}
}
return paths
}
// change paths order to avoid as much as possible multiple rsync to same host
// separate group of paths to the same host
func preparePathsOrder(paths []Path) []Path {
m := make(map[string][]Path)
for _, p := range paths {
m[p.host] = append(m[p.host], p)
}
// prepare sorted keys to iterate by sorted map m
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
var pathsSorted []Path
for i := 0; i < len(paths); i++ {
// iterate over sorted map keys
for _, host := range keys {
if len(m[host]) > 0 {
pathsSorted = append(pathsSorted, m[host][0])
m[host] = append(m[host][:0], m[host][1:]...)
}
}
}
return pathsSorted
}
func prepareCommands(paths []Path, config Config) []Command {
cmds := make([]Command, 0)
for _, p := range paths {
var s Command
s.cmd = "rsync"
s.args = []string{
"-avHAX",
"--delete",
"--backup",
"--backup-dir=" + config.Root_dir.(string) + "/" + p.host + "/" + path.Base(p.path) + "/" + fmt.Sprint(time.Now().Format(dateLayout)),
"--log-file=" + rsyncLogFilePattern + "." + p.host + ".log",
}
if p.bandwidthLimit != nil {
s.args = append(s.args, fmt.Sprintf("--bwlimit=%d", p.bandwidthLimit.(int)))
}
if p.loginPort != nil {
s.args = append(s.args, []string{"-e", fmt.Sprintf("ssh -p %d", p.loginPort)}...)
}
if p.loginUser != nil {
s.args = append(s.args, p.loginUser.(string) + "@" + p.host + ":" + p.path + "/")
} else {
s.args = append(s.args, p.host + ":" + p.path + "/")
}
s.args = append(s.args, config.Root_dir.(string) + "/" + p.host + "/" + path.Base(p.path) + "/current/")
cmds = append(cmds, s)
}
return cmds
}
func createTargetDirs(paths []Path, config Config) {
for _, p := range paths {
path := config.Root_dir.(string) + "/" + p.host + "/" + path.Base(p.path) + "/current"
if _, err := os.Stat(path); os.IsNotExist(err) {
log.Infof("Create directory %s", path)
os.MkdirAll(path, 0644)
}
}
}
func deleteExpiredBackups(paths []Path, config Config) {
for _, p := range paths {
path := config.Root_dir.(string) + "/" + p.host + "/" + path.Base(p.path)
if _, err := os.Stat(path); !os.IsNotExist(err) {
items, _ := ioutil.ReadDir(path)
for _, f := range items {
t, err := time.Parse(dateLayout, f.Name())
if err == nil {
currentPath := path + "/" + f.Name()
if time.Now().Unix() - t.Unix() > int64(p.retentionDays) * 86400 {
log.Infof("Expired backup, deleting directory %s", currentPath)
err := os.RemoveAll(currentPath)
if err != nil {
log.Errorf("Deleting directory %s failed", currentPath)
}
}
}
}
}
}
}
func executeWorkers(cmds []Command, config Config) {
jobs := make(chan Command, len(cmds))
results := make(chan error, len(cmds))
for w := 1; w <= config.Concurrent_rsync.(int); w++ {
go worker(w, jobs, results)
}
for _, j := range cmds {
jobs <- j
}
close(jobs)
for a := 1; a <= len(cmds); a++ {
<-results
}
}
func validatePaths(paths []Path, config *Config) []Path {
r := regexp.MustCompile(`/$`)
config.Root_dir = r.ReplaceAllString(config.Root_dir.(string), "")
for i, _ := range paths {
paths[i].path = r.ReplaceAllString(paths[i].path, "")
}
// downward loop to have ability to delete slice element while iterating
for i := len(paths) - 1; i >= 0; i-- {
if match, _ := regexp.MatchString(`^[\.]{1,2}$`, paths[i].path); match {
paths = append(paths[:i], paths[i+1:]...)
}
}
return paths
}
func validateParams(config *Config) {
if config.Concurrent_rsync == nil {
config.Concurrent_rsync = defaultConcurrentRsync
}
if config.Retention_days == nil {
config.Retention_days = defaultRetentionDays
}
if config.Root_dir == nil {
log.Fatal("Cannot find root_dir key in config root level")
}
}
func main() {
f, err := os.OpenFile(logFile, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
log.SetOutput(f)
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.TextFormatter{DisableColors: true})
lock, err := lockfile.New(filepath.Join(os.TempDir(), lockFile))
if err != nil {
log.Fatalf("Cannot init lock. reason: %v", err)
}
err = lock.TryLock()
if err != nil {
log.Fatalf("Cannot lock \"%v\", reason: %v", lock, err)
}
defer lock.Unlock()
go func() {
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM)
<-sigchan
log.Error("Program killed !")
lock.Unlock()
os.Exit(1)
}()
config := parseYaml(configFile)
validateParams(&config)
paths := preparePathsOrder(validatePaths(getPaths(config), &config))
cmds := prepareCommands(paths, config)
createTargetDirs(paths, config)
executeWorkers(cmds, config)
deleteExpiredBackups(paths, config)
}