Skip to content

PostgreSQL backup tool with Point-In-Time-Recovery and restore verification

FIX (backups): Make backup after specified previous run if there are no backups yet

Rostislav Duginf3c55435dcaf
diff --git a/backend/internal/features/intervals/model.go b/backend/internal/features/intervals/model.go
index d5e8b7ea..08c11352 100644
--- a/backend/internal/features/intervals/model.go
+++ b/backend/internal/features/intervals/model.go
@@ -64,41 +64,34 @@ func (i *Interval) ShouldTriggerBackup(now time.Time, lastBackupTime *time.Time)
}
}
-// daily trigger: calendar-based if TimeOfDay set, otherwise next calendar day
+// daily trigger: honour the TimeOfDay slot and catch up the previous one
func (i *Interval) shouldTriggerDaily(now, lastBackup time.Time) bool {
- if i.TimeOfDay != nil {
- target, err := time.Parse("15:04", *i.TimeOfDay)
- if err == nil {
- todayTarget := time.Date(
- now.Year(),
- now.Month(),
- now.Day(),
- target.Hour(),
- target.Minute(),
- 0,
- 0,
- now.Location(),
- )
-
- // if it's past today's target time and we haven't backed up today
- if now.After(todayTarget) && !isSameDay(lastBackup, now) {
- return true
- }
+ if i.TimeOfDay == nil {
+ return !isSameDay(lastBackup, now)
+ }
- // if it's exactly the target time and we haven't backed up today
- if now.Equal(todayTarget) && !isSameDay(lastBackup, now) {
- return true
- }
+ t, err := time.Parse("15:04", *i.TimeOfDay)
+ if err != nil {
+ return false // malformed ⇒ play safe
+ }
- // if it's before today's target time, don't trigger yet
- if now.Before(todayTarget) {
- return false
- }
- }
+ // Today's scheduled slot (todayTgt)
+ todayTgt := time.Date(
+ now.Year(), now.Month(), now.Day(),
+ t.Hour(), t.Minute(), 0, 0, now.Location(),
+ )
+
+ // The last scheduled slot that should already have happened
+ var lastScheduled time.Time
+ if now.Before(todayTgt) {
+ lastScheduled = todayTgt.AddDate(0, 0, -1)
+ } else {
+ lastScheduled = todayTgt
}
- // no TimeOfDay: if it's a new calendar day
- return !isSameDay(lastBackup, now)
+ // Fire when we are past that slot AND no backup has been taken since it
+ return (now.After(lastScheduled) || now.Equal(lastScheduled)) &&
+ lastBackup.Before(lastScheduled)
}
// weekly trigger: on specified weekday/calendar week, otherwise ≥7 days
diff --git a/backend/internal/features/intervals/model_test.go b/backend/internal/features/intervals/model_test.go
index 00f0fb9f..81b69eaa 100644
--- a/backend/internal/features/intervals/model_test.go
+++ b/backend/internal/features/intervals/model_test.go
@@ -104,6 +104,40 @@ func TestInterval_ShouldTriggerBackup_Daily(t *testing.T) {
assert.True(t, should)
},
)
+
+ t.Run(
+ "Manual backup before scheduled time should not prevent scheduled backup",
+ func(t *testing.T) {
+ timeOfDay := "21:00"
+ interval := &Interval{
+ ID: uuid.New(),
+ Interval: IntervalDaily,
+ TimeOfDay: &timeOfDay,
+ }
+
+ manual := time.Date(2025, 6, 6, 16, 17, 0, 0, time.UTC) // manual earlier
+ scheduled := time.Date(2025, 6, 6, 21, 0, 0, 0, time.UTC) // scheduled time
+
+ should := interval.ShouldTriggerBackup(scheduled, &manual)
+ assert.True(t, should, "scheduled run should trigger even after earlier manual backup")
+ },
+ )
+
+ t.Run("Catch up previous time slot", func(t *testing.T) {
+ timeOfDay := "21:00"
+ interval := &Interval{
+ ID: uuid.New(),
+ Interval: IntervalDaily,
+ TimeOfDay: &timeOfDay,
+ }
+
+ // It's June-07 15:00 UTC, yesterday's scheduled backup was missed
+ now := time.Date(2025, 6, 7, 15, 0, 0, 0, time.UTC)
+ lastBackup := time.Date(2025, 6, 6, 16, 0, 0, 0, time.UTC) // before yesterday's 21:00
+
+ should := interval.ShouldTriggerBackup(now, &lastBackup)
+ assert.True(t, should, "should catch up missed 21:00 backup the next day at 15:00")
+ })
}
func TestInterval_ShouldTriggerBackup_Weekly(t *testing.T) {