2021-10-11 03:43:05 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
2021-10-16 15:39:54 +02:00
|
|
|
Zfsnap map[string]string `json:"zfsnap"`
|
|
|
|
Box map[string]*BoxConfig `json:"box"`
|
|
|
|
Apps []AppConfig `json:apps`
|
|
|
|
Timezone string `json:"timezone"`
|
2021-10-11 03:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type BoxConfig struct {
|
|
|
|
Addr string `json:"addr"`
|
|
|
|
User string `json:"user"`
|
|
|
|
Key string `json:"key"`
|
2021-10-16 15:39:54 +02:00
|
|
|
ssh *SSHConfig
|
2021-10-11 03:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
cfgFile = flag.String("config", "config.json", "config file")
|
|
|
|
schedFlag = flag.String("schedule", "", "specific schedule")
|
|
|
|
testFlag = flag.Bool("test", true, "test run")
|
|
|
|
debugFlag = flag.Bool("debug", true, "debug")
|
|
|
|
cfg Config
|
|
|
|
)
|
|
|
|
|
|
|
|
//Load config from file
|
|
|
|
func (c *Config) Load() error {
|
2021-10-16 15:39:54 +02:00
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("SSHConfig.Load : Start")
|
|
|
|
}
|
2021-10-11 03:43:05 +02:00
|
|
|
b, err := ioutil.ReadFile(*cfgFile)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : ioutil.ReadFile(%s) : %s", *cfgFile, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(b, &c)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : json.Unmarshal : %s", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range c.Box {
|
|
|
|
s := &SSHConfig{
|
|
|
|
logged: false,
|
|
|
|
name: k,
|
|
|
|
}
|
2021-10-16 15:39:54 +02:00
|
|
|
v.ssh = s
|
2021-10-11 03:43:05 +02:00
|
|
|
keyRaw, err := ioutil.ReadFile(v.Key)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : ioutil.ReadFile(%s) : %s", k, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := ssh.ParseRawPrivateKey(keyRaw)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : ssh.ParseRawPrivateKey(%s) : %s", k, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.signer, err = ssh.NewSignerFromKey(key)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : ssh.NewSignerFromKey(%s) : %s", k, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.config = &ssh.ClientConfig{
|
|
|
|
User: v.User,
|
|
|
|
Auth: []ssh.AuthMethod{
|
|
|
|
ssh.PublicKeys(s.signer),
|
|
|
|
},
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
}
|
|
|
|
|
|
|
|
s.client, err = ssh.Dial("tcp", v.Addr, s.config)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : ssh.Dial(%s) : %s", k, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := s.client.NewSession()
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : client.NewSession(%s) : %s", k, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
session.Stdout = &b
|
2021-10-16 15:39:54 +02:00
|
|
|
|
|
|
|
err = session.Run("TZ=\"" + cfg.Timezone + "\" /usr/sbin/zfsnap --version")
|
2021-10-11 03:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : client.NewSession(%s) : %s", k, err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : logged into %s : %s", k, b.String())
|
|
|
|
}
|
|
|
|
session.Close()
|
|
|
|
s.logged = true
|
2021-10-16 15:39:54 +02:00
|
|
|
}
|
2021-10-11 03:43:05 +02:00
|
|
|
|
2021-10-16 15:39:54 +02:00
|
|
|
for _, box := range c.Box {
|
|
|
|
err = box.ssh.getTime()
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : ssh.getTime() : %s", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-10-11 03:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, app := range c.Apps {
|
|
|
|
for _, src := range app.Sources {
|
|
|
|
if !src.Valid() {
|
|
|
|
return fmt.Errorf("Source not valid : %s", string(src))
|
|
|
|
}
|
2021-10-16 15:39:54 +02:00
|
|
|
if _, ok := cfg.Box[src.Box()]; !ok {
|
2021-10-11 03:43:05 +02:00
|
|
|
return fmt.Errorf("No box defined for source : %s", string(src))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, dest := range app.Destinations {
|
|
|
|
if !dest.Valid() {
|
|
|
|
return fmt.Errorf("Destination not valid : %s", string(dest))
|
|
|
|
}
|
2021-10-16 15:39:54 +02:00
|
|
|
if _, ok := cfg.Box[dest.Box()]; !ok {
|
2021-10-11 03:43:05 +02:00
|
|
|
return fmt.Errorf("No box defined for destination : %s", string(dest))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for val, before := range app.Before {
|
|
|
|
_, err = regexp.Compile(val)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : invalid regex : %s", val)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !before.Valid() {
|
|
|
|
return fmt.Errorf("Before not valid : %s", string(before))
|
|
|
|
}
|
2021-10-16 15:39:54 +02:00
|
|
|
if _, ok := cfg.Box[before.Box()]; !ok {
|
2021-10-11 03:43:05 +02:00
|
|
|
return fmt.Errorf("No box defined for before : %s", string(before))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for val, after := range app.After {
|
|
|
|
_, err = regexp.Compile(val)
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("Config.Load : invalid regex : %s", val)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !after.Valid() {
|
|
|
|
return fmt.Errorf("After not valid : %s", string(after))
|
|
|
|
}
|
2021-10-16 15:39:54 +02:00
|
|
|
if _, ok := cfg.Box[after.Box()]; !ok {
|
2021-10-11 03:43:05 +02:00
|
|
|
return fmt.Errorf("No box defined for after : %s", string(after))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//Close config
|
|
|
|
func (c *Config) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
err := cfg.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot load config (%s)", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:54 +02:00
|
|
|
err = RunBackup()
|
2021-10-11 03:43:05 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot run schedule (%s)", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 15:39:54 +02:00
|
|
|
//RunBackup run all backup targets where schedule is registered
|
|
|
|
func RunBackup() error {
|
2021-10-11 03:43:05 +02:00
|
|
|
if *debugFlag {
|
2021-10-16 15:39:54 +02:00
|
|
|
log.Printf("RunBackup() : Start")
|
2021-10-11 03:43:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, app := range cfg.Apps {
|
2021-10-16 15:39:54 +02:00
|
|
|
err := app.RunAppBackup()
|
|
|
|
if err != nil {
|
|
|
|
if *debugFlag {
|
|
|
|
log.Printf("RunBackup() : Error running %s", app.Name)
|
2021-10-11 03:43:05 +02:00
|
|
|
}
|
2021-10-16 15:39:54 +02:00
|
|
|
return err
|
2021-10-11 03:43:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|