generated from NezuChan/golang-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (97 loc) · 2.93 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
package main
import (
"archive/zip"
"bytes"
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"log"
"os"
"os/exec"
"time"
"github.com/go-co-op/gocron"
"github.com/joho/godotenv"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
)
func GenerateRandomHex(length int) (string, error) {
if length % 2 != 0 {
return "", fmt.Errorf("length must be an even number")
}
bytes := make([]byte, length/2)
_, err := rand.Read(bytes)
if err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func main() {
_ = godotenv.Load()
logger := log.New(os.Stdout, "", log.Ldate|log.Ltime)
endpoint := os.Getenv("S3_ENDPOINT")
accessKeyID := os.Getenv("S3_ACCESS")
secretAccessKey := os.Getenv("S3_SECRET")
useSSL := os.Getenv("S3_USE_SSL") == "true"
client, err := minio.New(endpoint, &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Secure: useSSL,
})
if err != nil {
log.Fatalln(err)
}
c := gocron.NewScheduler(time.UTC)
Cron := os.Getenv("CRON_JOB")
logger.Printf("pgsql will be dumped every %s", Cron)
c.Cron(Cron).Do(
func() {
logger.Println("Starting to dump pgsql...")
cmd := "pg_dump"
args := []string{"-U", os.Getenv("PGUSERNAME"), "-h", os.Getenv("PGHOST"), "-p", os.Getenv("PGPORT")}
out, err := exec.Command(cmd, args...).Output()
if err != nil {
log.Println("Error executing command:", err)
return
}
zipBuffer := new(bytes.Buffer)
zipWriter := zip.NewWriter(zipBuffer)
zipEntry, err := zipWriter.Create("backup.sql")
if err != nil {
log.Printf("Error creating zip entry: %v\n", err)
return
}
_, err = zipEntry.Write(out)
if err != nil {
log.Printf("Error writing to zip entry: %v\n", err)
return
}
logger.Println("pgsql dumped successfully...")
zipWriter.Close()
zipWriter.Flush()
randHex, err := GenerateRandomHex(32)
if err != nil {
log.Printf("Error creating hex: %v\n", err)
return
}
currentTime := time.Now()
year, month, day := currentTime.Date()
date := fmt.Sprintf("%d-%02d-%02d", year, month, day)
fileName := fmt.Sprintf("%s/%d/%d/%d/%s (%s).zip", os.Getenv("FILE_PATH"), year, month, day, date, randHex)
log.Printf("File name: %s", fileName)
_, err = client.PutObject(context.Background(), os.Getenv("S3_BUCKET"), fileName, bytes.NewReader(zipBuffer.Bytes()), int64(zipBuffer.Len()), minio.PutObjectOptions{ContentType: "application/zip"})
if err != nil {
log.Printf("Error uploading file to S3: %v\n", err)
return
}
log.Printf("Uploaded the file to S3")
retainTime := currentTime.Add(3 * 24 * time.Hour)
client.PutObjectRetention(context.Background(), os.Getenv("S3_BUCKET"), fileName, minio.PutObjectRetentionOptions{
RetainUntilDate: &retainTime,
VersionID: fmt.Sprintf("Retain %s after 3 days", fileName),
})
log.Printf("%s will be retained after 3 days", fileName)
},
)
c.StartAsync()
select {}
}