-
Notifications
You must be signed in to change notification settings - Fork 14
/
backup.go
166 lines (138 loc) · 2.91 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
package main
import (
"archive/zip"
"context"
"io"
"log"
"os"
"path/filepath"
"time"
"cloud.google.com/go/storage"
"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type AwsCredentials struct {
AccessKeyID string
SecretAccessKey string
Region string
Bucket string
}
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()
for {
select {
case <-ticker.C:
ZipDirectory("db", "db.zip")
switch config.BackupProvider {
case "aws":
S3Upload()
case "gcp":
GCPBucketUpload()
default:
log.Println("🚫 we only support AWS at this time")
}
}
}
}
func GCPBucketUpload() {
bucket := getEnv("GCP_BUCKET_NAME")
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("db.zip")
if err != nil {
log.Fatal(err)
}
defer f.Close()
obj := client.Bucket(bucket).Object("db.zip")
// 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", "db.zip", bucket)
// delete the file.
err = os.Remove("db.zip")
if err != nil {
log.Fatal(err)
}
}
func S3Upload() {
bucket := getEnv("AWS_BUCKET_NAME")
cfg, err := awsConfig.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatal(err)
}
// Create an Amazon S3 service client
client := s3.NewFromConfig(cfg)
// Upload the file to S3
file, err := os.Open("db.zip")
if err != nil {
log.Fatal(err)
}
_, err = client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String("db.zip"),
Body: file,
})
if err != nil {
log.Fatal(err)
}
log.Printf("✅ Successfully uploaded %q to %q\n", "db.zip", bucket)
// delete the file
err = os.Remove("db.zip")
if err != nil {
log.Fatal(err)
}
}
func ZipDirectory(sourceDir, zipFileName string) error {
log.Println("📦 zipping up the database")
file, err := os.Create("db.zip")
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("db", walker)
if err != nil {
//panic(err)
}
log.Println("📦 database zipped up!")
return nil
}