backuper/main.go

113 lines
2.3 KiB
Go
Raw Normal View History

2023-03-11 14:13:35 +05:00
package main
import (
"log"
"os"
"path/filepath"
"time"
)
func init() {
log.SetFlags(0)
}
func main() {
if len(os.Args) < 2 {
2023-03-11 14:13:35 +05:00
printUsage()
os.Exit(1)
}
switch os.Args[1] {
case "f":
2023-03-11 14:13:35 +05:00
config, err := LoadConfig(os.Args[2])
if err != nil {
log.Fatalln(err)
2023-03-11 14:13:35 +05:00
}
err = config.FullBackup()
2023-03-11 14:13:35 +05:00
if err != nil {
log.Fatalln(err)
2023-03-11 14:13:35 +05:00
}
case "i":
2023-03-11 14:13:35 +05:00
config, err := LoadConfig(os.Args[2])
if err != nil {
log.Fatalln(err)
}
err = config.IncrementalBackup()
2023-03-11 14:13:35 +05:00
if err != nil {
log.Fatalln(err)
2023-03-11 14:13:35 +05:00
}
case "s":
config, err := LoadConfig(os.Args[2])
if err != nil {
log.Fatalln("read config error:", err)
2023-03-11 14:13:35 +05:00
}
idx, err := config.FindAll(os.Args[3])
if err != nil {
config.fatalln("search error:", err)
2023-03-11 14:13:35 +05:00
}
idx.ViewFileVersions(os.Stdout)
2023-03-11 14:13:35 +05:00
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"]`)
2023-03-11 14:13:35 +05:00
}
plan, err := config.extractionPlan(os.Args[3], t)
2023-03-11 14:13:35 +05:00
if err != nil {
log.Fatalln(err)
}
err = config.extract(plan, os.Args[5])
2023-03-11 14:13:35 +05:00
if err != nil {
log.Fatalln(err)
}
case "t":
config, err := LoadConfig(os.Args[2])
if err != nil {
log.Fatalln(err)
}
_, err = config.index(true)
2023-03-11 14:13:35 +05:00
if err != nil {
log.Fatalln(err)
2023-03-11 14:13:35 +05:00
}
default:
printUsage()
}
}
func printUsage() {
bin := filepath.Base(os.Args[0])
log.Print("Usage:\n")
log.Printf("%s i <config file path> - do incremental backup\n", bin)
log.Printf("%s f <config file path> - do full backup\n", bin)
log.Printf("%s s <config file path> <mask> - search file(s) in backup\n", bin)
log.Printf("%s r <config file path> <mask> <dd.mm.yyyy hh:mm> <path> - recover file(s) from backup\n", bin)
log.Printf("%s t <config file path> - test archive for errors\n", bin)
2023-03-11 14:13:35 +05:00
}