-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (96 loc) · 2.88 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
package main
import (
"context"
"flag"
"fmt"
"github.com/alexmullins/zip"
"github.com/andersfylling/snowflake"
"github.com/nickname32/discordhook"
"github.com/spf13/viper"
"io"
"log"
"os"
"os/exec"
"time"
)
var backupName = time.Now().Format("2006-01-02T150405")
func main() {
// loading configuration file path
configPath := flag.String("config", "/etc/mongobackup.yml", "path to configuration file")
flag.Parse()
log.Println("loading configuration file from " + *configPath)
// loading configuration
viper.SetConfigFile(*configPath)
err := viper.ReadInConfig()
if err != nil {
log.Fatal("an error occurred while reading the configuration: ", err)
}
// opening the log file
logPath := viper.GetString("logPath")
file, err := os.OpenFile(logPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatal("an error occurred while opening the log file: ", err)
}
// formatting log output
log.SetFlags(log.Ltime)
log.SetOutput(io.MultiWriter(file, os.Stdout))
runMongoDump()
webhookID := viper.GetString("webhook.id")
webhookToken := viper.GetString("webhook.token")
zipPassword := viper.GetString("zipPassword")
createZipFile(zipPassword)
sendToDiscord(webhookID, webhookToken)
}
func runMongoDump() {
subProcess := exec.Command("mongodump", "--uri=\""+viper.GetString("mongoUri")+"\"", "--archive=backups/dump."+backupName+".archive")
if err := subProcess.Run(); err != nil {
fmt.Println("An error occured: ", err)
}
}
func createZipFile(zipPassword string) {
archive, err := os.Create("backups/" + backupName + ".zip")
if err != nil {
log.Fatal("an error occurred while creating the archive: ", err)
}
defer func(archive *os.File) {
err = archive.Close()
if err != nil {
log.Println(err)
}
}(archive)
zipWriter := zip.NewWriter(archive)
writer, err := zipWriter.Encrypt("backups/dump."+backupName+".archive", zipPassword)
if err != nil {
log.Fatal("an error occurred while creating the archive: ", err)
}
file, err := os.Open("backups/dump." + backupName + ".archive")
if err != nil {
return
}
_, err = io.Copy(writer, file)
if err != nil {
log.Fatal("an error occurred while creating the archive: ", err)
}
defer func(zipWriter *zip.Writer) {
err = zipWriter.Close()
if err != nil {
log.Println(err)
}
}(zipWriter)
}
func sendToDiscord(webhookID string, webhookToken string) {
api, err := discordhook.NewWebhookAPI(snowflake.ParseSnowflakeString(webhookID), webhookToken, true, nil)
if err != nil {
log.Fatal("an error occurred while the webhook api creation: ", err)
}
file, err := os.Open("backups/" + backupName + ".zip")
if err != nil {
return
}
_, err = api.Execute(context.TODO(), &discordhook.WebhookExecuteParams{
Content: "backup: " + time.Now().Format(time.RFC3339),
}, file, time.Now().Format(time.RFC3339)+".zip")
if err != nil {
log.Fatal("an error occured while executing the webhook: ", err)
}
}