backup/backup.go
2023-07-31 10:13:36 +02:00

74 lines
1.3 KiB
Go

package main
import (
"flag"
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
)
var (
cfgFile = flag.String("config", "config.json", "config file")
isDaemon = flag.Bool("daemon", false, "run as daemon")
debug = flag.Bool("debug", false, "log debug messages")
quiet = flag.Bool("quiet", false, "remove most log messages")
logFile = flag.String("logfile", "", "log file")
cfg Config
email *Email
)
func main() {
flag.Parse()
fmt.Printf("backup (%s)\n", version)
if *debug {
log.SetLevel(log.DebugLevel)
}
if *quiet {
log.SetLevel(log.WarnLevel)
}
if *logFile != "" {
if f, err := os.OpenFile(*logFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644); err != nil {
log.Printf("Cannot open logfile (%s)", err)
} else {
log.SetOutput(f)
defer f.Close()
}
}
log.SetReportCaller(true)
email = new(Email)
email.startTime = time.Now()
email.items = make([]string, 0)
if err := cfg.LoadFile(*cfgFile); err != nil {
log.Printf("Cannot load config (%s)", err)
os.Exit(1)
}
if *isDaemon {
cfg.Start(nil)
server := NewServer(cfg.Admin.Addr, cfg.Admin.Username, cfg.Admin.Password)
server.Run()
} else {
e := NewEmail(time.Now())
cfg.Start(e)
defer cfg.Stop(e)
cfg.Run(e)
cfg.Cleanup(e)
if err := e.Send(); err != nil {
log.Printf("Cannot send email (%s)", err)
}
os.Exit(0)
}
}