From 4c804944333f70e9d6465ee99c51ca93fb222545 Mon Sep 17 00:00:00 2001 From: Frederik Ring Date: Sun, 22 Aug 2021 21:06:51 +0200 Subject: [PATCH] use go native strftime version --- README.md | 2 +- cmd/backup/main.go | 14 ++++++-------- entrypoint.sh | 2 +- go.mod | 3 ++- go.sum | 2 ++ 5 files changed, 12 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 218c1ed..65a3ace 100644 --- a/README.md +++ b/README.md @@ -79,7 +79,7 @@ AWS_S3_BUCKET_NAME="" # that is expected to be bigger than the maximum difference of backups. # Valid values have a suffix of (s)econds, (m)inutes or (h)ours. -# BACKUP_PRUNING_LEEWAY="10m" +# BACKUP_PRUNING_LEEWAY="1m" # In case your target bucket or directory contains other files than the ones # managed by this container, you can limit the scope of rotation by setting diff --git a/cmd/backup/main.go b/cmd/backup/main.go index 16d7d72..5453f72 100644 --- a/cmd/backup/main.go +++ b/cmd/backup/main.go @@ -11,11 +11,9 @@ import ( "io" "io/ioutil" "os" - "os/exec" "path" "path/filepath" "strconv" - "strings" "time" "github.com/docker/docker/api/types" @@ -23,6 +21,7 @@ import ( "github.com/docker/docker/api/types/swarm" "github.com/docker/docker/client" "github.com/joho/godotenv" + "github.com/leekchan/timeutil" minio "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/sirupsen/logrus" @@ -54,6 +53,7 @@ type script struct { archive string sources string passphrase string + now time.Time } // lock opens a lockfile at the given location, keeping it locked until the @@ -119,6 +119,8 @@ func (s *script) init() error { s.archive = os.Getenv("BACKUP_ARCHIVE") s.sources = os.Getenv("BACKUP_SOURCES") s.passphrase = os.Getenv("GPG_PASSPHRASE") + + s.now = time.Now() return nil } @@ -237,11 +239,7 @@ func (s *script) stopContainersAndRun(thunk func() error) error { // takeBackup creates a tar archive of the configured backup location and // saves it to disk. func (s *script) takeBackup() error { - outBytes, err := exec.Command("date", fmt.Sprintf("+%s", s.file)).Output() - if err != nil { - return fmt.Errorf("takeBackup: error formatting filename template: %w", err) - } - s.file = strings.TrimSpace(string(outBytes)) + s.file = timeutil.Strftime(&s.now, s.file) if err := targz.Compress(s.sources, s.file); err != nil { return fmt.Errorf("takeBackup: error compressing backup folder: %w", err) } @@ -345,7 +343,7 @@ func (s *script) pruneOldBackups() error { time.Sleep(sleepFor) s.logger.Infof("Trying to prune backups older than %d days now.", retentionDays) - deadline := time.Now().AddDate(0, 0, -retentionDays) + deadline := s.now.AddDate(0, 0, -retentionDays) if s.bucket != "" { candidates := s.mc.ListObjects(s.ctx, s.bucket, minio.ListObjectsOptions{ diff --git a/entrypoint.sh b/entrypoint.sh index 2469ee7..1258c08 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -17,7 +17,7 @@ BACKUP_FILENAME="${BACKUP_FILENAME:-backup-%Y-%m-%dT%H-%M-%S.tar.gz}" BACKUP_ARCHIVE="${BACKUP_ARCHIVE:-/archive}" BACKUP_RETENTION_DAYS="${BACKUP_RETENTION_DAYS:-}" -BACKUP_PRUNING_LEEWAY="${BACKUP_PRUNING_LEEWAY:-10m}" +BACKUP_PRUNING_LEEWAY="${BACKUP_PRUNING_LEEWAY:-1m}" BACKUP_PRUNING_PREFIX="${BACKUP_PRUNING_PREFIX:-}" BACKUP_STOP_CONTAINER_LABEL="${BACKUP_STOP_CONTAINER_LABEL:-true}" diff --git a/go.mod b/go.mod index 771befb..8ccfd39 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,9 @@ go 1.17 require ( github.com/docker/docker v20.10.8+incompatible github.com/joho/godotenv v1.3.0 + github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d github.com/minio/minio-go/v7 v7.0.12 + github.com/sirupsen/logrus v1.8.1 github.com/walle/targz v0.0.0-20140417120357-57fe4206da5a golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 ) @@ -32,7 +34,6 @@ require ( github.com/opencontainers/image-spec v1.0.1 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rs/xid v1.2.1 // indirect - github.com/sirupsen/logrus v1.8.1 // indirect golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect golang.org/x/text v0.3.4 // indirect diff --git a/go.sum b/go.sum index 6e3eade..1bb1be4 100644 --- a/go.sum +++ b/go.sum @@ -401,6 +401,8 @@ github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d h1:2puqoOQwi3Ai1oznMOsFIbifm6kIfJaLLyYzWD4IzTs= +github.com/leekchan/timeutil v0.0.0-20150802142658-28917288c48d/go.mod h1:hO90vCP2x3exaSH58BIAowSKvV+0OsY21TtzuFGHON4= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=