package main import ( "log" "os" "path/filepath" "time" ) func init() { log.SetFlags(0) } func main() { if len(os.Args) < 2 { printUsage() os.Exit(1) } switch os.Args[1] { case "f": config, err := LoadConfig(os.Args[2]) if err != nil { log.Fatalln(err) } err = config.FullBackup() if err != nil { log.Fatalln(err) } case "i": config, err := LoadConfig(os.Args[2]) if err != nil { log.Fatalln(err) } err = config.IncrementalBackup() if err != nil { log.Fatalln(err) } case "s": config, err := LoadConfig(os.Args[2]) if err != nil { log.Fatalln("read config error:", err) } idx, err := config.FindAll(os.Args[3]) if err != nil { config.fatalln("search error:", err) } idx.ViewFileVersions(os.Stdout) case "r": config, err := LoadConfig(os.Args[2]) if err != nil { log.Fatalln(err) } var t time.Time switch len(os.Args[4]) { case len("02.01.2006"): t, err = time.Parse("02.01.2006 15:04", os.Args[4]) if err != nil { config.fatalln("time parse error:", err) } case len("02.01.2006 15:04"): t, err = time.Parse("02.01.2006 15:04", os.Args[4]) if err != nil { config.fatalln("time parse error:", err) } case len("02.01.2006 15:04:05"): t, err = time.Parse("02.01.2006 15:04:05", os.Args[4]) if err != nil { config.fatalln("time parse error:", err) } default: config.fatalln(`wrong time format, must be ["DD.MM.YYYY", "DD.MM.YYYY hh:mm", "DD.MM.YYYY hh:mm:ss"]`) } plan, err := config.extractionPlan(os.Args[3], t) if err != nil { log.Fatalln(err) } err = config.extract(plan, os.Args[5]) if err != nil { log.Fatalln(err) } case "t": config, err := LoadConfig(os.Args[2]) if err != nil { log.Fatalln(err) } _, err = config.index(true) if err != nil { log.Fatalln(err) } default: printUsage() } } func printUsage() { bin := filepath.Base(os.Args[0]) log.Print("Usage:\n") log.Printf("%s i - do incremental backup\n", bin) log.Printf("%s f - do full backup\n", bin) log.Printf("%s s - search file(s) in backup\n", bin) log.Printf("%s r - recover file(s) from backup\n", bin) log.Printf("%s t - test archive for errors\n", bin) }