Skip to content

Commit 9f625a8

Browse files
author
Maxime Ancellin
committed
fmt: Fix syntax
1 parent 5b80750 commit 9f625a8

File tree

14 files changed

+92
-88
lines changed

14 files changed

+92
-88
lines changed

cmd/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@ package main
33
import (
44
"flag"
55
"fmt"
6+
"os"
7+
"runtime"
8+
69
"github.com/neo9/mongodb-backups/pkg/api"
710
"github.com/neo9/mongodb-backups/pkg/config"
811
"github.com/neo9/mongodb-backups/pkg/mongodb"
912
"github.com/neo9/mongodb-backups/pkg/restore"
1013
"github.com/neo9/mongodb-backups/pkg/scheduler"
1114
"github.com/neo9/mongodb-backups/pkg/utils"
1215
log "github.com/sirupsen/logrus"
13-
"os"
14-
"runtime"
1516
)
1617

1718
func printVersion() {
@@ -119,4 +120,3 @@ func main() {
119120
log.SetFormatter(&log.JSONFormatter{})
120121
}
121122
}
122-

pkg/api/server.go

-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ func init() {
1414
log.SetFormatter(&log.JSONFormatter{})
1515
}
1616

17-
1817
type HttpServer struct {
1918
Port int32
2019
}
@@ -28,7 +27,6 @@ func metricsRouter() http.Handler {
2827
return r
2928
}
3029

