Skip to content

Commit f76d4fd

Browse files
authored
Add optional jittering before any backup actions (#654)
* NEW: Add optional jittering before backing up * Removed deprecated rand seeding * Updated BACKUP_JITTER config option doc * Moved jittering logic from script init to runScript * Changed formatting of jittering log message
1 parent bebe348 commit f76d4fd

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

cmd/backup/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type Config struct {
3737
BackupLatestSymlink string `split_words:"true"`
3838
BackupArchive string `split_words:"true" default:"/archive"`
3939
BackupCronExpression string `split_words:"true" default:"@daily"`
40+
BackupJitter time.Duration `split_words:"true" default:"0s"`
4041
BackupRetentionDays int32 `split_words:"true" default:"-1"`
4142
BackupPruningLeeway time.Duration `split_words:"true" default:"1m"`
4243
BackupPruningPrefix string `split_words:"true"`

cmd/backup/run_script.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package main
66
import (
77
"errors"
88
"fmt"
9+
"math/rand"
910
"runtime/debug"
11+
"time"
1012

1113
"github.com/offen/docker-volume-backup/internal/errwrap"
1214
)
@@ -51,6 +53,15 @@ func runScript(c *Config) (err error) {
5153
}
5254
}()
5355

56+
if s.c != nil && s.c.BackupJitter > 0 {
57+
max := s.c.BackupJitter
58+
delay := time.Duration(rand.Int63n(int64(max) + 1))
59+
if delay > 0 {
60+
s.logger.Info(fmt.Sprintf("Applying startup jitter of %v", delay))
61+
time.Sleep(delay)
62+
}
63+
}
64+
5465
if initErr := s.init(); initErr != nil {
5566
err = errwrap.Wrap(initErr, "error instantiating script")
5667
return

docs/reference/index.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ The values for each key currently match its default.
4747
4848
# ---
4949
50+
# Optional startup delay ("jitter") applied before each backup run.
51+
# The jitter introduces a random delay between 0 and the given duration,
52+
#
53+
# Set to "0s" or omit the variable to disable jitter completely.
54+
# Default = "0s". In case you need to adjust this value, supply a duration
55+
# value as per https://pkg.go.dev/time#ParseDuration to `BACKUP_JITTER`.
56+
#
57+
# BACKUP_JITTER="0s"
58+
59+
# ---
60+
5061
# The compression algorithm used in conjunction with tar.
5162
# Valid options are: "gz" (Gzip), "zst" (Zstd) or "none" (tar only).
5263
# Default is "gz". Note that the selection affects the file extension.

0 commit comments

Comments
 (0)