Big refactor

Merge pull request #18 from netbenix/develop
This commit is contained in:
netbenix 2021-12-31 17:22:14 +01:00 committed by GitHub
commit 82c99aa75f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 413 additions and 193 deletions

3
.gitignore vendored
View file

@ -17,3 +17,6 @@ scabiosa
#tmp folder
tmp/
#config folder
config/

View file

@ -0,0 +1,111 @@
package Commands
import (
"fmt"
"github.com/urfave/cli/v2"
"os"
"scabiosa/Logging"
"scabiosa/Tools"
)
func GenerateNewConfigs() *cli.Command {
logger := Logging.Logger("generate-configs")
return &cli.Command{
Name: "generate-config",
Usage: "Generates the default configs",
Description: "Creates the specified configs",
HelpName: "generate-config",
Action: func(c *cli.Context) error {
err := os.RemoveAll("config/")
os.Mkdir("config", 0755)
if err != nil {
return err
}
var sqlConfig Tools.SQLConfig
var input string
var inputInt uint8
fmt.Printf("Want to use SQL? [Y]/[N]: ")
fmt.Scanf("%s", &input)
if input[0] == 'Y' || input[0] == 'y' {
sqlConfig.EnableSQL = true
fmt.Printf("What SQL Type do you want to use?\n")
fmt.Printf("[0] MariaDB\n")
fmt.Printf("\nSelection: ")
fmt.Scanf("%d", &inputInt)
switch inputInt {
case 0:
{
sqlConfig.SqlType = "mariadb"
}
default:
{
fmt.Printf("Invalid input!")
os.Exit(1)
}
}
fmt.Printf("\n\nSQL Address: ")
fmt.Scanf("%s", &sqlConfig.SqlAddress)
fmt.Printf("\nSQL Port: ")
fmt.Scanf("%d", &sqlConfig.SqlPort)
fmt.Printf("\nSQL Database: ")
fmt.Scanf("%s", &sqlConfig.Database)
fmt.Printf("\nSQL User: ")
fmt.Scanf("%s", &sqlConfig.DbUser)
fmt.Printf("\nSQL Password: ")
fmt.Scanf("%s", &sqlConfig.DbPassword)
Tools.GenerateSQLConfig(sqlConfig)
fmt.Printf("SQL config created!")
} else {
sqlConfig.EnableSQL = false
}
fmt.Printf("\n\nWhich storage do you want to use?\n")
fmt.Printf("[0]\tNone\n")
fmt.Printf("[1]\tAzure File Share\n")
fmt.Printf("\nSelection: ")
fmt.Scanf("%d", &inputInt)
switch inputInt {
case 0:
{
fmt.Printf("Reminder: remoteStorageType = none\n")
//Do (nearly) nothing! :D
}
case 1:
{
var azure Tools.AzureConfig
fmt.Printf("\n\nStorageAccount Name: ")
fmt.Scanf("%s", &azure.StorageAccountName)
fmt.Printf("\nStorageAccount Key: ")
fmt.Scanf("%s", &azure.StorageAccountKey)
fmt.Printf("\nFileshare Name: ")
fmt.Scanf("%s", &azure.FileshareName)
Tools.GenerateAzureConfig(azure)
fmt.Printf("\nAzure config created!\n")
fmt.Printf("Reminder: remoteStorageType = azure-file\n")
}
default:
{
fmt.Printf("Invalid input!")
os.Exit(1)
}
}
Tools.GenerateBaseConfig()
fmt.Printf("All configs generated!\n")
fmt.Printf("Please modify the config.json with your backup entries.\n")
return nil
},
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
if err != nil {
logger.Fatal(err)
}
return err
},
}
}

View file

