replace zfsnap for snapshot taking

This commit is contained in:
shoopea 2021-11-14 12:21:22 +08:00
parent 119d069083
commit d9c3eac4b6
6 changed files with 39 additions and 96 deletions

36
app.go
View File

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

23
box.go
View File

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

View File

@ -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() {

1
ctx.go Normal file
View File

@ -0,0 +1 @@
package main

44
ssh.go
View File

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

View File

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