From acee286a19330664e177e13332e0ac40115f491c Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Tue, 7 Mar 2023 21:15:54 +0100 Subject: [PATCH] trigger a backup check as soon as its booted, move util stuff separately for testing --- main.go | 24 +++++++++++++++++++----- restic/config.go | 10 +++++----- restic/util.go | 12 ++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 restic/util.go diff --git a/main.go b/main.go index fe534ec..68722fc 100644 --- a/main.go +++ b/main.go @@ -70,15 +70,26 @@ func updateSnapshots(cnf *restic.Config, mnu *resticmenu) { return } + updateLatestSnapshotTitle(mnu) + updateFutureSnapshotTitle(cnf, mnu) + mnu.backupNowSucceeded() +} + +func updateLatestSnapshotTitle(mnu *resticmenu) { snapshot := wrapper.LastSnapshot() - msg := strconv.Itoa(len(wrapper.LatestSnapshots)) + " snapshots. Next in " + strconv.Itoa(cnf.BackupTimeInHours) + " hour(s)" + mnu.latestSnapshot.SetTitle("Latest: " + snapshot.Id + " @ " + snapshot.ShortTime()) +} + +func updateFutureSnapshotTitle(cnf *restic.Config, mnu *resticmenu) { + msg := strconv.Itoa(len(wrapper.LatestSnapshots)) + " snapshots. " if isBackupNeeded(cnf) { - msg = "⚠️ Overdue - " + msg + msg += "⚠️ Overdue" + } else { + next := wrapper.LastSnapshot().Time.Add(cnf.BackupTimeInDuration()) + msg += "Next @ " + next.Format(restic.ShortTimeFormat) } - mnu.latestSnapshot.SetTitle("Latest: " + snapshot.Id + " @ " + snapshot.ShortTime()) mnu.nextSnapshot.SetTitle(msg) - mnu.backupNowSucceeded() } func isBackupNeeded(cnf *restic.Config) bool { @@ -119,13 +130,16 @@ func onSystrayReady() { mnu := buildMenu() - go updateSnapshots(cnf, mnu) backupCheckTime := make(chan bool, 1) hourlyBackupCheckFn := func() { time.Sleep(1 * time.Hour) backupCheckTime <- true } go hourlyBackupCheckFn() + go func() { + updateSnapshots(cnf, mnu) + backupCheckTime <- true // As soon as the snapshots are loaded,trigger a check. + }() for { select { diff --git a/restic/config.go b/restic/config.go index 2f6ebc7..9b877a0 100644 --- a/restic/config.go +++ b/restic/config.go @@ -6,6 +6,7 @@ import ( "fmt" "github.com/rs/zerolog/log" "os" + "time" ) type Config struct { @@ -14,16 +15,15 @@ type Config struct { BackupTimeInHours int `json:"backupTimeInHours"` } +func (c *Config) BackupTimeInDuration() time.Duration { + return time.Duration(c.BackupTimeInHours) * time.Hour +} + const ( DefaultBackupTimeInHours int = 24 ShortTimeFormat string = "2006-01-02T15:04:05" ) -var home, _ = os.UserHomeDir() -var executable, _ = os.Executable() -var isDev = os.Getenv("RESTICTRAY_DEV") -var configDir = home + "/.resitc/" - func IsDev() bool { return isDev != "" } diff --git a/restic/util.go b/restic/util.go new file mode 100644 index 0000000..adda4f7 --- /dev/null +++ b/restic/util.go @@ -0,0 +1,12 @@ +package restic + +import ( + "os" + "time" +) + +var home, _ = os.UserHomeDir() +var executable, _ = os.Executable() +var isDev = os.Getenv("RESTICTRAY_DEV") +var configDir = home + "/.restic/" +var timeNow = time.Now