feat: replace interval with a proper cron schedule
continuous-integration/drone/push Build is passing Details

This commit is contained in:
kolaente 2021-12-30 12:57:58 +01:00
parent 7b8e1d187c
commit 9cecce4f2d
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 20 additions and 19 deletions

View File

@ -46,12 +46,13 @@ Where all backup files will be stored.
Default: `/backups` Default: `/backups`
### `BACKUP_INTERVAL` ### `BACKUP_SCHEDULE`
The interval at which backups will happen. Must be a parsable string The cron schedule at which the backup job runs, using the common unix cron syntax.
as [time.Duration](https://pkg.go.dev/time#ParseDuration). Must be positive.
Default: `3h` Check out [crontab.dev](https://crontab.dev/) for a nice explanation of the schedule.
Default: `* */6 * * * *` (every 6 hours)
### `BACKUP_MAX` ### `BACKUP_MAX`

View File

@ -15,7 +15,7 @@ import (
type conf struct { type conf struct {
Folder string // Backup folder _with_ trailing slash Folder string // Backup folder _with_ trailing slash
fullCurrentBackupPath string fullCurrentBackupPath string
Interval time.Duration Schedule string
MaxBackups int MaxBackups int
} }
@ -26,14 +26,14 @@ var (
const ( const (
envBackupFolder = `BACKUP_FOLDER` envBackupFolder = `BACKUP_FOLDER`
envInterval = `BACKUP_INTERVAL` envSchedule = `BACKUP_SCHEDULE`
envMax = `BACKUP_MAX` envMax = `BACKUP_MAX`
) )
func init() { func init() {
config = &conf{ config = &conf{
Folder: "/backups/", Folder: "/backups/",
Interval: time.Hour * 6, Schedule: "* */6 * * * *",
MaxBackups: 12, MaxBackups: 12,
} }
@ -46,14 +46,9 @@ func init() {
config.Folder = folder config.Folder = folder
} }
var err error schedule, has := os.LookupEnv(envSchedule)
interval, has := os.LookupEnv(envInterval)
if has { if has {
config.Interval, err = time.ParseDuration(interval) config.Schedule = schedule
if err != nil {
log.Fatalf("Invalid interval: %s\n", err)
}
} }
max, has := os.LookupEnv(envMax) max, has := os.LookupEnv(envMax)

1
go.mod
View File

@ -9,6 +9,7 @@ require (
github.com/gorilla/mux v1.8.0 // indirect github.com/gorilla/mux v1.8.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/morikuni/aec v1.0.0 // indirect github.com/morikuni/aec v1.0.0 // indirect
github.com/robfig/cron/v3 v3.0.1
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/grpc v1.40.0 // indirect google.golang.org/grpc v1.40.0 // indirect
) )

2
go.sum
View File

@ -515,6 +515,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

12
main.go
View File

@ -1,8 +1,8 @@
package main package main
import ( import (
"github.com/robfig/cron/v3"
"log" "log"
"time"
) )
func main() { func main() {
@ -11,7 +11,8 @@ func main() {
log.Fatalf("Could not create client: %s", err) log.Fatalf("Could not create client: %s", err)
} }
for { cr := cron.New()
_, err = cr.AddFunc(config.Schedule, func() {
updateFullBackupPath() updateFullBackupPath()
containers, err := getContainers(c) containers, err := getContainers(c)
@ -32,8 +33,9 @@ func main() {
} }
log.Println("Done.") log.Println("Done.")
log.Printf("Sleeping for %s\n", config.Interval) })
if err != nil {
time.Sleep(config.Interval) log.Fatalf("Could not create cron job: %s\n", err)
} }
cr.Start()
} }