Skip to content

Commit

Permalink
add password support
Browse files Browse the repository at this point in the history
  • Loading branch information
Romitou committed Nov 12, 2021
1 parent 67296d0 commit 9ca9323
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 11 deletions.
7 changes: 4 additions & 3 deletions gitignore → .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config.yml
logs.log
.idea
config.yml
logs.log
.idea
backups
9 changes: 5 additions & 4 deletions config.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
logPath: ""
mongoUri: ""
webhook:
id: ""
logPath: ""
mongoUri: ""
zipPassword: ""
webhook:
id: ""
token: ""
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
38 changes: 34 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -18,6 +19,7 @@ import (
)

var backupData string
var backupName = time.Now().Format("2006-01-02T150405")

func main() {
// loading configuration file path
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
Expand Down

0 comments on commit 9ca9323

Please sign in to comment.