diff --git a/cmd/backup/config.go b/cmd/backup/config.go index 08232a8..dae3289 100644 --- a/cmd/backup/config.go +++ b/cmd/backup/config.go @@ -37,6 +37,7 @@ type Config struct { BackupLatestSymlink string `split_words:"true"` BackupArchive string `split_words:"true" default:"/archive"` BackupCronExpression string `split_words:"true" default:"@daily"` + BackupJitter time.Duration `split_words:"true" default:"0s"` BackupRetentionDays int32 `split_words:"true" default:"-1"` BackupPruningLeeway time.Duration `split_words:"true" default:"1m"` BackupPruningPrefix string `split_words:"true"` diff --git a/cmd/backup/run_script.go b/cmd/backup/run_script.go index 020cb2f..a99e79d 100644 --- a/cmd/backup/run_script.go +++ b/cmd/backup/run_script.go @@ -6,7 +6,9 @@ package main import ( "errors" "fmt" + "math/rand" "runtime/debug" + "time" "github.com/offen/docker-volume-backup/internal/errwrap" ) @@ -51,6 +53,15 @@ func runScript(c *Config) (err error) { } }() + if s.c != nil && s.c.BackupJitter > 0 { + max := s.c.BackupJitter + delay := time.Duration(rand.Int63n(int64(max) + 1)) + if delay > 0 { + s.logger.Info(fmt.Sprintf("Applying startup jitter of %v", delay)) + time.Sleep(delay) + } + } + if initErr := s.init(); initErr != nil { err = errwrap.Wrap(initErr, "error instantiating script") return diff --git a/docs/reference/index.md b/docs/reference/index.md index 8a423c7..f3290a5 100644 --- a/docs/reference/index.md +++ b/docs/reference/index.md @@ -47,6 +47,17 @@ The values for each key currently match its default. # --- +# Optional startup delay ("jitter") applied before each backup run. +# The jitter introduces a random delay between 0 and the given duration, +# +# Set to "0s" or omit the variable to disable jitter completely. +# Default = "0s". In case you need to adjust this value, supply a duration +# value as per https://pkg.go.dev/time#ParseDuration to `BACKUP_JITTER`. +# +# BACKUP_JITTER="0s" + +# --- + # The compression algorithm used in conjunction with tar. # Valid options are: "gz" (Gzip), "zst" (Zstd) or "none" (tar only). # Default is "gz". Note that the selection affects the file extension.