From 1532f02fb84941a6b5a16ecc4b363af21291cae8 Mon Sep 17 00:00:00 2001 From: pkonst Date: Sat, 6 Jul 2024 16:27:48 +0200 Subject: [PATCH 1/2] Fix pruning to work with safechars --- pkg/core/prune.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/core/prune.go b/pkg/core/prune.go index 3b4d10f5..7c9eb3f5 100644 --- a/pkg/core/prune.go +++ b/pkg/core/prune.go @@ -10,7 +10,7 @@ import ( ) // filenameRE is a regular expression to match a backup filename -var filenameRE = regexp.MustCompile(`^db_backup_(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z\.\w+$`) +var filenameRE = regexp.MustCompile(`^db_backup_(\d{4})-(\d{2})-(\d{2})T(\d{2})[:-](\d{2})[:-](\d{2})Z\.\w+$`) // Prune prune older backups func (e *Executor) Prune(opts PruneOptions) error { From 2f6c410037c476a6a880c0ea95a39a16a51f7f69 Mon Sep 17 00:00:00 2001 From: pkonst Date: Mon, 8 Jul 2024 19:55:45 +0200 Subject: [PATCH 2/2] Add tests for pruning of safe flenames --- pkg/core/prune_test.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/pkg/core/prune_test.go b/pkg/core/prune_test.go index cd3a4339..9593b225 100644 --- a/pkg/core/prune_test.go +++ b/pkg/core/prune_test.go @@ -52,7 +52,7 @@ func TestPrune(t *testing.T) { now := time.Date(2021, 1, 1, 0, 30, 0, 0, time.UTC) hoursAgo := []float32{0.25, 1, 2, 3, 24, 36, 48, 60, 72, 167, 168, 240, 336, 504, 576, 744, 720, 1000, 1440, 1800, 2160, 8760, 12000, 17520} // convert to filenames - var filenames []string + var filenames, safefilenames []string for _, h := range hoursAgo { // convert the time diff into a duration, do not forget the negative duration, err := time.ParseDuration(fmt.Sprintf("-%fh", h)) @@ -65,6 +65,8 @@ func TestPrune(t *testing.T) { // convert that into the filename filename := fmt.Sprintf("db_backup_%sZ.gz", relativeTime.Format("2006-01-02T15:04:05")) filenames = append(filenames, filename) + safefilename := fmt.Sprintf("db_backup_%sZ.gz", relativeTime.Format("2006-01-02T15-04-05")) + safefilenames = append(safefilenames, safefilename) } tests := []struct { name string @@ -83,6 +85,14 @@ func TestPrune(t *testing.T) { {"2 days", PruneOptions{Retention: "2d", Now: now}, filenames, filenames[0:6], nil}, // 3 weeks - file[13] is 504h+30m = 504.5h, so it should be pruned {"3 weeks", PruneOptions{Retention: "3w", Now: now}, filenames, filenames[0:13], nil}, + // repeat for safe file names + {"1 hour safe names", PruneOptions{Retention: "1h", Now: now}, safefilenames, safefilenames[0:1], nil}, + // 2 hours - file[2] is 2h+30m = 2.5h, so it should be pruned + {"2 hours safe names", PruneOptions{Retention: "2h", Now: now}, safefilenames, safefilenames[0:2], nil}, + // 2 days - file[6] is 48h+30m = 48.5h, so it should be pruned + {"2 days safe names", PruneOptions{Retention: "2d", Now: now}, safefilenames, safefilenames[0:6], nil}, + // 3 weeks - file[13] is 504h+30m = 504.5h, so it should be pruned + {"3 weeks safe names", PruneOptions{Retention: "3w", Now: now}, safefilenames, safefilenames[0:13], nil}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {