Skip to content

Commit

Permalink
Merge pull request #1 from kreatoo/always-bg
Browse files Browse the repository at this point in the history
feat: Use robfig/cron to make it always run in the foreground
  • Loading branch information
LobbyLobster authored Apr 25, 2024
2 parents 5c445cb + e7224cc commit b040736
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func dumpDB(db string, dst string) (dumpPath string, name string, err error) {
}

func Backup() {
logger.Info("monodb-backup started.")
logger.Info("monodb-backup job started.")
notify.SendAlarm("Database backup started.", false)

dateNow = rightNow{
Expand Down Expand Up @@ -214,8 +214,8 @@ func Backup() {
}
}
}
logger.Info("monodb-backup finished.")
notify.SendAlarm("monodb-backup finished.", false)
logger.Info("monodb-backup job finished.")
notify.SendAlarm("monodb-backup job finished.", false)
}

func uploads(name, db, filePath string) {
Expand Down
1 change: 1 addition & 0 deletions config/config.sample.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
backupDestination: /var/backups
database: postgresql # or mysql - default is postgresql
runEveryCron: "@every 1m" # run every minute
databases: # all databases if empty
- db1
- db2
Expand Down
1 change: 1 addition & 0 deletions config/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Params struct {
Rotation Rotation
Remote Remote
Cluster Cluster
RunEveryCron string
Notify struct {
Email struct {
Enabled bool
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/robfig/cron v1.2.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ github.com/pkg/sftp v1.13.6/go.mod h1:tz1ryNURKu77RL+GuCzmoJYxQczL3wLNNpPWagdg4Q
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
Expand Down
20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"flag"
"fmt"
"github.com/robfig/cron"
"monodb-backup/backup"
"monodb-backup/clog"
"monodb-backup/config"
Expand All @@ -21,11 +22,30 @@ func main() {

config.ParseParams(filePath)
clog.InitializeLogger()

var logger *clog.CustomLogger = &clog.Logger

logger.Info("monodb-backup started.")

if config.Parameters.RunEveryCron != "" {
c := cron.New()
c.AddFunc(config.Parameters.RunEveryCron, initBackup)
c.Start()
select {}
} else {
// backwards compatibility
initBackup()
}
}

func initBackup() {
if config.Parameters.Minio.Enabled {
backup.InitializeMinioClient()
}

if config.Parameters.S3.Enabled {
backup.InitializeS3Session()
}

backup.Backup()
}

0 comments on commit b040736

Please sign in to comment.