From f7df39f916babb83fde65883761bd7c8524dbb7f Mon Sep 17 00:00:00 2001 From: shoopea Date: Mon, 11 Oct 2021 22:47:03 +0800 Subject: [PATCH] functionnal --- backup.go | 79 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 31 deletions(-) diff --git a/backup.go b/backup.go index 7b00859..a9875c7 100644 --- a/backup.go +++ b/backup.go @@ -85,6 +85,11 @@ func (s Snapshot) Name() string { return s2[1] } +func (s Snapshot) Append(path string) Snapshot { + s2 := strings.Split(string(s), `@`) + return Snapshot(s2[0] + "/" + path + "@" + s2[1]) +} + //Load config from file func (c *Config) Load() error { b, err := ioutil.ReadFile(*cfgFile) @@ -233,6 +238,9 @@ func (c *Config) Close() error { } func (s *SSHConfig) getLastSnapshot(path string) (Snapshot, error) { + if *debugFlag { + log.Printf("SSHConfig.getLastSnapshot : Start %s:%s (%d snapshots)", s.name, path, len(s.snapshot)) + } var last Snapshot for _, v := range s.snapshot { if v.Path() == path { @@ -250,6 +258,9 @@ func (s *SSHConfig) getLastSnapshot(path string) (Snapshot, error) { } func (s *SSHConfig) isLastSnapshot(snapshot Snapshot) bool { + if *debugFlag { + log.Printf("SSHConfig.isLastSnapshot : Start %s:%s", s.name, string(snapshot)) + } _, err := s.getNextSnapshot(snapshot) if err != nil { return true @@ -259,6 +270,9 @@ func (s *SSHConfig) isLastSnapshot(snapshot Snapshot) bool { } func (s *SSHConfig) getFirstSnapshot(path string) (Snapshot, error) { + if *debugFlag { + log.Printf("SSHConfig.getFirstSnapshot : Start %s:%s", s.name, path) + } var first Snapshot for _, v := range s.snapshot { if v.Path() == path { @@ -270,6 +284,9 @@ func (s *SSHConfig) getFirstSnapshot(path string) (Snapshot, error) { } func (s *SSHConfig) getNextSnapshot(snapshot Snapshot) (Snapshot, error) { + if *debugFlag { + log.Printf("SSHConfig.getNextSnapshot : Start %s:%s", s.name, string(snapshot)) + } var next Snapshot for id, v := range s.snapshot { if v == snapshot { @@ -531,6 +548,9 @@ func (a AppConfig) RunAppSchedule(schedule string) error { } refreshSnapshot[v.Box()] = true } + for _, v := range a.Destinations { + refreshSnapshot[v.Box()] = true + } for k, _ := range refreshSnapshot { if *debugFlag { log.Printf("RunAppSchedule : refreshing snapshots for source %s", k) @@ -548,7 +568,7 @@ func (a AppConfig) RunAppSchedule(schedule string) error { if *debugFlag { log.Printf("RunAppSchedule : Sending snapshots from %s to %s", string(src), string(dest)) } - dLastSnapshot, err := cfg.ssh[dest.Box()].getLastSnapshot(dest.Path() + "/" + src.Path()) + dLastSnapshot, err := cfg.ssh[dest.Box()].getLastSnapshot(dest.Path() + "/" + src.Box() + "/" + src.Path()) if err != nil { if *debugFlag { log.Printf("RunAppSchedule : No snapshot for %s on %s", string(src), dest.Box()) @@ -563,54 +583,51 @@ func (a AppConfig) RunAppSchedule(schedule string) error { if *debugFlag { log.Printf("RunAppSchedule : Initializing snapshot on %s from %s", dest.Box(), string(sFirstSnapshot)) } - sNextSnapshot, err := cfg.ssh[src.Box()].getNextSnapshot(sFirstSnapshot) + err = cfg.ssh[dest.Box()].exec("/usr/bin/ssh root@" + src.Box() + " /sbin/zfs send " + string(sFirstSnapshot) + " | /sbin/zfs recv -F " + dest.Path() + "/" + src.Box() + "/" + src.Path()) if err != nil { if *debugFlag { - log.Printf("RunAppSchedule : All snapshots sent for %s", string(src)) + log.Printf("RunAppSchedule : Initializing snapshot on %s from %s failed (%s)", dest.Box(), string(sFirstSnapshot), err) } - } else { + return err + } + var sCurrSnapshot Snapshot + sNextSnapshot := sFirstSnapshot + for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) { + sCurrSnapshot = sNextSnapshot + sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot) if *debugFlag { - log.Printf("RunAppSchedule : Sending %s to %s", string(sNextSnapshot), dest.Box()) + log.Printf("RunAppSchedule : Sending incrementally %s to %s", string(sNextSnapshot), dest.Box()) } - for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) { - sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot) + err = cfg.ssh[dest.Box()].exec("/usr/bin/ssh root@" + src.Box() + " /sbin/zfs send -I " + string(sCurrSnapshot) + " " + string(sNextSnapshot) + " | /sbin/zfs recv " + dest.Path() + "/" + src.Box() + "/" + src.Path()) + if err != nil { if *debugFlag { - log.Printf("RunAppSchedule : Sending %s to %s", string(sNextSnapshot), dest.Box()) + log.Printf("RunAppSchedule : Sending snapshot on %s from %s failed (%s)", dest.Box(), string(sNextSnapshot), err) } + return err } - if *debugFlag { - log.Printf("RunAppSchedule : All snapshots sent for %s", string(src)) - } + } + if *debugFlag { + log.Printf("RunAppSchedule : All snapshots sent for %s", string(src)) } } } else { if *debugFlag { log.Printf("RunAppSchedule : Last snapshot on %s is %s", dest.Box(), string(dLastSnapshot)) } - if !cfg.ssh[src.Box()].isLastSnapshot(dLastSnapshot) { - sNextSnapshot, err := cfg.ssh[src.Box()].getNextSnapshot(dLastSnapshot) + var sCurrSnapshot Snapshot + sNextSnapshot := Snapshot(string(dLastSnapshot)[len(string(dest))+2:]) + for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) { + sCurrSnapshot = sNextSnapshot + sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot) + if *debugFlag { + log.Printf("RunAppSchedule : Sending incrementally %s to %s", string(sNextSnapshot), dest.Box()) + } + err = cfg.ssh[dest.Box()].exec("/usr/bin/ssh root@" + src.Box() + " /sbin/zfs send -I " + string(sCurrSnapshot) + " " + string(sNextSnapshot) + " | /sbin/zfs recv " + dest.Path() + "/" + src.Box() + "/" + src.Path()) if err != nil { if *debugFlag { - log.Printf("RunAppSchedule : No snapshot for %s", string(src)) + log.Printf("RunAppSchedule : Sending snapshot on %s from %s failed (%s)", dest.Box(), string(sNextSnapshot), err) } return err - } else { - if *debugFlag { - log.Printf("RunAppSchedule : Sending %s to %s", string(sNextSnapshot), dest.Box()) - } - for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) { - sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot) - if *debugFlag { - log.Printf("RunAppSchedule : Sending %s to %s", string(sNextSnapshot), dest.Box()) - } - } - if *debugFlag { - log.Printf("RunAppSchedule : All snapshots sent for", string(src)) - } - } - } else { - if *debugFlag { - log.Printf("RunAppSchedule : Last snapshot sent for", string(src)) } } }