96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
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")
|
|
stopOnErrorFlag = flag.Bool("stop-on-error", false, "stop processing on error")
|
|
cfg Config
|
|
email *Email
|
|
)
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
|
|
fmt.Printf("backup (%s)\n", version)
|
|
|
|
email = new(Email)
|
|
email.startTime = time.Now()
|
|
email.items = make([]string, 0)
|
|
|
|
err := cfg.Load()
|
|
if err != nil {
|
|
log.Printf("Cannot load config (%s)", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if *testMailFlag {
|
|
SendMail(cfg.Email.SmtpHost, cfg.Email.FromEmail, "test backup email topic", "test backup email body", cfg.Email.ToEmail)
|
|
os.Exit(0)
|
|
}
|
|
|
|
err = RunBackup(*appFlag, *stopOnErrorFlag)
|
|
if err != nil {
|
|
log.Printf("Cannot run schedule (%s)", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
if len(email.items) > 0 {
|
|
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, stopOnError bool) error {
|
|
if app == "" {
|
|
if *debugFlag {
|
|
log.Printf("RunBackup() : Start")
|
|
}
|
|
for _, a := range cfg.Apps {
|
|
err := a.RunAppBackup()
|
|
if err != nil {
|
|
if *debugFlag {
|
|
log.Printf("RunBackup() : Error running %s", a.Name)
|
|
}
|
|
if stopOnError {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
if *debugFlag {
|
|
log.Printf("RunBackup() : Start %s", app)
|
|
}
|
|
for _, a := range cfg.Apps {
|
|
if a.Name == app {
|
|
err := a.RunAppBackup()
|
|
if err != nil {
|
|
if *debugFlag {
|
|
log.Printf("RunBackup() : Error running %s", a.Name)
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|