test email and clean first snapshots

This commit is contained in:
shoopea 2022-06-17 20:54:14 +08:00
parent 41d56eb90a
commit e62627e406
5 changed files with 88 additions and 18 deletions

16
app.go
View File

@ -704,6 +704,14 @@ func (a AppConfig) RunAppBackup() error {
} }
} }
err = a.CleanupSnapshot()
if err != nil {
if *debugFlag {
log.Printf("AppConfig.RunAppBackup : %s : CleanupSnapshot : %s", a.Name, err)
}
return err
}
if schedule != "" { if schedule != "" {
err = a.ExecBefore(schedule) err = a.ExecBefore(schedule)
if err != nil { if err != nil {
@ -748,13 +756,5 @@ func (a AppConfig) RunAppBackup() error {
} }
} }
err = a.CleanupSnapshot()
if err != nil {
if *debugFlag {
log.Printf("AppConfig.RunAppBackup : %s : RefreshSnapshot : %s", a.Name, err)
}
return err
}
return nil return nil
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"time"
) )
var ( var (
@ -15,6 +16,7 @@ var (
testFlag = flag.Bool("test", false, "test run") testFlag = flag.Bool("test", false, "test run")
debugFlag = flag.Bool("debug", false, "debug") debugFlag = flag.Bool("debug", false, "debug")
cfg Config cfg Config
email *Email
) )
func main() { func main() {
@ -22,6 +24,9 @@ func main() {
fmt.Printf("backup (%s)\n", version) fmt.Printf("backup (%s)\n", version)
email = new(Email)
email.startTime = time.Now()
err := cfg.Load() err := cfg.Load()
if err != nil { if err != nil {
log.Printf("Cannot load config (%s)", err) log.Printf("Cannot load config (%s)", err)
@ -33,6 +38,10 @@ func main() {
log.Printf("Cannot run schedule (%s)", err) log.Printf("Cannot run schedule (%s)", err)
os.Exit(1) os.Exit(1)
} }
if len(email.items) > 0 {
SendMail(cfg.Email.smtpHost, cfg.Email.fromEmail, "Autobackup report", fmt.Sprintf("%v", email.items), cfg.Email.toEmail)
}
} }
//RunBackup run all backup targets where schedule is registered //RunBackup run all backup targets where schedule is registered

View File

@ -15,8 +15,9 @@ import (
type Config struct { type Config struct {
Zfsnap map[string]string `json:"zfsnap"` Zfsnap map[string]string `json:"zfsnap"`
Box map[string]*Box `json:"box"` Box map[string]*Box `json:"box"`
Apps []AppConfig `json:apps` Apps []AppConfig `json:"apps"`
Timezone string `json:"timezone"` Timezone string `json:"timezone"`
Email EmailConfig `json:"email"`
Now time.Time `json:"-"` Now time.Time `json:"-"`
} }
@ -135,7 +136,7 @@ func (c *Config) Load() error {
return fmt.Errorf("No box defined for source : %s", string(src)) return fmt.Errorf("No box defined for source : %s", string(src))
} }
if !cfg.Box[src.Box()].online { if !cfg.Box[src.Box()].online {
return fmt.Errorf("Source box offline for app : %s", app.Name) email.items = append(email.items, fmt.Sprintf("Source box offline for app : %s", app.Name))
} }
} }
var allOffline bool = true var allOffline bool = true
@ -151,7 +152,7 @@ func (c *Config) Load() error {
} }
} }
if allOffline { if allOffline {
return fmt.Errorf("No online destination box for app : %s", app.Name) email.items = append(email.items, fmt.Sprintf("No online destination box for app : %s", app.Name))
} }
for val, before := range app.Before { for val, before := range app.Before {
@ -169,7 +170,7 @@ func (c *Config) Load() error {
return fmt.Errorf("No box defined for before : %s", string(before)) return fmt.Errorf("No box defined for before : %s", string(before))
} }
if !cfg.Box[before.Box()].online { if !cfg.Box[before.Box()].online {
return fmt.Errorf("Before box offline for app : %s", app.Name) email.items = append(email.items, fmt.Sprintf("Before box offline for app : %s", app.Name))
} }
} }
for val, after := range app.After { for val, after := range app.After {
@ -187,7 +188,7 @@ func (c *Config) Load() error {
return fmt.Errorf("No box defined for after : %s", string(after)) return fmt.Errorf("No box defined for after : %s", string(after))
} }
if !cfg.Box[after.Box()].online { if !cfg.Box[after.Box()].online {
return fmt.Errorf("After box offline for app : %s", app.Name) email.items = append(email.items, fmt.Sprintf("After box offline for app : %s", app.Name))
} }
} }
} }

60
email.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"encoding/base64"
"net/smtp"
"strings"
"time"
)
type Email struct {
startTime time.Time
items []string
}
type EmailConfig struct {
smtpHost string `json:"smtp"`
fromEmail string `json:"email_from"`
toEmail []string `json:"email_to"`
}
func SendMail(addr, from, subject, body string, to []string) error {
r := strings.NewReplacer("\r\n", "", "\r", "", "\n", "", "%0a", "", "%0d", "")
c, err := smtp.Dial(addr)
if err != nil {
return err
}
defer c.Close()
if err = c.Mail(r.Replace(from)); err != nil {
return err
}
for i := range to {
to[i] = r.Replace(to[i])
if err = c.Rcpt(to[i]); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
msg := "To: " + strings.Join(to, ",") + "\r\n" +
"From: " + from + "\r\n" +
"Subject: " + subject + "\r\n" +
"Content-Type: text/html; charset=\"UTF-8\"\r\n" +
"Content-Transfer-Encoding: base64\r\n" +
"\r\n" + base64.StdEncoding.EncodeToString([]byte(body))
_, err = w.Write([]byte(msg))
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}

View File

@ -1,6 +1,6 @@
// Code generated by version.sh (@generated) DO NOT EDIT. // Code generated by version.sh (@generated) DO NOT EDIT.
package main package main
var githash = "2ae1737" var githash = "41d56eb"
var buildstamp = "2022-05-21_15:22:30" var buildstamp = "2022-06-17_12:53:48"
var commits = "33" var commits = "34"
var version = "2ae1737-b33 - 2022-05-21_15:22:30" var version = "41d56eb-b34 - 2022-06-17_12:53:48"