backup/backup.go

72 lines
1.3 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
"os"
2023-06-29 22:58:24 +02:00
log "github.com/sirupsen/logrus"
2021-10-11 03:43:05 +02:00
)
var (
2023-08-20 16:57:53 +02:00
cfgFile = flag.String("config", "", "config file")
2023-06-29 22:58:24 +02:00
isDaemon = flag.Bool("daemon", false, "run as daemon")
debug = flag.Bool("debug", false, "log debug messages")
2023-06-29 23:46:53 +02:00
quiet = flag.Bool("quiet", false, "remove most log messages")
2023-06-29 22:58:24 +02:00
logFile = flag.String("logfile", "", "log file")
2023-08-20 16:57:53 +02:00
cfg *Config
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)
2023-06-29 22:58:24 +02:00
if *debug {
log.SetLevel(log.DebugLevel)
}
2023-06-29 23:46:53 +02:00
if *quiet {
log.SetLevel(log.WarnLevel)
}
2023-06-29 22:58:24 +02:00
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)
2023-08-20 16:57:53 +02:00
if *cfgFile != "" {
if c, err := LoadConfigFile(*cfgFile); err != nil {
log.Printf("Cannot load config (%s)", err)
os.Exit(1)
} else {
cfg = c
}
} else if c, err := LoadConfigFile("backup.json"); err == nil {
cfg = c
} else {
cfg, _ = LoadConfigByte(sampleCfg)
2021-10-11 03:43:05 +02:00
}
2023-06-29 22:58:24 +02:00
if *isDaemon {
2023-07-31 18:11:29 +02:00
if cfg.Admin == nil {
cfg.Admin = NewAdmin()
2023-07-31 10:13:36 +02:00
}
2023-08-20 16:57:53 +02:00
if cfg.Admin.Secrets == nil {
cfg.Admin.Secrets = NewSecrets()
}
if len(cfg.Admin.Users) == 0 {
cfg.Admin.NewAdminUser()
}
2023-07-31 18:11:29 +02:00
cfg.Admin.Run()
} else {
cfg.Run()
2023-06-29 22:58:24 +02:00
os.Exit(0)
2021-10-11 03:43:05 +02:00
}
}