@ -0,0 +1,91 @@
package Commands
import (
"github.com/google/uuid"
"github.com/urfave/cli/v2"
"os"
"scabiosa/Compressor"
"scabiosa/Logging"
"scabiosa/SQL"
"scabiosa/StorageTypes"
"scabiosa/Tools"
"time"
)
func StartBackupProc() *cli.Command {
logger := Logging.Logger("backup")
return &cli.Command{
Name: "backup",
Usage: "Starts backup process",
Description: "Compresses and uploads/stores the backups",
HelpName: "backup",
Action: func(c *cli.Context) error {
Tools.CheckIfConfigExists()
config := Tools.GetConfig()
SQL.CreateDefaultTables(SQL.GetSQLInstance())
for _, backupItem := range config.FolderToBackup {
var storage StorageTypes.Storage
var destPath string
if backupItem.RemoteStorageType != "none" {
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
} else {
destPath = backupItem.LocalTargetPath
}
bakFile := Compressor.CreateBakFile(backupItem.BackupName+getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
if backupItem.RemoteStorageType != "none" {
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
}
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none" {
backupItem.LocalTargetPath = "NONE"
_ = os.Remove(bakFile)
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file", time.Now())
}
if backupItem.RemoteStorageType == "none" {
backupItem.CreateLocalBackup = true
backupItem.RemoteTargetPath = "NONE"
}
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
}
return nil
},
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
if err != nil {
logger.Fatal(err)
}
return err
},
}
}
func getTimeSuffix() string {
currTime := time.Now()
return "_" + currTime.Format("02-01-2006_15-04")
}
func checkTmpPath(createLocalBackup bool, targetPath string) string {
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
if !createLocalBackup {
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
dirErr := os.Mkdir("tmp", 0775)
if dirErr != nil {
logger.Fatal(err)
}
}
return "tmp"
}
return targetPath
}

View file

@ -19,33 +19,39 @@ type MariaDBConnector struct {
DbPassword string
}
func GetMariaDBInstance(config Tools.Config) MariaDBConnector {
func GetMariaDBInstance(sqlConfig Tools.SQLConfig) MariaDBConnector {
var mariadb MariaDBConnector
mariadb.Address = config.SQLConfig.SqlAddress
mariadb.Port = config.SQLConfig.SqlPort
mariadb.Database = config.SQLConfig.Database
mariadb.DbUser = config.SQLConfig.DbUser
mariadb.DbPassword = config.SQLConfig.DbPassword
mariadb.Address = sqlConfig.SqlAddress
mariadb.Port = sqlConfig.SqlPort
mariadb.Database = sqlConfig.Database
mariadb.DbUser = sqlConfig.DbUser
mariadb.DbPassword = sqlConfig.DbPassword
return mariadb
}
func checkIfEventLogTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'EventLog';", mariadb.Database)
if !rows.Next(){ return false }
if !rows.Next() {
return false
}
return true
}
func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'Backups';", mariadb.Database)
if !rows.Next(){ return false }
if !rows.Next() {
return false
}
return true
}
func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string, hostname string) bool {
rows, _ := db.Query("SELECT * FROM `"+mariadb.Database+"`.Backups WHERE Hostname = ? AND BackupName = ?;", hostname, backupName)
if !rows.Next(){ return false; }
if !rows.Next() {
return false
}
return true
}

View file

