-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
60 lines (53 loc) · 1.14 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/robfig/cron/v3"
"log"
"path/filepath"
"strings"
)
type Uploader interface {
upload(key, filePath string) error
}
type Dumper interface {
Dump() (string, error)
}
func start(conf *Config) {
uploader := NewQiniuUploader(&conf.Qiniu)
for _, db := range conf.Databases {
dump := NewMysqlDump(&db)
file, err := dump.Dump()
if err != nil {
log.Printf("Backup database %s failed: %s", db.Database, err)
continue
}
fileName := filepath.Base(file)
key := fmt.Sprintf("%s/%s/%s/%s", strings.TrimRight(conf.Qiniu.FilePrefix, "/"), db.Host, db.User, fileName)
err = uploader.upload(key, file)
if err != nil {
log.Printf("Upload to Qiniu failed: %s", err)
continue
}
}
}
func main() {
conf := flag.String("config", "config.json", "config file path")
flag.Parse()
config, err := loadConfig(*conf)
if err != nil {
log.Fatalf("Load config failed: %s", err)
}
if config.Cron != "" {
task := cron.New()
_, e := task.AddFunc(config.Cron, func() {
start(config)
})
if e != nil {
log.Fatalf("Run cron task failed: %s", e)
}
task.Run()
} else {
start(config)
}
}