31-
3230
func (server *HttpServer) Start() {
3331

3432
r := chi.NewRouter()
@@ -38,4 +36,3 @@ func (server *HttpServer) Start() {
3836

3937
log.Error(http.ListenAndServe(fmt.Sprintf(":%v", server.Port), r))
4038
}
41-

pkg/bucket/gs.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package bucket
33
import (
44
"context"
55
"fmt"
6-
"github.com/neo9/mongodb-backups/pkg/config"
7-
"google.golang.org/api/iterator"
86
"io"
97
"os"
108
"path"
119
"strings"
1210

11+
"github.com/neo9/mongodb-backups/pkg/config"
12+
"google.golang.org/api/iterator"
13+
1314
"cloud.google.com/go/storage"
1415

1516
log "github.com/sirupsen/logrus"
@@ -70,10 +71,10 @@ func (bucket *GSBucket) ListFiles(destFolder string) ([]S3File, error) {
7071
break
7172
}
7273
if err != nil {
73-
fmt.Errorf("listBucket: unable to list bucket %q: %v", bucket.GS.Name, err)
74+
_ = fmt.Errorf("listBucket: unable to list bucket %q: %v", bucket.GS.Name, err)
7475
return []S3File{}, err
7576
}
76-
if destFolder == obj.Name || (destFolder + "/") == obj.Name {
77+
if destFolder == obj.Name || (destFolder+"/") == obj.Name {
7778
continue
7879
}
7980

@@ -99,6 +100,9 @@ func (bucket *GSBucket) DownloadFile(src string) (string, error) {
99100

100101
filename := path.Join("/tmp", path.Base(src))
101102
file, err := os.Create(filename)
103+
if err != nil {
104+
return "", err
105+
}
102106
defer file.Close()
103107

104108
rc, err := bucketClient.Object(src).NewReader(ctx)

pkg/bucket/s3.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ package bucket
22

33
import (
44
"fmt"
5+
"os"
6+
"path"
7+
"strings"
8+
59
"github.com/aws/aws-sdk-go/service/s3"
610
"github.com/aws/aws-sdk-go/service/s3/s3manager"
711
"github.com/neo9/mongodb-backups/pkg/config"
812
"github.com/neo9/mongodb-backups/pkg/utils"
9-
"os"
10-
"path"
11-
"strings"
1213

1314
"github.com/aws/aws-sdk-go/aws"
1415
"github.com/aws/aws-sdk-go/aws/session"
1516
)
1617

1718
type S3Bucket struct {
1819
Session *session.Session
19-
S3 *config.S3
20+
S3 *config.S3
2021
}
2122

2223
type S3File struct {
@@ -32,11 +33,10 @@ func NewS3Bucket(s3 *config.S3) *S3Bucket {
3233

3334
return &S3Bucket{
3435
Session: s3Session,
35-
S3: s3,
36+
S3: s3,
3637
}
3738
}
3839

39-
4040
func (bucket *S3Bucket) Upload(filename string, destFolder string) error {
4141
uploader := s3manager.NewUploader(bucket.Session)
4242
file, err := os.Open(filename)
@@ -46,20 +46,20 @@ func (bucket *S3Bucket) Upload(filename string, destFolder string) error {
4646
defer file.Close()
4747

4848
_, err = uploader.Upload(&s3manager.UploadInput{
49-
Bucket: aws.String(bucket.S3.Name),
50-
Key: aws.String(path.Join(destFolder, path.Base(filename))),
51-
Body: file,
49+
Bucket: aws.String(bucket.S3.Name),
50+
Key: aws.String(path.Join(destFolder, path.Base(filename))),
51+
Body: file,
5252
ServerSideEncryption: aws.String("AES256"),
5353
})
5454

5555
return err
5656
}
5757

5858
func (bucket *S3Bucket) ListFiles(destFolder string) ([]S3File, error) {
59-
svc := s3.New(bucket.Session)
59+
svc := s3.New(bucket.Session)
6060

6161
var files []S3File
62-
i := 0
62+
i := 0
6363
err := svc.ListObjectsPages(&s3.ListObjectsInput{
6464
Bucket: &bucket.S3.Name,
6565
Prefix: &destFolder,
@@ -94,13 +94,16 @@ func (bucket *S3Bucket) DownloadFile(src string) (string, error) {
9494

9595
filename := path.Join("/tmp", path.Base(src))
9696
file, err := os.Create(filename)
97+
if err != nil {
98+
return "", err
99+
}
97100
defer file.Close()
98101

99102
writer := &progressWriter{
100-
writer: file,
101-
size: size,
103+
writer: file,
104+
size: size,
102105
humanSize: utils.GetHumanBytes(size),
103-
written: 0,
106+
written: 0,
104107
}
105108
params := &s3.GetObjectInput{
106109
Bucket: aws.String(bucket.S3.Name),
@@ -120,7 +123,7 @@ func (bucket *S3Bucket) DeleteFile(filename string) error {
120123
svc := s3.New(bucket.Session)
121124
_, err := svc.DeleteObject(&s3.DeleteObjectInput{
122125
Bucket: &bucket.S3.Name,
123-
Key: aws.String("//" + filename),
126+
Key: aws.String("//" + filename),
124127
})
125128

126129
return err

pkg/metrics/metrics.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package metrics
33
import "github.com/prometheus/client_golang/prometheus"
44

55
type BackupMetrics struct {
6-
Total *prometheus.CounterVec
7-
RetentionTotal *prometheus.CounterVec
8-
BucketCount *prometheus.GaugeVec
9-
Size *prometheus.GaugeVec
10-
Latency *prometheus.SummaryVec
6+
Total *prometheus.CounterVec
7+
RetentionTotal *prometheus.CounterVec
8+
BucketCount *prometheus.GaugeVec
9+
Size *prometheus.GaugeVec
10+
Latency *prometheus.SummaryVec
1111
LastSuccessfulSnapshot *prometheus.GaugeVec
1212
}
1313

@@ -82,4 +82,4 @@ func New(namespace string, subsystem string) *BackupMetrics {
8282
prometheus.MustRegister(prom.LastSuccessfulSnapshot)
8383

8484
return prom
85-
}
85+
}

pkg/mongodb/dump.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func CreateDump(plan *config.Plan) (MongoDBDump, error) {
2727

2828
authArgs := getAuthenticationArguments()
2929
dumpCommand := fmt.Sprintf(
30-
"mongodump --authenticationDatabase admin %s --archive=%v --gzip --host %s --port %s",
30+
"mongodump --forceTableScan --authenticationDatabase admin %s --archive=%v --gzip --host %s --port %s",
3131
authArgs,
3232
mongoDBDump.ArchiveFile,
3333
plan.MongoDB.Host,

pkg/mongodb/mongodb.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ package mongodb
22

33
import (
44
"fmt"
5-
log "github.com/sirupsen/logrus"
65
"io/ioutil"
76
"os"
7+
8+
log "github.com/sirupsen/logrus"
89
)
910

1011
func init() {
1112
log.SetFormatter(&log.JSONFormatter{})
1213
}
1314

14-
1515
func RemoveFile(filename string) {
1616
err := os.Remove(filename)
1717
if err != nil {

pkg/mongodb/restore.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package mongodb
22

33
import (
44
"fmt"
5+
"time"
6+
57
"github.com/codeskyblue/go-sh"
68
"github.com/neo9/mongodb-backups/pkg/config"
79
"github.com/neo9/mongodb-backups/pkg/utils"
810
"github.com/prometheus/common/log"
9-
"time"
1011
)
1112

1213
func RestoreDump(filename string, args string, plan *config.Plan) error {

pkg/restore/restore.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package restore
22

33
import (
44
"errors"
5+
"os"
6+
"path"
7+
"strings"
8+
"time"
9+
510
"github.com/aws/aws-sdk-go/aws"
611
"github.com/neo9/mongodb-backups/pkg/bucket"
712
"github.com/neo9/mongodb-backups/pkg/mongodb"
813
"github.com/neo9/mongodb-backups/pkg/scheduler"
914
"github.com/neo9/mongodb-backups/pkg/utils"
1015
log "github.com/sirupsen/logrus"
11-
"os"
12-
"path"
13-
"strings"
14-
"time"
1516
)
1617

1718
func DisplayBackups(scheduler *scheduler.Scheduler) error {
@@ -53,7 +54,7 @@ func RestoreLast(scheduler *scheduler.Scheduler, args string) error {
5354
return errors.New("NO_BACKUP")
5455
}
5556

56-
file := files[len(files) - 1]
57+
file := files[len(files)-1]
5758
log.Infof("Restoring backup %s from snapshot %s", file.Etag, file.Name)
5859
return restoreBackup(scheduler, file.Name, args)
5960
}
@@ -111,4 +112,3 @@ func getFiles(scheduler *scheduler.Scheduler) ([]bucket.S3File, error) {
111112

112113
return dumpFiles, nil
113114
}
114-

pkg/scheduler/backup.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package scheduler
22

33
import (
4-
"github.com/neo9/mongodb-backups/pkg/mongodb"
5-
log "github.com/sirupsen/logrus"
64
"os"
75
"path"
86
"time"
7+
8+
"github.com/neo9/mongodb-backups/pkg/mongodb"
9+
log "github.com/sirupsen/logrus"
910
)
1011

1112
func init() {
@@ -72,4 +73,3 @@ func (scheduler *Scheduler) uploadToS3(filename string, destFolder string) error
7273

7374
return nil
7475
}
75-

pkg/scheduler/retention.go

+28-27
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,68 @@ package scheduler
22

33
import (
44
"fmt"
5+
"time"
6+
57
"github.com/neo9/mongodb-backups/pkg/utils"
68
log "github.com/sirupsen/logrus"
7-
"time"
89
)
910

1011
func init() {
1112
log.SetFormatter(&log.JSONFormatter{})
1213
}
1314

1415
func (scheduler *Scheduler) deleteOldBackups() {
15-
files, err := scheduler.Bucket.ListFiles(scheduler.Plan.Name)
16-
if err != nil {
16+
files, err := scheduler.Bucket.ListFiles(scheduler.Plan.Name)
17+
if err != nil {
1718
scheduler.incRetentionMetricError(fmt.Sprintf("Could not list files for plan %s", scheduler.Plan.Name))
18-
return
19+
return
1920
}
2021

21-
retentionDuration, err := utils.GetDurationFromTimeString(scheduler.Plan.Retention)
22-
if err != nil {
22+
retentionDuration, err := utils.GetDurationFromTimeString(scheduler.Plan.Retention)
23+
if err != nil {
2324
scheduler.incRetentionMetricError(fmt.Sprintf("Could not execute retention: %v", err))
24-
return
25+
return
2526
}
2627

27-
var removeFiles []string
28+
var removeFiles []string
2829

29-
for i := 0; i < len(files); i++ {
30-
file := files[i]
31-
log.Debugf("File: ", file.Name)
32-
timestamp, err := utils.GetBucketFileTimestamp(file.Name)
33-
if err != nil {
30+
for i := 0; i < len(files); i++ {
31+
file := files[i]
32+
log.Debugf("File: ", file.Name)
33+
timestamp, err := utils.GetBucketFileTimestamp(file.Name)
34+
if err != nil {
3435
scheduler.incRetentionMetricError(fmt.Sprintf("Could not apply retention: %v", err))
3536
}
3637

37-
ageInSeconds := time.Now().Unix() - timestamp
38-
diffInSeconds := ageInSeconds - int64(retentionDuration.Seconds())
38+
ageInSeconds := time.Now().Unix() - timestamp
39+
diffInSeconds := ageInSeconds - int64(retentionDuration.Seconds())
3940

40-
if diffInSeconds > 0 {
41+
if diffInSeconds > 0 {
4142
log.Debugf("File is %s old and schedule for removal", file.Name)
42-
removeFiles = append(removeFiles, file.Name)
43+
removeFiles = append(removeFiles, file.Name)
4344
} else {
4445
log.Debugf("File is %s old and will be removed in %s", file.Name,
45-
time.Duration(diffInSeconds * -1) * time.Second)
46+
time.Duration(diffInSeconds*-1)*time.Second)
4647
}
4748
}
4849

49-
log.Infof("Retention: %d file(s) to remove", len(removeFiles))
50+
log.Infof("Retention: %d file(s) to remove", len(removeFiles))
5051

51-
status := "success"
52-
for i := 0; i < len(removeFiles); i++ {
53-
err := scheduler.Bucket.DeleteFile(removeFiles[i])
54-
if err != nil {
52+
status := "success"
53+
for i := 0; i < len(removeFiles); i++ {
54+
err := scheduler.Bucket.DeleteFile(removeFiles[i])
55+
if err != nil {
5556
scheduler.incRetentionMetricError(fmt.Sprintf("Could not remove file %s", removeFiles[i]))
56-
status = "error"
57+
status = "error"
5758
}
5859
}
5960

60-
snapshotCount := float64(len(files) / 2 - len(removeFiles))
61+
snapshotCount := float64(len(files)/2 - len(removeFiles))
6162
scheduler.Metrics.BucketCount.WithLabelValues(scheduler.Plan.Name).Set(snapshotCount)
6263
scheduler.Metrics.RetentionTotal.WithLabelValues(scheduler.Plan.Name, status).Inc()
6364
}
6465

6566
func (scheduler *Scheduler) incRetentionMetricError(error string) {
66-
log.Error(error)
67-
scheduler.Metrics.RetentionTotal.WithLabelValues(scheduler.Plan.Name, "error").Inc()
67+
log.Error(error)
68+
scheduler.Metrics.RetentionTotal.WithLabelValues(scheduler.Plan.Name, "error").Inc()
6869
}

0 commit comments

Comments
 (0)