@ -13,33 +13,38 @@ type SQLService interface {
}
func CreateDefaultTables(sqlService SQLService) {
config := Tools.GetConfig()
if config.SQLConfig.EnableSQL{
sqlConfig := Tools.GetSQLConfig()
if sqlConfig.EnableSQL {
sqlService.createDefaultTables()
}
}
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
config := Tools.GetConfig()
if config.SQLConfig.EnableSQL{
sqlConfig := Tools.GetSQLConfig()
if sqlConfig.EnableSQL {
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
}
}
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
config := Tools.GetConfig()
if config.SQLConfig.EnableSQL{
sqlConfig := Tools.GetSQLConfig()
if sqlConfig.EnableSQL {
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
}
}
func GetSQLInstance() SQLService {
config := Tools.GetConfig()
sqlConfig := Tools.GetSQLConfig()
if !config.SQLConfig.EnableSQL { return nil }
if !sqlConfig.EnableSQL {
return nil
}
switch config.SQLConfig.SqlType {
case "mariadb": {return GetMariaDBInstance(config)}
switch sqlConfig.SqlType {
case "mariadb":
{
return GetMariaDBInstance(sqlConfig)
}
}
return nil

View file

@ -12,18 +12,17 @@ import (
"path/filepath"
"scabiosa/Logging"
"scabiosa/SQL"
"scabiosa/Tools"
"strings"
"time"
)
type AzureFileStorage struct {
FileshareName string `json:"fileshareName"`
TargetDirectory string `json:"targetDirectory"`
StorageAccountName string `json:"storageAccountName"`
StorageAccountKey string `json:"storageAccountKey"`
FileshareName string
StorageAccountName string
StorageAccountKey string
}
func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string) {
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
@ -43,11 +42,7 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
logger.Fatal(err)
}
if destinationPath != ""{
azure.TargetDirectory = destinationPath
}
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s/%s/%s", azure.StorageAccountName, azure.FileshareName ,azure.TargetDirectory, filepath.Base(fileName)))
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s/%s/%s", azure.StorageAccountName, azure.FileshareName, destinationPath, filepath.Base(fileName)))
fileURL := azfile.NewFileURL(*u, azfile.NewPipeline(credential, azfile.PipelineOptions{}))
@ -56,7 +51,6 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
fmt.Printf("[%s] Starting upload to Azure File Share...\n", backupName, ".bak")
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now())
progressBar := pb.StartNew(int(fileSize.Size()))
progressBar.Set(pb.Bytes, true)
err = azfile.UploadFileToAzureFile(ctx, file, fileURL,
@ -89,16 +83,20 @@ func readConfig() []byte {
return file
}
func GetAzureStorage() AzureFileStorage {
logger := Logging.DetailedLogger("AzureFileStorage", "GetAzureStorage")
var azureStorage AzureFileStorage
var azureConfig Tools.AzureConfig
var azureFileShare AzureFileStorage
jsonErr := json.Unmarshal(readConfig(), &azureStorage)
jsonErr := json.Unmarshal(readConfig(), &azureConfig)
if jsonErr != nil {
logger.Fatal(jsonErr)
}
return azureStorage
azureFileShare.StorageAccountName = azureConfig.StorageAccountName
azureFileShare.StorageAccountKey = azureConfig.StorageAccountKey
azureFileShare.FileshareName = azureConfig.FileshareName
return azureFileShare
}

View file

@ -2,13 +2,12 @@ package Tools
import (
"encoding/json"
"fmt"
"os"
"scabiosa/Logging"
)
type Config struct {
LocalBackupPath string `json:"localBackupPath"`
SQLConfig struct{
type SQLConfig struct {
EnableSQL bool `json:"enableSQL"`
SqlType string `json:"sqlType"`
SqlAddress string `json:"sql-address"`
@ -16,7 +15,15 @@ type Config struct {
Database string `json:"database"`
DbUser string `json:"db-user"`
DbPassword string `json:"db-password"`
} `json:"sqlConfig"`
}
type AzureConfig struct {
FileshareName string `json:"fileshareName"`
StorageAccountName string `json:"storageAccountName"`
StorageAccountKey string `json:"storageAccountKey"`
}
type Config struct {
FolderToBackup []struct {
BackupName string `json:"backupName"`
FolderPath string `json:"folderPath"`
@ -37,6 +44,16 @@ func readConfig() []byte {
return file
}
func readSQLConfig() []byte {
logger := Logging.DetailedLogger("ConfigHandler", "readSQLConfig")
file, err := os.ReadFile("config/sql-config.json")
if err != nil {
logger.Fatal(err)
}
return file
}
func CheckIfConfigExists() {
logger := Logging.DetailedLogger("ConfigHandler", "CheckIfConfigExists")
@ -45,27 +62,68 @@ func CheckIfConfigExists(){
if fileErr != nil {
logger.Fatal(fileErr)
}
generateDefaultConfig()
fmt.Printf("No configs detected. Please use 'scabiosa generate-config'\n")
os.Exit(0)
}
}
func generateDefaultConfig() {
logger := Logging.DetailedLogger("ConfigHandler", "GenerateDefaultConfig")
func GenerateBaseConfig() {
logger := Logging.DetailedLogger("ConfigHandler", "GenerateBaseConfig")
var baseConfig Config
var config Config
var conf []byte
conf, err := json.MarshalIndent(config, "", "\t")
//conf, err := json.Marshal(config)
conf, err := json.MarshalIndent(baseConfig, "", "\t")
if err != nil {
logger.Fatal(err)
}
for _, s := range baseConfig.FolderToBackup {
s.BackupName = ""
}
err = os.WriteFile("config/config.json", conf, 0775)
if err != nil {
logger.Fatal(err)
}
err = os.WriteFile("config/config.json", conf, 0755)
}
func GenerateAzureConfig(azure AzureConfig) {
logger := Logging.DetailedLogger("ConfigHandler", "GenerateAzureConfig")
conf, err := json.MarshalIndent(azure, "", "\t")
if err != nil {
logger.Fatal(err)
}
err = os.WriteFile("config/azure.json", conf, 0775)
if err != nil {
logger.Fatal(err)
}
}
func GenerateSQLConfig(sqlConfig SQLConfig) {
logger := Logging.DetailedLogger("ConfigHandler", "GenerateSQLConfig")
conf, err := json.MarshalIndent(sqlConfig, "", "\t")
if err != nil {
logger.Fatal(err)
}
err = os.WriteFile("config/sql-config.json", conf, 0775)
if err != nil {
logger.Fatal(err)
}
}
func GetSQLConfig() SQLConfig {
logger := Logging.DetailedLogger("ConfigHandler", "GetSQLConfig")
var sqlConfig SQLConfig
err := json.Unmarshal(readSQLConfig(), &sqlConfig)
if err != nil {
logger.Fatal(err)
}
return sqlConfig
}
func GetConfig() Config {

View file

@ -1,6 +0,0 @@
{
"storageAccountName": "",
"storageAccountKey": "",
"fileshareName": "",
"targetDirectory": ""
}

View file

@ -1,21 +0,0 @@
{
"localBackupPath": "",
"sqlConfig": {
"enableSQL": false,
"sql-address": "",
"sql-port": 0,
"database": "",
"db-user": "",
"db-password": ""
},
"foldersToBackup": [
{
"backupName": "",
"folderPath": "",
"remoteStorageType": "",
"remoteTargetPath": "",
"createLocalBackup": false,
"localTargetPath": ""
}
]
}

4
go.mod
View file

@ -9,11 +9,13 @@ require (
github.com/cheggaaa/pb/v3 v3.0.8
github.com/go-sql-driver/mysql v1.6.0
github.com/google/uuid v1.3.0
github.com/urfave/cli/v2 v2.3.0
)
require (
github.com/Azure/azure-pipeline-go v0.2.1 // indirect
github.com/VividCortex/ewma v1.1.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149 // indirect
@ -21,6 +23,8 @@ require (
github.com/mattn/go-runewidth v0.0.12 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
golang.org/x/text v0.3.0 // indirect

11
go.sum
View file

@ -2,10 +2,13 @@ github.com/Azure/azure-pipeline-go v0.2.1 h1:OLBdZJ3yvOn2MezlWvbrBMTEUQC72zAftRZ
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
github.com/Azure/azure-storage-file-go v0.8.0 h1:OX8DGsleWLUE6Mw4R/OeWEZMvsTIpwN94J59zqKQnTI=
github.com/Azure/azure-storage-file-go v0.8.0/go.mod h1:3w3mufGcMjcOJ3w+4Gs+5wsSgkT7xDwWWqMMIrXtW4c=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/cheggaaa/pb/v3 v3.0.8 h1:bC8oemdChbke2FHIIGy9mn4DPJ2caZYQnfbRqwmdCoA=
github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
@ -35,10 +38,16 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@ -51,5 +60,7 @@ golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57i
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

78
main.go
View file

@ -1,74 +1,34 @@
package main
import (
"github.com/google/uuid"
"github.com/urfave/cli/v2"
"os"
"scabiosa/Compressor"
"scabiosa/Commands"
"scabiosa/Logging"
"scabiosa/SQL"
"scabiosa/StorageTypes"
"scabiosa/Tools"
"time"
)
func main() {
Tools.CheckIfConfigExists()
config := Tools.GetConfig()
logger := Logging.Logger("mainThread")
SQL.CreateDefaultTables(SQL.GetSQLInstance())
for _, backupItem := range config.FolderToBackup{
var storage StorageTypes.Storage
var destPath string
if backupItem.RemoteStorageType != "none"{
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
} else {
destPath = backupItem.LocalTargetPath
app := &cli.App{
Name: "scabiosa",
Usage: "Backup Util",
Authors: []*cli.Author{
{
Name: "netbenix",
Email: "netbenix@codenoodles.de",
},
},
Copyright: "(c) 2021-2022 netbenix",
Commands: []*cli.Command{
Commands.StartBackupProc(),
Commands.GenerateNewConfigs(),
},
}
bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
if backupItem.RemoteStorageType != "none"{
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
}
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none"{
backupItem.LocalTargetPath = "NONE"
_ = os.Remove(bakFile)
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now())
}
if backupItem.RemoteStorageType == "none" {
backupItem.CreateLocalBackup = true
backupItem.RemoteTargetPath = "NONE"
}
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
}
}
func getTimeSuffix() string{
currTime := time.Now()
return "_" + currTime.Format("02-01-2006_15-04")
}
func checkTmpPath(createLocalBackup bool, targetPath string) string{
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
if !createLocalBackup {
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
dirErr := os.Mkdir("tmp", 0775)
if dirErr != nil {
err := app.Run(os.Args)
if err != nil {
logger.Fatal(err)
}
}
return "tmp"
}
return targetPath
}