81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"concord.hectabit.org/Hectabit/burgerbackup/lib/client"
|
|
"github.com/spf13/viper"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
var configPath = "./config.ini"
|
|
if len(os.Args) > 1 {
|
|
if os.Args[1] == "-h" || os.Args[1] == "--help" {
|
|
log.Println("[INFO] Usage: burgerbackup-client </path/to/config/file>")
|
|
os.Exit(0)
|
|
} else {
|
|
configPath = os.Args[1]
|
|
}
|
|
}
|
|
|
|
if _, err := os.Stat(configPath); err == nil {
|
|
log.Println("[INFO] Config loaded at", time.Now().Unix())
|
|
} else if os.IsNotExist(err) {
|
|
originalConfigPath := configPath
|
|
configPath = "/etc/burgerbackup/client.ini"
|
|
if _, err := os.Stat(configPath); err == nil {
|
|
log.Println("[INFO] Config loaded at", time.Now().Unix())
|
|
log.Println("[WARN] The config file was not found at", originalConfigPath, "so the default config path of", configPath, "was used.")
|
|
} else if os.IsNotExist(err) {
|
|
log.Fatalln("[FATAL]", originalConfigPath, "does not exist")
|
|
} else {
|
|
log.Fatalln("[FATAL] File is in quantum uncertainty:", err)
|
|
}
|
|
} else {
|
|
log.Fatalln("[FATAL] File is in quantum uncertainty:", err)
|
|
}
|
|
|
|
viper.SetConfigName(configPath)
|
|
viper.AddConfigPath("./")
|
|
viper.AutomaticEnv()
|
|
|
|
err := viper.ReadInConfig()
|
|
if err != nil {
|
|
log.Fatalln("[FATAL] Error in config file at", strconv.FormatInt(time.Now().Unix(), 10)+":", err)
|
|
}
|
|
|
|
backupKey := viper.GetString("BACKUP_KEY")
|
|
cryptoKey := viper.GetString("CRYPTO_KEY")
|
|
backupInterval := viper.GetInt("BACKUP_INTERVAL")
|
|
fileLocation := viper.GetString("FILE_LOCATION")
|
|
remoteURL := viper.GetString("REMOTE_URL")
|
|
|
|
for {
|
|
err, errCode := client.PerformBackup(fileLocation, backupKey, cryptoKey, remoteURL)
|
|
if err != nil {
|
|
if errCode == 0 {
|
|
log.Println("[CRITICAL] Unknown in performBackup() file read:", err)
|
|
} else if errCode == 1 {
|
|
log.Println("[CRITICAL] Unknown in performBackup() content encryption:", err)
|
|
} else if errCode == 2 {
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() request creation:", err)
|
|
} else if errCode == 3 {
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() hash creation:", err)
|
|
} else if errCode == 4 {
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() request execution:", err)
|
|
} else if errCode == 5 {
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() response read:", err)
|
|
} else if errCode == 6 {
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() response marshalling:", err)
|
|
} else if errCode == 7 {
|
|
log.Println("[CRITICAL] Server sent message in SendFileToServer():", err)
|
|
} else {
|
|
log.Println("[CRITICAL] Unknown error in main():", err)
|
|
}
|
|
}
|
|
time.Sleep(time.Duration(backupInterval) * 1000000000)
|
|
}
|
|
}
|