-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
174 lines (124 loc) · 3.11 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//https://github.com/JamesStewy/go-mysqldump
package main
import (
"fmt"
"database/sql"
"github.com/JamesStewy/go-mysqldump"
_ "github.com/go-sql-driver/mysql"
"os"
"os/exec"
"log"
"github.com/joho/godotenv"
"github.com/jasonlvhit/gocron"
)
const (
PATH_DAILY = "dumps/daily"
PATH_WEEKLY = "dumps/weekly"
PATH_MONTHLY = "dumps/monthly"
)
func initDB() *sql.DB{
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
DB_HOST := os.Getenv("DB_HOST")
DB_PORT := os.Getenv("DB_PORT")
DB_USERNAME := os.Getenv("DB_USERNAME")
DB_PASSWORD := os.Getenv("DB_PASSWORD")
DB_NAME := os.Getenv("DB_NAME")
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", DB_USERNAME, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME))
if err != nil {
panic(err)
}
if db == nil {
panic("db nil")
}
return db
}
func Daily(){
db := initDB()
DB_NAME := os.Getenv("DB_NAME")
_, err := os.Stat(PATH_DAILY)
if os.IsNotExist(err) {
errDir := os.MkdirAll(PATH_DAILY, 0755)
if errDir != nil {
log.Fatal(err)
}
}
dumpFilenameFormat := fmt.Sprintf("%s-daily-20060102T150405", DB_NAME) // accepts time layout string and add .sql at the end of file
dumper, err := mysqldump.Register(db, PATH_DAILY, dumpFilenameFormat)
if err != nil {
fmt.Println("Error registering databse:", err)
return
}
// Dump database to file
resultFilename, err := dumper.Dump()
if err != nil {
fmt.Println("Error dumping:", err)
return
}
fmt.Printf("\nFile is saved to %s", resultFilename)
// Close dumper and connected database
dumper.Close()
}
func Weekly(){
db := initDB()
DB_NAME := os.Getenv("DB_NAME")
_, err := os.Stat(PATH_WEEKLY)
if os.IsNotExist(err) {
errDir := os.MkdirAll(PATH_WEEKLY, 0755)
if errDir != nil {
log.Fatal(err)
}
}
dumpFilenameFormat := fmt.Sprintf("%s-weekly-20060102T150405", DB_NAME) // accepts time layout string and add .sql at the end of file
dumper, err := mysqldump.Register(db, PATH_WEEKLY, dumpFilenameFormat)
if err != nil {
fmt.Println("Error registering databse:", err)
return
}
// Dump database to file
resultFilename, err := dumper.Dump()
if err != nil {
fmt.Println("Error dumping:", err)
return
}
fmt.Printf("\nFile is saved to %s", resultFilename)
// Close dumper and connected database
dumper.Close()
}
func RotateDaily(){
arg1 := "find dumps/daily -type f -mtime +30 -exec rm -f {} \;"
cmd := exec.Command(arg1)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(stdout)
}
func RotateWeekly(){
arg1 := "find dumps/weekly -type f -mtime +120 -exec rm -f {} \;"
cmd := exec.Command(arg1)
stdout, err := cmd.Output()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(stdout)
}
func Echo(){
fmt.Println("service up !")
}
func main(){
fmt.Println("Service up")
s := gocron.NewScheduler()
s.Every(30).Second().Do(Echo)
s.Every(1).Day().Do(Daily)
s.Every(1).Week().Do(Weekly)
// clean files old 1 months
s.Every(1).Day().Do(RotateDaily)
// clean files old 3 months
s.Every(1).Day().Do(RotateWeekly)
<- s.Start()
}