2024-07-12 21:53:59 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"concord.hectabit.org/Hectabit/burgerbackup/lib/client"
|
2024-07-13 10:07:39 +01:00
|
|
|
"github.com/spf13/viper"
|
2024-07-12 21:53:59 +01:00
|
|
|
"log"
|
2024-07-13 10:07:39 +01:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2024-07-13 17:13:57 +01:00
|
|
|
"strings"
|
2024-07-12 21:53:59 +01:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-07-13 10:30:35 +01:00
|
|
|
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 {
|
2024-07-13 10:07:39 +01:00
|
|
|
log.Println("[INFO] Config loaded at", time.Now().Unix())
|
|
|
|
} else if os.IsNotExist(err) {
|
2024-07-13 10:30:35 +01:00
|
|
|
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)
|
|
|
|
}
|
2024-07-13 10:07:39 +01:00
|
|
|
} else {
|
|
|
|
log.Fatalln("[FATAL] File is in quantum uncertainty:", err)
|
|
|
|
}
|
|
|
|
|
2024-07-13 17:13:57 +01:00
|
|
|
viper.SetConfigType("ini")
|
|
|
|
configPathSlice := strings.Split(configPath, "/")
|
|
|
|
configPathNoIni := strings.Split(configPathSlice[len(configPathSlice)-1], ".")[0]
|
|
|
|
viper.SetConfigName(configPathNoIni)
|
|
|
|
configPathNoFile := strings.Join(configPathSlice[:len(configPathSlice)-1], "/") + "/"
|
|
|
|
viper.AddConfigPath(configPathNoFile)
|
2024-07-13 10:07:39 +01:00
|
|
|
viper.AutomaticEnv()
|
|
|
|
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("[FATAL] Error in config file at", strconv.FormatInt(time.Now().Unix(), 10)+":", err)
|
|
|
|
}
|
|
|
|
|
2024-07-13 17:13:57 +01:00
|
|
|
backupKey := viper.GetString("config.BACKUP_KEY")
|
|
|
|
cryptoKey := viper.GetString("config.CRYPTO_KEY")
|
|
|
|
backupInterval := viper.GetInt("config.BACKUP_INTERVAL")
|
|
|
|
fileLocation := viper.GetString("config.FILE_LOCATION")
|
|
|
|
remoteURL := viper.GetString("config.REMOTE_URL")
|
2024-07-13 10:07:39 +01:00
|
|
|
|
2024-07-12 21:53:59 +01:00
|
|
|
for {
|
2024-07-13 10:07:39 +01:00
|
|
|
err, errCode := client.PerformBackup(fileLocation, backupKey, cryptoKey, remoteURL)
|
2024-07-12 21:53:59 +01:00
|
|
|
if err != nil {
|
2024-07-13 17:13:57 +01:00
|
|
|
switch errCode {
|
|
|
|
case 0:
|
|
|
|
log.Println("[CRITICAL] Unknown in PerformBackup() file read:", err)
|
|
|
|
case 1:
|
|
|
|
log.Println("[CRITICAL] Unknown in PerformBackup() file stat:", err)
|
|
|
|
case 2:
|
|
|
|
log.Println("[CRITICAL] Unknown in PerformBackup() content marshal:", err)
|
|
|
|
case 3:
|
|
|
|
log.Println("[CRITICAL] Unknown in PerformBackup() content encryption:", err)
|
|
|
|
case 4:
|
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() form writer creation:", err)
|
|
|
|
case 5:
|
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() IO Copying:", err)
|
|
|
|
case 6:
|
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() writer closing:", err)
|
|
|
|
case 7:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() request creation:", err)
|
2024-07-13 17:13:57 +01:00
|
|
|
case 8:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() hash creation:", err)
|
2024-07-13 17:13:57 +01:00
|
|
|
case 9:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() request execution:", err)
|
2024-07-13 17:13:57 +01:00
|
|
|
case 10:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() response read:", err)
|
2024-07-13 17:13:57 +01:00
|
|
|
case 11:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Unknown in SendFileToServer() response marshalling:", err)
|
2024-07-13 17:13:57 +01:00
|
|
|
case 12:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Server sent message in SendFileToServer():", err)
|
2024-07-13 17:13:57 +01:00
|
|
|
default:
|
2024-07-12 21:53:59 +01:00
|
|
|
log.Println("[CRITICAL] Unknown error in main():", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(time.Duration(backupInterval) * 1000000000)
|
|
|
|
}
|
|
|
|
}
|