functionnal

This commit is contained in:
shoopea 2021-10-11 22:47:03 +08:00
parent c5b75c245c
commit f7df39f916

View File

@ -85,6 +85,11 @@ func (s Snapshot) Name() string {
return s2[1] 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 //Load config from file
func (c *Config) Load() error { func (c *Config) Load() error {
b, err := ioutil.ReadFile(*cfgFile) b, err := ioutil.ReadFile(*cfgFile)
@ -233,6 +238,9 @@ func (c *Config) Close() error {
} }
func (s *SSHConfig) getLastSnapshot(path string) (Snapshot, 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 var last Snapshot
for _, v := range s.snapshot { for _, v := range s.snapshot {
if v.Path() == path { if v.Path() == path {
@ -250,6 +258,9 @@ func (s *SSHConfig) getLastSnapshot(path string) (Snapshot, error) {
} }
func (s *SSHConfig) isLastSnapshot(snapshot Snapshot) bool { func (s *SSHConfig) isLastSnapshot(snapshot Snapshot) bool {
if *debugFlag {
log.Printf("SSHConfig.isLastSnapshot : Start %s:%s", s.name, string(snapshot))
}
_, err := s.getNextSnapshot(snapshot) _, err := s.getNextSnapshot(snapshot)
if err != nil { if err != nil {
return true return true
@ -259,6 +270,9 @@ func (s *SSHConfig) isLastSnapshot(snapshot Snapshot) bool {
} }
func (s *SSHConfig) getFirstSnapshot(path string) (Snapshot, error) { func (s *SSHConfig) getFirstSnapshot(path string) (Snapshot, error) {
if *debugFlag {
log.Printf("SSHConfig.getFirstSnapshot : Start %s:%s", s.name, path)
}
var first Snapshot var first Snapshot
for _, v := range s.snapshot { for _, v := range s.snapshot {
if v.Path() == path { if v.Path() == path {
@ -270,6 +284,9 @@ func (s *SSHConfig) getFirstSnapshot(path string) (Snapshot, error) {
} }
func (s *SSHConfig) getNextSnapshot(snapshot Snapshot) (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 var next Snapshot
for id, v := range s.snapshot { for id, v := range s.snapshot {
if v == snapshot { if v == snapshot {
@ -531,6 +548,9 @@ func (a AppConfig) RunAppSchedule(schedule string) error {
} }
refreshSnapshot[v.Box()] = true refreshSnapshot[v.Box()] = true
} }
for _, v := range a.Destinations {
refreshSnapshot[v.Box()] = true
}
for k, _ := range refreshSnapshot { for k, _ := range refreshSnapshot {
if *debugFlag { if *debugFlag {
log.Printf("RunAppSchedule : refreshing snapshots for source %s", k) log.Printf("RunAppSchedule : refreshing snapshots for source %s", k)
@ -548,7 +568,7 @@ func (a AppConfig) RunAppSchedule(schedule string) error {
if *debugFlag { if *debugFlag {
log.Printf("RunAppSchedule : Sending snapshots from %s to %s", string(src), string(dest)) 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 err != nil {
if *debugFlag { if *debugFlag {
log.Printf("RunAppSchedule : No snapshot for %s on %s", string(src), dest.Box()) 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 { if *debugFlag {
log.Printf("RunAppSchedule : Initializing snapshot on %s from %s", dest.Box(), string(sFirstSnapshot)) 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 err != nil {
if *debugFlag { 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
if *debugFlag {
log.Printf("RunAppSchedule : Sending %s to %s", string(sNextSnapshot), dest.Box())
} }
var sCurrSnapshot Snapshot
sNextSnapshot := sFirstSnapshot
for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) { for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) {
sCurrSnapshot = sNextSnapshot
sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot) sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot)
if *debugFlag { 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())
}
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 snapshot on %s from %s failed (%s)", dest.Box(), string(sNextSnapshot), err)
}
return err
} }
} }
if *debugFlag { if *debugFlag {
log.Printf("RunAppSchedule : All snapshots sent for %s", string(src)) log.Printf("RunAppSchedule : All snapshots sent for %s", string(src))
} }
} }
}
} else { } else {
if *debugFlag { if *debugFlag {
log.Printf("RunAppSchedule : Last snapshot on %s is %s", dest.Box(), string(dLastSnapshot)) log.Printf("RunAppSchedule : Last snapshot on %s is %s", dest.Box(), string(dLastSnapshot))
} }
if !cfg.ssh[src.Box()].isLastSnapshot(dLastSnapshot) { var sCurrSnapshot Snapshot
sNextSnapshot, err := cfg.ssh[src.Box()].getNextSnapshot(dLastSnapshot) sNextSnapshot := Snapshot(string(dLastSnapshot)[len(string(dest))+2:])
if err != nil {
if *debugFlag {
log.Printf("RunAppSchedule : No snapshot for %s", string(src))
}
return err
} else {
if *debugFlag {
log.Printf("RunAppSchedule : Sending %s to %s", string(sNextSnapshot), dest.Box())
}
for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) { for !cfg.ssh[src.Box()].isLastSnapshot(sNextSnapshot) {
sCurrSnapshot = sNextSnapshot
sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot) sNextSnapshot, err = cfg.ssh[src.Box()].getNextSnapshot(sNextSnapshot)
if *debugFlag { 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())
}
} }
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 { if *debugFlag {
log.Printf("RunAppSchedule : All snapshots sent for", 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 : Last snapshot sent for", string(src))
} }
} }
} }