backup/backup.go

96 lines
2.1 KiB
Go
Raw Normal View History

2021-10-11 03:43:05 +02:00
package main
import (
"flag"
2021-11-14 05:23:53 +01:00
"fmt"
2021-10-11 03:43:05 +02:00
"log"
"os"
2022-06-17 14:54:14 +02:00
"time"
2021-10-11 03:43:05 +02:00
)
var (
2022-10-08 04:49:28 +02:00
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
2021-10-11 03:43:05 +02:00
)
func main() {
flag.Parse()
2021-11-14 05:23:53 +01:00
fmt.Printf("backup (%s)\n", version)
2022-06-17 14:54:14 +02:00
email = new(Email)
email.startTime = time.Now()
2022-06-19 05:59:17 +02:00
email.items = make([]string, 0)
2022-06-17 14:54:14 +02:00
2021-10-11 03:43:05 +02:00
err := cfg.Load()
if err != nil {
log.Printf("Cannot load config (%s)", err)
os.Exit(1)
}
2022-06-19 06:12:00 +02:00
if *testMailFlag {
2022-06-19 06:33:24 +02:00
SendMail(cfg.Email.SmtpHost, cfg.Email.FromEmail, "test backup email topic", "test backup email body", cfg.Email.ToEmail)
2022-06-19 06:12:00 +02:00
os.Exit(0)
}
2022-10-08 04:49:28 +02:00
err = RunBackup(*appFlag, *stopOnErrorFlag)
2021-10-11 03:43:05 +02:00
if err != nil {
log.Printf("Cannot run schedule (%s)", err)
os.Exit(1)
}
2022-06-17 14:54:14 +02:00
if len(email.items) > 0 {
2022-10-08 04:49:28 +02:00
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)
2022-06-19 05:59:17 +02:00
log.Printf("Sending summary email\r\n%v", email.items)
2022-06-17 14:54:14 +02:00
}
2021-10-11 03:43:05 +02:00
}
2021-10-16 15:39:54 +02:00
//RunBackup run all backup targets where schedule is registered
2022-10-08 04:49:28 +02:00
func RunBackup(app string, stopOnError bool) error {
2021-10-18 16:20:44 +02:00
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)
}
2022-10-08 04:49:28 +02:00
if stopOnError {
return err
}
2021-10-18 16:20:44 +02:00
}
}
} 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
}
2021-10-11 03:43:05 +02:00
}
}
}
return nil
}