-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (76 loc) · 2.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bufio"
"container/list"
"flag"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
var dryRun bool
var password string
var targetFolder string
var configFile string
var logsFolder string
var taskNamesToExecute string
flag.BoolVar(&dryRun, "dryRun", true, "dry run")
flag.StringVar(&password, "password", "", "protect archive with given password")
flag.StringVar(&targetFolder, "targetFolder", "", "archive location")
flag.StringVar(&configFile, "configFile", "", "configuration file")
flag.StringVar(&logsFolder, "logsFolder", "", "logs folder location")
flag.StringVar(&taskNamesToExecute, "taskNamesToExecute", "", "list of backup tasks to execute")
flag.Parse()
var configs = LoadBackupTaskConfigs(configFile)
if len(logsFolder) > 0 {
logsFolder = strings.Replace(logsFolder, "\\", "/", -1)
if !(strings.HasSuffix(logsFolder, "/")) {
logsFolder += "/"
}
RotateLogs(logsFolder, dryRun)
}
var taskNamesToKeepArray []string
if len(taskNamesToExecute) > 0 {
taskNamesToKeepArray = strings.Split(taskNamesToExecute, ",")
} else {
taskNamesToKeepArray = make([]string, 0)
}
var failedTasks list.List
for i := 0; i < len(configs); i++ {
var config = configs[i]
fmt.Print("\n----------[ executing step " + strconv.Itoa(i+1) + "/" + strconv.Itoa(len(configs)) + ", backup of " + config.TaskName + " ]----------\n")
if len(taskNamesToExecute) > 0 && !IsElementExist(taskNamesToKeepArray, config.TaskName) {
fmt.Println("✔ Skipped by backup tasks filter (taskNamesToExecute)")
continue
}
var archiveName = strings.Replace(config.Source, "\\", "_", -1)
archiveName = strings.Replace(archiveName, "/", "_", -1)
archiveName = strings.Replace(archiveName, ":", "", -1)
compressResult := Compress(config.Source, config.Excludes, targetFolder, archiveName, password, config.ProtectWithPassword == "true", dryRun)
if len(logsFolder) > 0 {
SaveLogs(compressResult, logsFolder, dryRun)
}
if validate7zOutput(compressResult) {
fmt.Println("✅ Compression completed with success")
} else {
failedTasks.PushFront("❌ " + config.TaskName + ":\nBad compression output: " + compressResult)
}
}
if failedTasks.Len() > 0 {
fmt.Println("\n----------[ ❌ error report ❌ ]----------")
SaveLogs(strconv.Itoa(failedTasks.Len())+" task(s) failed", logsFolder, dryRun)
for e := failedTasks.Front(); e != nil; e = e.Next() {
SaveLogs(fmt.Sprint(e.Value), logsFolder, dryRun)
fmt.Println(e.Value)
}
fmt.Println("\n" + strconv.Itoa(failedTasks.Len()) + " task(s) failed")
} else {
fmt.Println("\n😎 Everything is OK! 😎")
SaveLogs("Everything is OK!", logsFolder, dryRun)
}
fmt.Print("\nPress any key to exit...")
input := bufio.NewScanner(os.Stdin)
input.Scan()
fmt.Println(input.Text())
}