backup/backup.go
2023-06-29 22:58:24 +02:00

64 lines
1.1 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")
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 *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)
os.Exit(0)
}
}