diff --git a/.gitignore b/.gitignore index f4d99cc..6db1cf6 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,6 @@ scabiosa #tmp folder tmp/ + +#config folder +config/ \ No newline at end of file diff --git a/Commands/GenerateDefaultConfigs.go b/Commands/GenerateDefaultConfigs.go new file mode 100644 index 0000000..3e35915 --- /dev/null +++ b/Commands/GenerateDefaultConfigs.go @@ -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 + }, + } +} diff --git a/Commands/StartBackupProc.go b/Commands/StartBackupProc.go new file mode 100644 index 0000000..97ee12a --- /dev/null +++ b/Commands/StartBackupProc.go @@ -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 +} diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 864e338..55b14c9 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -12,70 +12,76 @@ import ( ) type MariaDBConnector struct { - Address string - Port uint16 - Database string - DbUser string + Address string + Port uint16 + Database string + DbUser string 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; } + rows, _ := db.Query("SELECT * FROM `"+mariadb.Database+"`.Backups WHERE Hostname = ? AND BackupName = ?;", hostname, backupName) + if !rows.Next() { + return false + } return true } -func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB{ +func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB { logger := Logging.DetailedLogger("MariaDB", "createConnection") - db, err := sql.Open("mysql", mariadb.DbUser + ":" + mariadb.DbPassword + "@(" + mariadb.Address + ":" +strconv.Itoa(int(mariadb.Port))+ ")/" + mariadb.Database) - if err != nil{ + db, err := sql.Open("mysql", mariadb.DbUser+":"+mariadb.DbPassword+"@("+mariadb.Address+":"+strconv.Itoa(int(mariadb.Port))+")/"+mariadb.Database) + if err != nil { logger.Fatal(err) } return db } -func (mariadb MariaDBConnector) createDefaultTables(){ +func (mariadb MariaDBConnector) createDefaultTables() { logger := Logging.DetailedLogger("MariaDB", "createDefaultTables") - eventLogSQL := "create table `" + mariadb.Database +"`.EventLog(UUID text null, LogType enum ('INFO', 'WARNING', 'ERROR', 'FATAL') null, Hostname varchar(256) null,BackupName varchar(256) null, Stage enum ('COMPRESS', 'UPLOAD', 'DELETE TMP') null, RemoteStorage enum ('AZURE-FILE', 'AZURE-BLOB', 'NONE') null, Description text null, Timestamp datetime null);" - backupSQL := "create table `" + mariadb.Database +"`.Backups(UUID text null, Hostname varchar(256) null, BackupName varchar(256) null, LastBackup datetime null, LocalBackup tinyint(1) null, FilePath varchar(256) null, RemoteStorage enum ('AZURE-FILE', 'AZURE-BLOB', 'NONE') null, RemotePath varchar(256) null, LocalPath varchar(256) null);" + eventLogSQL := "create table `" + mariadb.Database + "`.EventLog(UUID text null, LogType enum ('INFO', 'WARNING', 'ERROR', 'FATAL') null, Hostname varchar(256) null,BackupName varchar(256) null, Stage enum ('COMPRESS', 'UPLOAD', 'DELETE TMP') null, RemoteStorage enum ('AZURE-FILE', 'AZURE-BLOB', 'NONE') null, Description text null, Timestamp datetime null);" + backupSQL := "create table `" + mariadb.Database + "`.Backups(UUID text null, Hostname varchar(256) null, BackupName varchar(256) null, LastBackup datetime null, LocalBackup tinyint(1) null, FilePath varchar(256) null, RemoteStorage enum ('AZURE-FILE', 'AZURE-BLOB', 'NONE') null, RemotePath varchar(256) null, LocalPath varchar(256) null);" db := createMariaDBConnection(mariadb) - if !checkIfBackupTableExist(db, mariadb){ + if !checkIfBackupTableExist(db, mariadb) { _, err := db.Exec(backupSQL) - if err != nil{ + if err != nil { logger.Fatal(err) } } - if !checkIfEventLogTableExist(db, mariadb){ + if !checkIfEventLogTableExist(db, mariadb) { _, err := db.Exec(eventLogSQL) - if err != nil{ + if err != nil { logger.Fatal(err) } } @@ -83,34 +89,34 @@ func (mariadb MariaDBConnector) createDefaultTables(){ _ = db.Close() } -func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time){ +func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) { logger := Logging.DetailedLogger("MariaDB", "newLogEntry") db := createMariaDBConnection(mariadb) hostname, _ := os.Hostname() - _, err := db.Query("INSERT INTO `" + mariadb.Database + "`.EventLog VALUES (?, ?, ?, ?, ?, ?, ?, ?);", uuid.String(), strconv.FormatInt(int64(logType), 10), hostname, backupName, stage, strconv.FormatInt(int64(storageType), 10), description ,timestamp) - if err != nil{ + _, err := db.Query("INSERT INTO `"+mariadb.Database+"`.EventLog VALUES (?, ?, ?, ?, ?, ?, ?, ?);", uuid.String(), strconv.FormatInt(int64(logType), 10), hostname, backupName, stage, strconv.FormatInt(int64(storageType), 10), description, timestamp) + if err != nil { logger.Fatal(err) } } -func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string){ +func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) { logger := Logging.DetailedLogger("MariaDB", "newBackupEntry") db := createMariaDBConnection(mariadb) hostname, _ := os.Hostname() - if checkIfBackupEntryExist(db, mariadb, backupName, hostname){ - _, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ?, LocalBackup = ?, RemoteStorage = ?, RemotePath = ?, LocalPath = ? WHERE Hostname = ? AND BackupName = ?;",lastBackup, localBackup, strconv.FormatInt(int64(storageType), 10), remotePath, localPath, hostname, backupName) + if checkIfBackupEntryExist(db, mariadb, backupName, hostname) { + _, err := db.Query("UPDATE `"+mariadb.Database+"`.Backups SET LastBackup = ?, LocalBackup = ?, RemoteStorage = ?, RemotePath = ?, LocalPath = ? WHERE Hostname = ? AND BackupName = ?;", lastBackup, localBackup, strconv.FormatInt(int64(storageType), 10), remotePath, localPath, hostname, backupName) if err != nil { logger.Fatal(err) } } else { - _, err := db.Query("INSERT INTO `" + mariadb.Database + "`.Backups VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", uuid.New(), hostname, backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath, localPath) + _, err := db.Query("INSERT INTO `"+mariadb.Database+"`.Backups VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", uuid.New(), hostname, backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath, localPath) if err != nil { logger.Fatal(err) } } -} \ No newline at end of file +} diff --git a/SQL/SQLInterface.go b/SQL/SQLInterface.go index 8137b5c..fd03e1c 100644 --- a/SQL/SQLInterface.go +++ b/SQL/SQLInterface.go @@ -12,35 +12,40 @@ type SQLService interface { newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) } -func CreateDefaultTables(sqlService SQLService){ - config := Tools.GetConfig() - if config.SQLConfig.EnableSQL{ +func CreateDefaultTables(sqlService SQLService) { + 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{ +func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) { + 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{ +func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) { + sqlConfig := Tools.GetSQLConfig() + if sqlConfig.EnableSQL { sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath) } } -func GetSQLInstance() SQLService{ - config := Tools.GetConfig() +func GetSQLInstance() SQLService { + 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 -} \ No newline at end of file +} diff --git a/StorageTypes/AzureFileStorage.go b/StorageTypes/AzureFileStorage.go index 142be30..510384a 100644 --- a/StorageTypes/AzureFileStorage.go +++ b/StorageTypes/AzureFileStorage.go @@ -12,19 +12,18 @@ 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"` +type AzureFileStorage struct { + FileshareName string + StorageAccountName string + StorageAccountKey string } - -func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string){ +func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string) { logger := Logging.DetailedLogger("AzureFileStorage", "upload") file, err := os.Open(fileName) @@ -39,15 +38,11 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina } credential, err := azfile.NewSharedKeyCredential(azure.StorageAccountName, azure.StorageAccountKey) - if err != nil{ + if err != nil { 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,21 +51,20 @@ 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, azfile.UploadToAzureFileOptions{ - Parallelism: 3, - FileHTTPHeaders: azfile.FileHTTPHeaders{ - CacheControl: "no-transform", - }, - Progress: func(bytesTransferred int64){ - progressBar.SetCurrent(bytesTransferred) - //fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(backupName, ".bak") ,bytesTransferred, fileSize.Size()) - }}) + Parallelism: 3, + FileHTTPHeaders: azfile.FileHTTPHeaders{ + CacheControl: "no-transform", + }, + Progress: func(bytesTransferred int64) { + progressBar.SetCurrent(bytesTransferred) + //fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(backupName, ".bak") ,bytesTransferred, fileSize.Size()) + }}) - if err != nil{ + if err != nil { logger.Fatal(err) } progressBar.Finish() @@ -82,23 +76,27 @@ func readConfig() []byte { logger := Logging.DetailedLogger("AzureFileStorage", "readConfig") file, err := os.ReadFile("config/azure.json") - if err != nil{ + if err != nil { logger.Fatal(err) } 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) - if jsonErr != nil{ + jsonErr := json.Unmarshal(readConfig(), &azureConfig) + if jsonErr != nil { logger.Fatal(jsonErr) } - return azureStorage -} \ No newline at end of file + azureFileShare.StorageAccountName = azureConfig.StorageAccountName + azureFileShare.StorageAccountKey = azureConfig.StorageAccountKey + azureFileShare.FileshareName = azureConfig.FileshareName + + return azureFileShare +} diff --git a/Tools/Config.go b/Tools/Config.go index 87feb19..aa785f4 100644 --- a/Tools/Config.go +++ b/Tools/Config.go @@ -2,26 +2,33 @@ package Tools import ( "encoding/json" + "fmt" "os" "scabiosa/Logging" ) +type SQLConfig struct { + EnableSQL bool `json:"enableSQL"` + SqlType string `json:"sqlType"` + SqlAddress string `json:"sql-address"` + SqlPort uint16 `json:"sql-port"` + Database string `json:"database"` + DbUser string `json:"db-user"` + DbPassword string `json:"db-password"` +} + +type AzureConfig struct { + FileshareName string `json:"fileshareName"` + StorageAccountName string `json:"storageAccountName"` + StorageAccountKey string `json:"storageAccountKey"` +} + type Config struct { - LocalBackupPath string `json:"localBackupPath"` - SQLConfig struct{ - EnableSQL bool `json:"enableSQL"` - SqlType string `json:"sqlType"` - SqlAddress string `json:"sql-address"` - SqlPort uint16 `json:"sql-port"` - Database string `json:"database"` - DbUser string `json:"db-user"` - DbPassword string `json:"db-password"` - } `json:"sqlConfig"` - FolderToBackup []struct{ - BackupName string `json:"backupName"` - FolderPath string `json:"folderPath"` + FolderToBackup []struct { + BackupName string `json:"backupName"` + FolderPath string `json:"folderPath"` RemoteStorageType string `json:"remoteStorageType"` - RemoteTargetPath string `json:"remoteTargetPath"` + RemoteTargetPath string `json:"remoteTargetPath"` CreateLocalBackup bool `json:"createLocalBackup"` LocalTargetPath string `json:"LocalTargetPath"` } `json:"foldersToBackup"` @@ -37,35 +44,86 @@ func readConfig() []byte { return file } -func CheckIfConfigExists(){ +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") - if _, err := os.Stat("config/config.json"); os.IsNotExist(err){ + if _, err := os.Stat("config/config.json"); os.IsNotExist(err) { _, fileErr := os.OpenFile("config/config.json", os.O_CREATE, 0775) - if fileErr != nil{ + 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 { @@ -79,4 +137,4 @@ func GetConfig() Config { } return config -} \ No newline at end of file +} diff --git a/config/azure.json b/config/azure.json deleted file mode 100644 index 5ca6995..0000000 --- a/config/azure.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "storageAccountName": "", - "storageAccountKey": "", - "fileshareName": "", - "targetDirectory": "" -} \ No newline at end of file diff --git a/config/config.json b/config/config.json deleted file mode 100644 index 6cbb73d..0000000 --- a/config/config.json +++ /dev/null @@ -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": "" - } - ] -} \ No newline at end of file diff --git a/go.mod b/go.mod index 2a6d031..230aab4 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5bcab7a..feb7c0b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index 5f590a0..f6dbfa8 100644 --- a/main.go +++ b/main.go @@ -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() +func main() { + logger := Logging.Logger("mainThread") - SQL.CreateDefaultTables(SQL.GetSQLInstance()) + 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(), + }, + } - 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) + err := app.Run(os.Args) + if err != nil { + logger.Fatal(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 -}