update email and stop on error

This commit is contained in:
shoopea
2022-10-08 10:49:28 +08:00
parent f36e7da824
commit a7ee0bfeb7
3 changed files with 26 additions and 18 deletions

View File

@@ -9,15 +9,16 @@ import (
)
var (
appFlag = flag.String("app", "", "run specific app")
cfgFile = flag.String("config", "config.json", "config file")
schedFlag = flag.String("schedule", "", "specific schedule")
slowFlag = flag.Bool("slow", false, "slow process")
testFlag = flag.Bool("test", false, "test run")
debugFlag = flag.Bool("debug", false, "debug")
testMailFlag = flag.Bool("test-mail", false, "test email setup")
cfg Config
email *Email
appFlag = flag.String("app", "", "run specific app")
cfgFile = flag.String("config", "config.json", "config file")
schedFlag = flag.String("schedule", "", "specific schedule")
slowFlag = flag.Bool("slow", false, "slow process")
testFlag = flag.Bool("test", false, "test run")
debugFlag = flag.Bool("debug", false, "debug")
testMailFlag = flag.Bool("test-mail", false, "test email setup")
stopOnErrorFlag = flag.Bool("stop-on-error", false, "stop processing on error")
cfg Config
email *Email
)
func main() {
@@ -40,20 +41,24 @@ func main() {
os.Exit(0)
}
err = RunBackup(*appFlag)
err = RunBackup(*appFlag, *stopOnErrorFlag)
if err != nil {
log.Printf("Cannot run schedule (%s)", err)
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)
body := " - " + email.items[0]
for _, v := range email.items[1:] {
body = body + "\r\n" + " - " + v
}
SendMail(cfg.Email.SmtpHost, cfg.Email.FromEmail, "Autobackup report", body, cfg.Email.ToEmail)
log.Printf("Sending summary email\r\n%v", email.items)
}
}
//RunBackup run all backup targets where schedule is registered
func RunBackup(app string) error {
func RunBackup(app string, stopOnError bool) error {
if app == "" {
if *debugFlag {
log.Printf("RunBackup() : Start")
@@ -64,7 +69,9 @@ func RunBackup(app string) error {
if *debugFlag {
log.Printf("RunBackup() : Error running %s", a.Name)
}
return err
if stopOnError {
return err
}
}
}
} else {