From d9c3eac4b62ac950777ac7438979b977d3f8a6db Mon Sep 17 00:00:00 2001 From: shoopea Date: Sun, 14 Nov 2021 12:21:22 +0800 Subject: [PATCH] replace zfsnap for snapshot taking --- app.go | 36 +++++++----------------------------- box.go | 23 ++++++++++++++--------- config.go | 23 +++++++++++++---------- ctx.go | 1 + ssh.go | 44 -------------------------------------------- version.go | 8 ++++---- 6 files changed, 39 insertions(+), 96 deletions(-) create mode 100644 ctx.go diff --git a/app.go b/app.go index 104584b..aec1002 100644 --- a/app.go +++ b/app.go @@ -16,16 +16,6 @@ type AppConfig struct { After map[string]Location `json:"after"` } -func (a AppConfig) getTime() time.Time { - for _, v := range a.Sources { - return cfg.Box[v.Box()].GetTime() - } - for _, v := range a.Destinations { - return cfg.Box[v.Box()].GetTime() - } - return time.Now() -} - func (a AppConfig) getSchedule() (string, error) { var schedule string if *debugFlag { @@ -123,9 +113,8 @@ func (a AppConfig) needYearlySnapshot() bool { } // finding an eligible timestamp - now := a.getTime() for t, _ := range timeTotal { - if t.Year() == now.Year() { + if t.Year() == cfg.Now.Year() { return false } } @@ -185,9 +174,8 @@ func (a AppConfig) needMonthlySnapshot() bool { } // finding an eligible timestamp - now := a.getTime() for t, _ := range timeTotal { - if t.Year() == now.Year() && t.Month() == now.Month() { + if t.Year() == cfg.Now.Year() && t.Month() == cfg.Now.Month() { return false } } @@ -247,8 +235,7 @@ func (a AppConfig) needWeeklySnapshot() bool { } // finding an eligible timestamp - now := a.getTime() - nowYear, nowWeek := now.ISOWeek() + nowYear, nowWeek := cfg.Now.ISOWeek() for t, _ := range timeTotal { snapYear, snapWeek := t.ISOWeek() if nowYear == snapYear && nowWeek == snapWeek { @@ -311,9 +298,8 @@ func (a AppConfig) needDailySnapshot() bool { } // finding an eligible timestamp - now := a.getTime() for t, _ := range timeTotal { - if t.Year() == now.Year() && t.Month() == now.Month() && t.Day() == now.Day() { + if t.Year() == cfg.Now.Year() && t.Month() == cfg.Now.Month() && t.Day() == cfg.Now.Day() { return false } } @@ -373,9 +359,8 @@ func (a AppConfig) needHourlySnapshot() bool { } // finding an eligible timestamp - now := a.getTime() for t, _ := range timeTotal { - if t.Year() == now.Year() && t.Month() == now.Month() && t.Day() == now.Day() && t.Hour() == now.Hour() { + if t.Year() == cfg.Now.Year() && t.Month() == cfg.Now.Month() && t.Day() == cfg.Now.Day() && t.Hour() == cfg.Now.Hour() { return false } } @@ -455,18 +440,11 @@ func (a AppConfig) TakeSnapshot(schedule string) error { log.Printf("AppConfig.TakeSnapshot : %s : Start %s", a.Name, schedule) } - takeSnapshot := make(map[string]string) for _, v := range a.Sources { - takeSnapshot[v.Box()] = takeSnapshot[v.Box()] + " " + v.Path() - } - for k, v := range takeSnapshot { - if *debugFlag { - log.Printf("AppConfig.TakeSnapshot : %s : taking snapshot on %s for %s", a.Name, k, v) - } - err := cfg.Box[k].SSHExec("zfsnap snapshot -p " + schedule + "- -a " + cfg.Zfsnap[schedule] + v) + err := cfg.Box[v.Box()].ZFSTakeSnapshot(schedule, v.Path()) if err != nil { if *debugFlag { - log.Printf("AppConfig.TakeSnapshot : %s : Error executing zfsnap on %s", a.Name, k) + log.Printf("AppConfig.TakeSnapshot : %s : ZFSTakeSnapshot", a.Name) } return err } diff --git a/box.go b/box.go index f784598..84f1551 100644 --- a/box.go +++ b/box.go @@ -1,11 +1,15 @@ package main -import "time" +import ( + "fmt" + "log" +) type Box struct { Addr string `json:"addr"` User string `json:"user"` Key string `json:"key"` + Name string `json:"-"` ssh *SSHConfig } @@ -45,14 +49,15 @@ func (b *Box) ZFSCreateZFS(path string) (err error) { return b.ssh.createZFS(path) } -func (b *Box) GetTime() time.Time { - return b.ssh.now -} - -func (b *Box) UpdateTime() (err error) { - return b.ssh.getTime() -} - func (b *Box) SSHExec(cmd string) (err error) { return b.ssh.exec(cmd) } + +func (b *Box) ZFSTakeSnapshot(schedule, path string) (err error) { + if *debugFlag { + log.Printf("Box.ZFSTakeSnapshot : %s : taking snapshot on %s for %s", b.Name, path, schedule) + } + timestamp := cfg.Now.Format("2006-01-02_15.04.05") + name := fmt.Sprintf("%s-%s--%s", schedule, timestamp, cfg.Zfsnap[schedule]) + return b.ssh.exec("zfs snapshot " + path + "@" + name) +} diff --git a/config.go b/config.go index 1bbfa13..6011234 100644 --- a/config.go +++ b/config.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "log" "regexp" + "time" "golang.org/x/crypto/ssh" ) @@ -16,6 +17,7 @@ type Config struct { Box map[string]*Box `json:"box"` Apps []AppConfig `json:apps` Timezone string `json:"timezone"` + Now time.Time `json:"-"` } //Load config from file @@ -39,7 +41,18 @@ func (c *Config) Load() error { return err } + l, err := time.LoadLocation(cfg.Timezone) + if err != nil { + if *debugFlag { + log.Printf("Config.Load : time.LoadLocation : %s", err) + } + return err + } + + c.Now = time.Now().In(l) + for k, v := range c.Box { + v.Name = k s := &SSHConfig{ logged: false, name: k, @@ -110,16 +123,6 @@ func (c *Config) Load() error { s.logged = true } - for _, box := range c.Box { - err = box.ssh.getTime() - if err != nil { - if *debugFlag { - log.Printf("Config.Load : ssh.getTime() : %s", err) - } - return err - } - } - for _, app := range c.Apps { for _, src := range app.Sources { if !src.Valid() { diff --git a/ctx.go b/ctx.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/ctx.go @@ -0,0 +1 @@ +package main diff --git a/ssh.go b/ssh.go index 4a2e777..f663f0a 100644 --- a/ssh.go +++ b/ssh.go @@ -6,7 +6,6 @@ import ( "fmt" "log" "strings" - "time" "golang.org/x/crypto/ssh" ) @@ -19,7 +18,6 @@ type SSHConfig struct { name string zfs map[string]string snapshot []Snapshot - now time.Time } func (s *SSHConfig) getLastSnapshot(path string) (Snapshot, error) { @@ -222,48 +220,6 @@ func (s *SSHConfig) isZFS(path string) bool { return false } -func (s *SSHConfig) getTime() error { - if *debugFlag { - log.Printf("SSHConfig.getTime : %s : Start", s.name) - } - - session, err := s.client.NewSession() - if err != nil { - if *debugFlag { - log.Printf("SSHConfig.getTime : %s : client.NewSession() : %s", s.name, err) - } - return err - } - - var b bytes.Buffer - session.Stdout = &b - - err = session.Run("TZ=\"" + cfg.Timezone + "\" date +\"%F %T\"") - if err != nil { - if *debugFlag { - log.Printf("SSHConfig.getTime : %s : session.Run() : %s", s.name, err) - } - return err - } - - s.now, err = time.Parse("2006-01-02 15:04:05\n", b.String()) - if err != nil { - if *debugFlag { - log.Printf("SSHConfig.getTime : %s : time.Parse() : %s", s.name, err) - } - return err - } - - if *debugFlag { - log.Printf("SSHConfig.getTime : %s : now is %s", s.name, s.now.String()) - } - - session.Close() - - return nil - -} - func (s *SSHConfig) exec(cmd string) error { if *debugFlag { log.Printf("SSHConfig.exec : %s : Start %s", s.name, cmd) diff --git a/version.go b/version.go index ac92a2b..4cce4ac 100644 --- a/version.go +++ b/version.go @@ -1,6 +1,6 @@ // Code generated by version.sh (@generated) DO NOT EDIT. package main -var githash = "0f4fca3" -var buildstamp = "2021-11-14_02:51:24" -var commits = "17" -var version = "0f4fca3-b17 - 2021-11-14_02:51:24" +var githash = "119d069" +var buildstamp = "2021-11-14_04:20:23" +var commits = "18" +var version = "119d069-b18 - 2021-11-14_04:20:23"