mirror of
https://github.com/offen/docker-volume-backup.git
synced 2026-04-21 16:12:40 +02:00
Compare commits
6 Commits
v2.37.0
...
v2.37.0-al
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
abfeb93628 | ||
|
|
cb31554791 | ||
|
|
749ca85db9 | ||
|
|
d14e826529 | ||
|
|
21191d601a | ||
|
|
d642a60c4d |
@@ -48,12 +48,7 @@ func loadEnvVars() (*Config, error) {
|
|||||||
return loadConfig(os.LookupEnv)
|
return loadConfig(os.LookupEnv)
|
||||||
}
|
}
|
||||||
|
|
||||||
type configFile struct {
|
func loadEnvFiles(directory string) ([]*Config, error) {
|
||||||
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) {
|
||||||
@@ -62,7 +57,7 @@ func loadEnvFiles(directory string) ([]configFile, 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
cs := []configFile{}
|
var cs = make([]*Config, 0)
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if item.IsDir() {
|
if item.IsDir() {
|
||||||
continue
|
continue
|
||||||
@@ -80,7 +75,7 @@ func loadEnvFiles(directory string) ([]configFile, 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, configFile{config: c, name: item.Name()})
|
cs = append(cs, c)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cs, nil
|
return cs, nil
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
// 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
|
|
||||||
}
|
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/robfig/cron/v3"
|
"github.com/robfig/cron/v3"
|
||||||
@@ -123,7 +122,7 @@ func runScript(c *Config) (err error) {
|
|||||||
return runErr
|
return runErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *command) runInForeground(profileCronExpression string) error {
|
func (c *command) runInForeground() error {
|
||||||
cr := cron.New(
|
cr := cron.New(
|
||||||
cron.WithParser(
|
cron.WithParser(
|
||||||
cron.NewParser(
|
cron.NewParser(
|
||||||
@@ -132,11 +131,11 @@ func (c *command) runInForeground(profileCronExpression string) error {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
addJob := func(config *Config, name string) error {
|
addJob := func(config *Config) 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 script on schedule %s",
|
"Now running schedule %s",
|
||||||
config.BackupCronExpression,
|
config.BackupCronExpression,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
@@ -154,14 +153,7 @@ func (c *command) runInForeground(profileCronExpression string) 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,7 +167,7 @@ func (c *command) runInForeground(profileCronExpression string) 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, "from environment")
|
err = addJob(c)
|
||||||
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)
|
||||||
}
|
}
|
||||||
@@ -183,32 +175,13 @@ func (c *command) runInForeground(profileCronExpression string) 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.config, config.name)
|
err = addJob(config)
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if profileCronExpression != "" {
|
|
||||||
if _, err := cr.AddFunc(profileCronExpression, func() {
|
|
||||||
memStats := runtime.MemStats{}
|
|
||||||
runtime.ReadMemStats(&memStats)
|
|
||||||
c.logger.Info(
|
c.logger.Info(
|
||||||
"Collecting runtime information",
|
fmt.Sprintf("Successfully scheduled backup with expression %s", config.BackupCronExpression),
|
||||||
"num_goroutines",
|
|
||||||
runtime.NumGoroutine(),
|
|
||||||
"memory_heap_alloc",
|
|
||||||
formatBytes(memStats.HeapAlloc, false),
|
|
||||||
"memory_heap_inuse",
|
|
||||||
formatBytes(memStats.HeapInuse, false),
|
|
||||||
"memory_heap_sys",
|
|
||||||
formatBytes(memStats.HeapSys, false),
|
|
||||||
"memory_heap_objects",
|
|
||||||
memStats.HeapObjects,
|
|
||||||
)
|
)
|
||||||
}); err != nil {
|
|
||||||
return fmt.Errorf("runInForeground: error adding profiling job: %w", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,12 +210,11 @@ func (c *command) runAsCommand() error {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
foreground := flag.Bool("foreground", false, "run the tool in the foreground")
|
foreground := flag.Bool("foreground", false, "run the tool in the foreground")
|
||||||
profile := flag.String("profile", "", "collect runtime metrics and log them periodically on the given cron expression")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
c := newCommand()
|
c := newCommand()
|
||||||
if *foreground {
|
if *foreground {
|
||||||
c.must(c.runInForeground(*profile))
|
c.must(c.runInForeground())
|
||||||
} else {
|
} else {
|
||||||
c.must(c.runAsCommand())
|
c.must(c.runAsCommand())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -112,12 +112,6 @@ func newScript(c *Config) (*script, error) {
|
|||||||
return nil, fmt.Errorf("newScript: failed to create docker client")
|
return nil, fmt.Errorf("newScript: failed to create docker client")
|
||||||
}
|
}
|
||||||
s.cli = cli
|
s.cli = cli
|
||||||
s.registerHook(hookLevelPlumbing, func(err error) error {
|
|
||||||
if err := s.cli.Close(); err != nil {
|
|
||||||
return fmt.Errorf("newScript: failed to close docker client: %w", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logFunc := func(logType storage.LogLevel, context string, msg string, params ...any) {
|
logFunc := func(logType storage.LogLevel, context string, msg string, params ...any) {
|
||||||
|
|||||||
Reference in New Issue
Block a user