-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
155 lines (130 loc) · 3.2 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/YenchangChan/ch2s3/backup"
"github.com/YenchangChan/ch2s3/config"
"github.com/YenchangChan/ch2s3/constant"
"github.com/YenchangChan/ch2s3/log"
)
var (
partition = flag.String("p", "", "which partition to backup")
ttl = flag.String("ttl", "", "ttl interval")
r = flag.Bool("restore", false, "restore table")
op_type string
cwd string
Version string
BuildStamp string
Githash string
)
func main() {
conf, err := config.ParseConfig(cwd)
if err != nil {
fmt.Printf("parse config failed:%v", err)
os.Exit(-1)
}
log.InitLogger(conf.LogLevel, []string{"stdout", "ch2s3.log"})
log.Logger.Infof("ch2s3, partition: %s, cwd: %s, version: %s, build timestamp: %s, git hash: %s",
*partition, cwd, Version, BuildStamp, Githash)
DumpConfig(conf)
current_partition_only := false
if *ttl == "" {
current_partition_only = true
}
back := backup.NewBack(conf, op_type, *partition, cwd, current_partition_only)
switch op_type {
case constant.OP_TYPE_BACKUP:
err = ch2s3(back)
case constant.OP_TYPE_RESTORE:
err = restore(back)
}
if err != nil {
log.Logger.Panic(err)
}
log.Logger.Infof("backup completed, please see reporter from [%s]!", back.RepoterPath())
}
func init() {
flag.Parse()
op_type = constant.OP_TYPE_BACKUP
if *r {
op_type = constant.OP_TYPE_RESTORE
}
//指定TTL仅在备份时有效
if !*r && *ttl != "" {
//指定TTL时,默认按照toYYYYMMDD分区
ttlExpr := strings.SplitN(*ttl, " ", 2)
interval := ttlExpr[0]
unit := strings.ToUpper(ttlExpr[1])
var year, month, day int
switch unit {
case "DAY", "D":
if d, err := strconv.Atoi(interval); err == nil {
day = d * (-1)
}
case "WEEK", "W":
if w, err := strconv.Atoi(interval); err == nil {
day = w * 7 * (-1)
}
case "MONTH", "M", "MON":
if m, err := strconv.Atoi(interval); err == nil {
month = m * (-1)
}
case "YEAR", "Y":
if y, err := strconv.Atoi(interval); err == nil {
year = y * (-1)
}
}
*partition = time.Now().AddDate(year, month, day).Format("20060102")
}
if *partition == "" {
*partition = time.Now().Format("20060102")
}
exe, _ := filepath.Abs(os.Args[0])
cwd = filepath.Dir(filepath.Dir(exe))
}
func ch2s3(back *backup.Backup) error {
var err error
if err = back.Init(); err != nil {
return err
}
log.Logger.Infof("backup init success!")
defer back.Stop()
if err = back.Do(); err != nil {
return err
}
log.Logger.Infof("backup to s3 success!")
if err = back.Repoter(op_type); err != nil {
return err
}
log.Logger.Infof("backup reporter success!")
return nil
}
func restore(back *backup.Backup) error {
var err error
if err = back.Init(); err != nil {
return err
}
log.Logger.Infof("restore init success!")
defer back.Stop()
if err = back.Restore(); err != nil {
return err
}
log.Logger.Infof("restore from s3 success!")
if err = back.Repoter(op_type); err != nil {
return err
}
log.Logger.Infof("restore reporter success!")
return nil
}
func DumpConfig(c *config.Config) {
raw, err := json.MarshalIndent(c, " ", " ")
if err == nil {
log.Logger.Infof("%s", string(raw))
}
}