-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbackup.go
220 lines (188 loc) Β· 4.13 KB
/
backup.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
package main
import (
"archive/zip"
"context"
"io"
"log"
"os"
"path/filepath"
"time"
"cloud.google.com/go/storage"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func backupDatabase() {
if config.BackupProvider == "none" || config.BackupProvider == "" {
log.Println("π« no backup provider set")
return
}
ticker := time.NewTicker(time.Duration(config.BackupIntervalHours) * time.Hour)
defer ticker.Stop()
zipFileName := "db.zip"
for {
select {
case <-ticker.C:
ZipDirectory("db", zipFileName)
switch config.BackupProvider {
case "s3":
S3Upload(zipFileName)
case "aws":
AwsUpload(zipFileName)
case "gcp":
GCPBucketUpload(zipFileName)
default:
log.Println("π« we only support AWS, GCP, and S3 at this time")
}
}
}
}
// Deprecated: Use S3Upload instead
func GCPBucketUpload(zipFileName string) {
if config.GcpConfig == nil {
log.Fatal("π« GCP specified as backup provider but no GCP config found. Check environment variables.")
}
bucket := config.GcpConfig.Bucket
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
defer client.Close()
// open the zip db file.
f, err := os.Open(zipFileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
obj := client.Bucket(bucket).Object(zipFileName)
// Upload an object with storage.Writer.
wc := obj.NewWriter(ctx)
if _, err = io.Copy(wc, f); err != nil {
log.Fatal(err)
}
if err := wc.Close(); err != nil {
log.Fatal(err)
}
log.Printf("β
Successfully uploaded %q to %q\n", zipFileName, bucket)
// delete the file.
err = os.Remove(zipFileName)
if err != nil {
log.Fatal(err)
}
}
// Deprecated: Use S3Upload instead
func AwsUpload(zipFileName string) {
if config.AwsConfig == nil {
log.Fatal("π« AWS specified as backup provider but no AWS config found. Check environment variables.")
}
s3UploadShared(
zipFileName,
config.AwsConfig.AccessKeyID,
config.AwsConfig.SecretAccessKey,
"s3.amazonaws.com",
config.AwsConfig.Region,
config.AwsConfig.Bucket,
true,
)
}
func S3Upload(zipFileName string) {
if config.S3Config == nil {
log.Fatal("π« S3 specified as backup provider but no S3 config found. Check environment variables.")
}
s3UploadShared(
zipFileName,
config.S3Config.AccessKeyID,
config.S3Config.SecretKey,
config.S3Config.Endpoint,
config.S3Config.Region,
config.S3Config.BucketName,
true,
)
}
func s3UploadShared(
zipFileName string,
accessKey string,
secret string,
endpoint string,
region string,
bucketName string,
secure bool,
) {
log.Println("π uploading to S3 Bucket...")
// Create MinIO client
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKey, secret, ""),
Region: region,
Secure: secure,
})
if err != nil {
log.Fatal(err)
}
// Upload the file to the S3 bucket
file, err := os.Open(zipFileName)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fileInfo, err := file.Stat()
if err != nil {
log.Fatal(err)
}
_, err = client.PutObject(
context.Background(),
bucketName,
zipFileName,
file,
fileInfo.Size(),
minio.PutObjectOptions{
ContentType: "application/octet-stream",
},
)
if err != nil {
log.Fatal(err)
}
log.Printf("β
Successfully uploaded %q to %q\n", zipFileName, bucketName)
// delete the file
err = os.Remove(zipFileName)
if err != nil {
log.Fatal(err)
}
}
func ZipDirectory(sourceDir, zipFileName string) error {
log.Println("π¦ zipping up the database")
file, err := os.Create(zipFileName)
if err != nil {
panic(err)
}
defer file.Close()
w := zip.NewWriter(file)
defer w.Close()
walker := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
f, err := w.Create(path)
if err != nil {
return err
}
_, err = io.Copy(f, file)
if err != nil {
return err
}
return nil
}
err = filepath.Walk(sourceDir, walker)
if err != nil {
//panic(err)
}
log.Println("π¦ database zipped up!")
return nil
}