mirror of
https://github.com/offen/docker-volume-backup.git
synced 2026-04-22 16:42:40 +02:00
Compare commits
1 Commits
v2.37.0-al
...
v2.37.0-al
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7cfea62933 |
@@ -48,7 +48,12 @@ func loadEnvVars() (*Config, error) {
|
|||||||
return loadConfig(os.LookupEnv)
|
return loadConfig(os.LookupEnv)
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadEnvFiles(directory string) ([]*Config, error) {
|
type configFile struct {
|
||||||
|
name string
|
||||||
|
config *Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadEnvFiles(directory string) ([]configFile, error) {
|
||||||
items, err := os.ReadDir(directory)
|
items, err := os.ReadDir(directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
@@ -57,7 +62,7 @@ func loadEnvFiles(directory string) ([]*Config, error) {
|
|||||||
return nil, fmt.Errorf("loadEnvFiles: failed to read files from env directory: %w", err)
|
return nil, fmt.Errorf("loadEnvFiles: failed to read files from env directory: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var cs = make([]*Config, 0)
|
cs := []configFile{}
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if item.IsDir() {
|
if item.IsDir() {
|
||||||
continue
|
continue
|
||||||
@@ -75,7 +80,7 @@ func loadEnvFiles(directory string) ([]*Config, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("loadEnvFiles: error loading config from file %s: %w", p, err)
|
return nil, fmt.Errorf("loadEnvFiles: error loading config from file %s: %w", p, err)
|
||||||
}
|
}
|
||||||
cs = append(cs, c)
|
cs = append(cs, configFile{config: c, name: item.Name()})
|
||||||
}
|
}
|
||||||
|
|
||||||
return cs, nil
|
return cs, nil
|
||||||
|
|||||||
29
cmd/backup/cron.go
Normal file
29
cmd/backup/cron.go
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// Copyright 2024 - Offen Authors <hioffen@posteo.de>
|
||||||
|
// SPDX-License-Identifier: MPL-2.0
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/robfig/cron/v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// checkCronSchedule detects whether the given cron expression will actually
|
||||||
|
// ever be executed or not.
|
||||||
|
func checkCronSchedule(expression string) (ok bool) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
sched, err := cron.ParseStandard(expression)
|
||||||
|
if err != nil {
|
||||||
|
ok = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
sched.Next(now) // panics when the cron would never run
|
||||||
|
ok = true
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -131,11 +131,11 @@ func (c *command) runInForeground() error {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
addJob := func(config *Config) error {
|
addJob := func(config *Config, name string) error {
|
||||||
if _, err := cr.AddFunc(config.BackupCronExpression, func() {
|
if _, err := cr.AddFunc(config.BackupCronExpression, func() {
|
||||||
c.logger.Info(
|
c.logger.Info(
|
||||||
fmt.Sprintf(
|
fmt.Sprintf(
|
||||||
"Now running schedule %s",
|
"Now running script on schedule %s",
|
||||||
config.BackupCronExpression,
|
config.BackupCronExpression,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -153,7 +153,14 @@ func (c *command) runInForeground() error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return fmt.Errorf("addJob: error adding schedule %s: %w", config.BackupCronExpression, err)
|
return fmt.Errorf("addJob: error adding schedule %s: %w", config.BackupCronExpression, err)
|
||||||
}
|
}
|
||||||
c.logger.Info(fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression))
|
|
||||||
|
c.logger.Info(fmt.Sprintf("Successfully scheduled backup %s with expression %s", name, config.BackupCronExpression))
|
||||||
|
if ok := checkCronSchedule(config.BackupCronExpression); !ok {
|
||||||
|
c.logger.Warn(
|
||||||
|
fmt.Sprintf("Scheduled cron expression %s will never run, is this intentional?", config.BackupCronExpression),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,7 +174,7 @@ func (c *command) runInForeground() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("runInForeground: could not load config from environment variables: %w", err)
|
return fmt.Errorf("runInForeground: could not load config from environment variables: %w", err)
|
||||||
} else {
|
} else {
|
||||||
err = addJob(c)
|
err = addJob(c, "from environment")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("runInForeground: error adding job from env: %w", err)
|
return fmt.Errorf("runInForeground: error adding job from env: %w", err)
|
||||||
}
|
}
|
||||||
@@ -175,13 +182,10 @@ func (c *command) runInForeground() error {
|
|||||||
} else {
|
} else {
|
||||||
c.logger.Info("/etc/dockervolumebackup/conf.d was found, using configuration files from this directory.")
|
c.logger.Info("/etc/dockervolumebackup/conf.d was found, using configuration files from this directory.")
|
||||||
for _, config := range cs {
|
for _, config := range cs {
|
||||||
err = addJob(config)
|
err = addJob(config.config, config.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("runInForeground: error adding jobs from conf files: %w", err)
|
return fmt.Errorf("runInForeground: error adding jobs from conf files: %w", err)
|
||||||
}
|
}
|
||||||
c.logger.Info(
|
|
||||||
fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user