trigger a backup check as soon as its booted, move util stuff separately for testing

This commit is contained in:
Wouter Groeneveld 2023-03-07 21:15:54 +01:00
parent 8f5b6c0727
commit acee286a19
3 changed files with 36 additions and 10 deletions

24
main.go
View File

@ -70,15 +70,26 @@ func updateSnapshots(cnf *restic.Config, mnu *resticmenu) {
return return
} }
updateLatestSnapshotTitle(mnu)
updateFutureSnapshotTitle(cnf, mnu)
mnu.backupNowSucceeded()
}
func updateLatestSnapshotTitle(mnu *resticmenu) {
snapshot := wrapper.LastSnapshot() 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) { 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.nextSnapshot.SetTitle(msg)
mnu.backupNowSucceeded()
} }
func isBackupNeeded(cnf *restic.Config) bool { func isBackupNeeded(cnf *restic.Config) bool {
@ -119,13 +130,16 @@ func onSystrayReady() {
mnu := buildMenu() mnu := buildMenu()
go updateSnapshots(cnf, mnu)
backupCheckTime := make(chan bool, 1) backupCheckTime := make(chan bool, 1)
hourlyBackupCheckFn := func() { hourlyBackupCheckFn := func() {
time.Sleep(1 * time.Hour) time.Sleep(1 * time.Hour)
backupCheckTime <- true backupCheckTime <- true
} }
go hourlyBackupCheckFn() go hourlyBackupCheckFn()
go func() {
updateSnapshots(cnf, mnu)
backupCheckTime <- true // As soon as the snapshots are loaded,trigger a check.
}()
for { for {
select { select {

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"os" "os"
"time"
) )
type Config struct { type Config struct {
@ -14,16 +15,15 @@ type Config struct {
BackupTimeInHours int `json:"backupTimeInHours"` BackupTimeInHours int `json:"backupTimeInHours"`
} }
func (c *Config) BackupTimeInDuration() time.Duration {
return time.Duration(c.BackupTimeInHours) * time.Hour
}
const ( const (
DefaultBackupTimeInHours int = 24 DefaultBackupTimeInHours int = 24
ShortTimeFormat string = "2006-01-02T15:04:05" 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 { func IsDev() bool {
return isDev != "" return isDev != ""
} }

12
restic/util.go Normal file
View File

@ -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