Skip to content

Commit

Permalink
StoragePath-optional (maxisam#42)
Browse files Browse the repository at this point in the history
* ✨feature: skip local storage when retention = 0
  • Loading branch information
maxisam authored and danielchristianschroeter committed Nov 6, 2024
1 parent 02cda4d commit f09596b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["backoff", "Infof", "mlog", "Rclone", "Wrapf"]
}
6 changes: 4 additions & 2 deletions pkg/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func (s *HttpServer) Start(version string) {
r.Post("/{planID}", postBackup)
})

FileServer(r, "/storage", http.Dir(s.Config.StoragePath))
if s.Config.StoragePath != "" {
FileServer(r, "/storage", http.Dir(s.Config.StoragePath))
}

log.Error(http.ListenAndServe(fmt.Sprintf("%s:%v", s.Config.Host, s.Config.Port), r))
}
Expand All @@ -59,7 +61,7 @@ func FileServer(r chi.Router, path string, root http.FileSystem) {
fs := http.StripPrefix(path, http.FileServer(root))

if path != "/" && path[len(path)-1] != '/' {
r.Get(path, http.RedirectHandler(path+"/", 301).ServeHTTP)
r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
path += "/"
}
path += "*"
Expand Down
71 changes: 38 additions & 33 deletions pkg/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,13 @@ import (

func Run(plan config.Plan, conf *config.AppConfig, modules *config.ModuleConfig) (Result, error) {
tmpPath := conf.TmpPath
storagePath := conf.StoragePath

t1 := time.Now()
planDir := fmt.Sprintf("%v/%v", storagePath, plan.Name)

archive, mlog, err := dump(plan, tmpPath, t1.UTC())
log.WithFields(log.Fields{
"archive": archive,
"mlog": mlog,
"planDir": planDir,
"err": err,
}).Info("new dump")

Expand All @@ -38,50 +36,32 @@ func Run(plan config.Plan, conf *config.AppConfig, modules *config.ModuleConfig)
return res, err
}

err = sh.Command("mkdir", "-p", planDir).Run()
if err != nil {
return res, errors.Wrapf(err, "creating dir %v in %v failed", plan.Name, storagePath)
}

fi, err := os.Stat(archive)
if err != nil {
return res, errors.Wrapf(err, "stat file %v failed", archive)
}
res.Size = fi.Size()

err = sh.Command("mv", archive, planDir).Run()
if err != nil {
return res, errors.Wrapf(err, "moving file from %v to %v failed", archive, planDir)
}
file := archive

// check if log file exists, is not always created
if _, err := os.Stat(mlog); os.IsNotExist(err) {
log.Debug("appears no log file was generated")
} else {
err = sh.Command("mv", mlog, planDir).Run()
if plan.Encryption != nil {
encryptedFile := fmt.Sprintf("%v.encrypted", archive)
output, err := encrypt(archive, encryptedFile, plan, conf)
if err != nil {
return res, errors.Wrapf(err, "moving file from %v to %v failed", mlog, planDir)
return res, err
} else {
removeUnencrypted(archive, encryptedFile)
file = encryptedFile
log.WithField("plan", plan.Name).Infof("Encryption finished %v", output)
}
}

if plan.Scheduler.Retention > 0 {
err = applyRetention(planDir, plan.Scheduler.Retention)
if err != nil {
return res, errors.Wrap(err, "retention job failed")
}
}

file := filepath.Join(planDir, res.Name)

if plan.Encryption != nil {
encryptedFile := fmt.Sprintf("%v.encrypted", file)
output, err := encrypt(file, encryptedFile, plan, conf)
if conf.StoragePath != "" && plan.Scheduler.Retention != 0 {
localBackupOutput, err := localBackup(file, conf.StoragePath, mlog, plan)
if err != nil {
return res, err
} else {
removeUnencrypted(file, encryptedFile)
file = encryptedFile
log.WithField("plan", plan.Name).Infof("Encryption finished %v", output)
log.WithField("plan", plan.Name).Infof("Local backup finished %v", localBackupOutput)
}
}

Expand Down Expand Up @@ -130,8 +110,33 @@ func Run(plan config.Plan, conf *config.AppConfig, modules *config.ModuleConfig)
}
}

output, err := cleanup(file, mlog)
if err != nil {
return res, err
} else {
log.WithField("plan", plan.Name).Infof("Clean up temp finished %v", output)
}

t2 := time.Now()
res.Status = 200
res.Duration = t2.Sub(t1)
return res, nil
}

func cleanup(file string, mlog string) (string, error) {
err := sh.Command("rm", file).Run()
if err != nil {
return "", errors.Wrapf(err, "remove file from %v failed", file)
}
// check if log file exists, is not always created
if _, err := os.Stat(mlog); os.IsNotExist(err) {
log.Debug("appears no log file was generated")
} else {
err = sh.Command("rm", mlog).Run()
if err != nil {
return "", errors.Wrapf(err, "remove file from %v failed", mlog)
}
}
msg := fmt.Sprintf("Temp folder cleanup finished, `%v` is removed.", file)
return msg, nil
}
35 changes: 35 additions & 0 deletions pkg/backup/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math"
"os"
"path/filepath"
"regexp"
"strings"
"time"
Expand All @@ -15,6 +16,40 @@ import (
"github.com/stefanprodan/mgob/pkg/config"
)

func localBackup(file string, storagePath string, mlog string, plan config.Plan) (string, error) {
t1 := time.Now()
planDir := fmt.Sprintf("%v/%v", storagePath, plan.Name)
err := sh.Command("mkdir", "-p", planDir).Run()
if err != nil {
return "", errors.Wrapf(err, "creating dir %v in %v failed", plan.Name, storagePath)
}
err = sh.Command("cp", file, planDir).Run()
if err != nil {
return "", errors.Wrapf(err, "moving file from %v to %v failed", file, planDir)
}
// check if log file exists, is not always created
if _, err := os.Stat(mlog); os.IsNotExist(err) {
log.Debug("appears no log file was generated")
} else {
err = sh.Command("cp", mlog, planDir).Run()
if err != nil {
return "", errors.Wrapf(err, "moving file from %v to %v failed", mlog, planDir)
}
}
if plan.Scheduler.Retention > 0 {
err = applyRetention(planDir, plan.Scheduler.Retention)
if err != nil {
return "", errors.Wrap(err, "retention job failed")
}
}
_, filename := filepath.Split(file)
distPath := filepath.Join(planDir, filename)
t2 := time.Now()
msg := fmt.Sprintf("Local backup finished `%v` -> `%v` Duration: %v",
file, distPath, t2.Sub(t1))
return msg, nil
}

func dump(plan config.Plan, tmpPath string, ts time.Time) (string, string, error) {
retryCount := 0.0
archive := fmt.Sprintf("%v/%v-%v.gz", tmpPath, plan.Name, ts.Unix())
Expand Down

0 comments on commit f09596b

Please sign in to comment.