From 9ca93236731df7872598b847b8b87ed3cc0dfbf4 Mon Sep 17 00:00:00 2001 From: Romitou Date: Fri, 12 Nov 2021 17:54:32 +0100 Subject: [PATCH] add password support --- gitignore => .gitignore | 7 ++++--- config.yml | 9 +++++---- go.mod | 1 + go.sum | 2 ++ main.go | 38 ++++++++++++++++++++++++++++++++++---- 5 files changed, 46 insertions(+), 11 deletions(-) rename gitignore => .gitignore (60%) diff --git a/gitignore b/.gitignore similarity index 60% rename from gitignore rename to .gitignore index 6dd8381..af9b781 100644 --- a/gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ -config.yml -logs.log -.idea \ No newline at end of file +config.yml +logs.log +.idea +backups \ No newline at end of file diff --git a/config.yml b/config.yml index 87d6327..996b037 100644 --- a/config.yml +++ b/config.yml @@ -1,5 +1,6 @@ -logPath: "" -mongoUri: "" -webhook: - id: "" +logPath: "" +mongoUri: "" +zipPassword: "" +webhook: + id: "" token: "" \ No newline at end of file diff --git a/go.mod b/go.mod index d127398..e020b36 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/Romitou/MongoBackup go 1.16 require ( + github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0 // indirect github.com/andersfylling/snowflake v1.3.0 // indirect github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6 // indirect github.com/coreos/bbolt v1.3.2 // indirect diff --git a/go.sum b/go.sum index 2da2313..4d049db 100644 --- a/go.sum +++ b/go.sum @@ -44,6 +44,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE github.com/alecthomas/gometalinter v2.0.10+incompatible/go.mod h1:qfIpQGGz3d+NmgyPBqv+LSh50emm1pt72EtcX2vKYQk= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0 h1:BVts5dexXf4i+JX8tXlKT0aKoi38JwTXSe+3WUneX0k= +github.com/alexmullins/zip v0.0.0-20180717182244-4affb64b04d0/go.mod h1:FDIQmoMNJJl5/k7upZEnGvgWVZfFeE6qHeN7iCMbCsA= github.com/andersfylling/snowflake v1.3.0 h1:dobYwnkDhe3EzvX3FMZlrRygQ1CVj1W+s3wlJuiuqwQ= github.com/andersfylling/snowflake v1.3.0/go.mod h1:mePGbOEexjod5BUkgAfCoccI//TzrywM4S25u4unsQM= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= diff --git a/main.go b/main.go index bac4d60..72a0d44 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "flag" + "github.com/alexmullins/zip" "github.com/andersfylling/snowflake" "github.com/nickname32/discordhook" "github.com/spf13/viper" @@ -18,6 +19,7 @@ import ( ) var backupData string +var backupName = time.Now().Format("2006-01-02T150405") func main() { // loading configuration file path @@ -77,6 +79,9 @@ func main() { } webhookID := viper.GetString("webhook.id") webhookToken := viper.GetString("webhook.token") + zipPassword := viper.GetString("zipPassword") + + createZipFile(zipPassword) sendToDiscord(webhookID, webhookToken) } @@ -100,19 +105,44 @@ func backupCollection(cursor *mongo.Cursor, ctx context.Context) { backupData += string(marshal) + "\n" } if err := cursor.Err(); err != nil { - log.Fatal("an error occured with the mongo cursor: ", err) + log.Fatal("an error occurred with the mongo cursor: ", 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 archive.Close() + + zipWriter := zip.NewWriter(archive) + writer, err := zipWriter.Encrypt(backupName+".json", zipPassword) + if err != nil { + log.Fatal("an error occurred while creating the archive: ", err) + } + + _, err = io.Copy(writer, strings.NewReader(backupData)) + if err != nil { + log.Fatal("an error occurred while creating the archive: ", err) } + defer zipWriter.Close() } func sendToDiscord(webhookID string, webhookToken string) { api, err := discordhook.NewWebhookAPI(snowflake.ParseSnowflakeString(webhookID), webhookToken, true, nil) if err != nil { - log.Fatal("an error occured while the webhook api creation: ", err) + 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().String(), - }, strings.NewReader(backupData), "backup.json") + 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)