Skip to content

Commit

Permalink
pkg: Add --dump support
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Boissiere committed Aug 9, 2019
1 parent 310403c commit 25b203e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Can be easily monitored by Prometheus.
./mongodb-backups --config ./config.yaml --restore [id] --args '--drop'
# Restore last backup
./mongodb-backups --config ./config.yaml --restore-last --args '--drop'
# Arbitrary dump
./mongodb-backups --config ./config.yaml --dump
```

Parameters:
Expand Down
29 changes: 29 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"github.com/neo9/mongodb-backups/pkg/api"
"github.com/neo9/mongodb-backups/pkg/config"
"github.com/neo9/mongodb-backups/pkg/mongodb"
"github.com/neo9/mongodb-backups/pkg/restore"
"github.com/neo9/mongodb-backups/pkg/scheduler"
"github.com/neo9/mongodb-backups/pkg/utils"
log "github.com/sirupsen/logrus"
"os"
"runtime"
Expand Down Expand Up @@ -52,6 +54,29 @@ func restoreLastBackup(confPath string, args string) {
}
}

func arbitraryDump(confPath string) {
backupScheduler := getScheduler(confPath)
log.Infof("Creating MongoDB dump for %s", backupScheduler.Plan.Name)
mongoDBDump, err := mongodb.CreateDump(backupScheduler.Plan)
if err != nil {
log.Errorf("Error creating dump for %s", backupScheduler.Plan.Name)
os.Exit(1)
}

uploadDumpFile(mongoDBDump.ArchiveFile, backupScheduler)
uploadDumpFile(mongoDBDump.LogFile, backupScheduler)
log.Infof("Dump successful")
}

func uploadDumpFile(filename string, scheduler *scheduler.Scheduler) {
log.Infof("Upload file %s. Size: %s", filename, utils.GetHumanFileSize(filename))
err := scheduler.Bucket.Upload(filename, scheduler.Plan.Name)
if err != nil {
log.Errorf("Could not upload log file: %v", err)
os.Exit(1)
}
}

func launchServer(confPath string, port int32) {
printVersion()
backupScheduler := getScheduler(confPath)
Expand All @@ -69,6 +94,7 @@ func main() {
confPath := flag.String("config", "./config.yaml", "Plan config path")
port := flag.Int("port", 8080, "Server port")
list := flag.Bool("list", false, "List backups")
dump := flag.Bool("dump", false, "Arbitrary dump")
restoreID := flag.String("restore", "", "Restore specific backup")
restoreLast := flag.Bool("restore-last", false, "Restore last backup")
args := flag.String("args", "", "MongoDB args")
Expand All @@ -77,6 +103,9 @@ func main() {
if *list {
log.SetFormatter(&log.TextFormatter{})
listBackups(*confPath)
} else if *dump {
log.SetFormatter(&log.TextFormatter{})
arbitraryDump(*confPath)
} else if *restoreLast {
log.SetFormatter(&log.TextFormatter{})
restoreLastBackup(*confPath, *args)
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"errors"
"fmt"
"os"
"regexp"
"strconv"
)
Expand All @@ -21,3 +22,11 @@ func GetBucketFileTimestamp(file string) (int64, error) {

return timestamp, nil
}

func GetHumanFileSize(filename string) string {
stat, err := os.Stat(filename)
if err != nil {
return "UNKNOWN"
}
return GetHumanBytes(stat.Size())
}

0 comments on commit 25b203e

Please sign in to comment.