From 465c0e7973df59c729459f53e85b37f3fc7407d5 Mon Sep 17 00:00:00 2001 From: netbenix Date: Tue, 23 Nov 2021 08:22:39 +0100 Subject: [PATCH 1/5] Moved MariaDBConnector and Compression do modules --- SQL/{MariaDB.go => MariaDBConnector.go} | 0 Compression.go => Tools/Compression.go | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename SQL/{MariaDB.go => MariaDBConnector.go} (100%) rename Compression.go => Tools/Compression.go (100%) diff --git a/SQL/MariaDB.go b/SQL/MariaDBConnector.go similarity index 100% rename from SQL/MariaDB.go rename to SQL/MariaDBConnector.go diff --git a/Compression.go b/Tools/Compression.go similarity index 100% rename from Compression.go rename to Tools/Compression.go From 6f95fedbc1fecb79d2c71014123a2f57ea449429 Mon Sep 17 00:00:00 2001 From: netbenix Date: Tue, 23 Nov 2021 08:23:06 +0100 Subject: [PATCH 2/5] Added function to get selected sql instance --- SQL/SQLInterface.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/SQL/SQLInterface.go b/SQL/SQLInterface.go index 79f69ee..a53d111 100644 --- a/SQL/SQLInterface.go +++ b/SQL/SQLInterface.go @@ -2,6 +2,7 @@ package SQL import ( "github.com/google/uuid" + "scabiosa/Tools" "time" ) @@ -21,4 +22,14 @@ func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupN func NewBackupEntry(sqlService SQLService, uuid uuid.UUID, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, durationToBackup time.Duration, hadErrors bool){ sqlService.newBackupEntry(uuid, backupName, lastBackup, localBackup, filePath, storageType, remotePath, durationToBackup, hadErrors) +} + +func GetSQLInstance() SQLService{ + config := Tools.GetConfig() + + switch config.SQLConfig.SqlType { + case "mariadb": {return GetMariaDBInstance()} + } + + return nil } \ No newline at end of file From a23791daa5d245b39d15c8f516eedfd0c506e21d Mon Sep 17 00:00:00 2001 From: netbenix Date: Tue, 23 Nov 2021 08:23:18 +0100 Subject: [PATCH 3/5] Chaged static sql instance to dynamic instance --- StorageTypes/AzureFileStorage.go | 6 ++---- Tools/Compression.go | 11 ++++------- main.go | 5 ++--- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/StorageTypes/AzureFileStorage.go b/StorageTypes/AzureFileStorage.go index bfcb84f..73837e1 100644 --- a/StorageTypes/AzureFileStorage.go +++ b/StorageTypes/AzureFileStorage.go @@ -49,8 +49,7 @@ func (azure AzureFileStorage) upload(fileName string){ ctx := context.Background() fmt.Printf("[%s] Starting upload to Azure File Share...\n", strings.Trim(filepath.Base(fileName), ".bak")) - //TODO Remove Hardcoded SQL Instance - SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now()) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now()) err = azfile.UploadFileToAzureFile(ctx, file, fileURL, azfile.UploadToAzureFileOptions{ @@ -63,8 +62,7 @@ func (azure AzureFileStorage) upload(fileName string){ }}) fmt.Printf("[%s] Upload finished.\n", strings.Trim(filepath.Base(fileName), ".bak")) - //TODO Remove Hardcoded SQL Instance - SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Finished upload.", time.Now()) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Finished upload.", time.Now()) } func readConfig() []byte { diff --git a/Tools/Compression.go b/Tools/Compression.go index 957d66d..0a9cd27 100644 --- a/Tools/Compression.go +++ b/Tools/Compression.go @@ -1,4 +1,4 @@ -package main +package Tools import ( "archive/tar" @@ -32,8 +32,7 @@ func CreateBakFile(filename string, folderPath string, destinationPath string) s logger.Fatal(err) } - //TODO Remove Hardcoded SQL Instance - SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now()) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now()) return fileName @@ -47,8 +46,7 @@ func compress(folderPath string, buf io.Writer){ tw := tar.NewWriter(zr) fmt.Printf("[%s] Start compression...\n", filepath.Base(folderPath)) - //TODO Remove Hardcoded SQL Instance - SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Start compression", time.Now()) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Start compression", time.Now()) filepath.Walk(folderPath, func(file string, fi os.FileInfo, err error) error { header, err := tar.FileInfoHeader(fi, file) if err != nil { @@ -86,6 +84,5 @@ func compress(folderPath string, buf io.Writer){ fmt.Printf("[%s] Compression Done.\n", filepath.Base(folderPath)) - //TODO Remove Hardcoded SQL Instance - SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now()) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now()) } \ No newline at end of file diff --git a/main.go b/main.go index 3a82cbc..a675089 100644 --- a/main.go +++ b/main.go @@ -19,13 +19,12 @@ func main(){ storage := StorageTypes.CheckStorageType(backupItem.StorageType) destPath := checkTmpPath(config, backupItem.CreateLocalBackup) - bakFile := CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath) + bakFile := Tools.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath) StorageTypes.UploadFile(storage, destPath + string(os.PathSeparator) + bakFile) if !backupItem.CreateLocalBackup { _ = os.Remove(destPath + string(os.PathSeparator) + bakFile) - //TODO Remove Hardcoded SQL Instance - SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now()) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now()) } } From 63faa60a9c0615ae7e12f1e627d9a875adaa6f11 Mon Sep 17 00:00:00 2001 From: netbenix Date: Tue, 23 Nov 2021 08:57:01 +0100 Subject: [PATCH 4/5] Little refactor --- {Tools => Compressor}/Compression.go | 2 +- SQL/MariaDBConnector.go | 3 +-- SQL/SQLInterface.go | 2 +- main.go | 5 +++-- 4 files changed, 6 insertions(+), 6 deletions(-) rename {Tools => Compressor}/Compression.go (99%) diff --git a/Tools/Compression.go b/Compressor/Compression.go similarity index 99% rename from Tools/Compression.go rename to Compressor/Compression.go index 0a9cd27..fd8f25b 100644 --- a/Tools/Compression.go +++ b/Compressor/Compression.go @@ -1,4 +1,4 @@ -package Tools +package Compressor import ( "archive/tar" diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 905a4aa..bd4b0c4 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -18,8 +18,7 @@ type MariaDBConnector struct { DbPassword string } -func GetMariaDBInstance() MariaDBConnector{ - config := Tools.GetConfig() +func GetMariaDBInstance(config Tools.Config) MariaDBConnector { var mariadb MariaDBConnector mariadb.Address = config.SQLConfig.SqlAddress diff --git a/SQL/SQLInterface.go b/SQL/SQLInterface.go index a53d111..b597a51 100644 --- a/SQL/SQLInterface.go +++ b/SQL/SQLInterface.go @@ -28,7 +28,7 @@ func GetSQLInstance() SQLService{ config := Tools.GetConfig() switch config.SQLConfig.SqlType { - case "mariadb": {return GetMariaDBInstance()} + case "mariadb": {return GetMariaDBInstance(config)} } return nil diff --git a/main.go b/main.go index a675089..28e50f9 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/google/uuid" "os" + "scabiosa/Compressor" "scabiosa/Logging" "scabiosa/SQL" "scabiosa/StorageTypes" @@ -13,13 +14,13 @@ import ( func main(){ config := Tools.GetConfig() - SQL.CreateDefaultTables(SQL.GetMariaDBInstance()) + SQL.CreateDefaultTables(SQL.GetSQLInstance()) for _, backupItem := range config.FolderToBackup{ storage := StorageTypes.CheckStorageType(backupItem.StorageType) destPath := checkTmpPath(config, backupItem.CreateLocalBackup) - bakFile := Tools.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath) + bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath) StorageTypes.UploadFile(storage, destPath + string(os.PathSeparator) + bakFile) if !backupItem.CreateLocalBackup { From d01230c2c8364ac3af6cde22b910caf0183753f3 Mon Sep 17 00:00:00 2001 From: netbenix Date: Tue, 23 Nov 2021 09:40:33 +0100 Subject: [PATCH 5/5] Updated README.md --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 86ec1f9..f1c1107 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,19 @@ Please keep in mind that this project is WIP. ## What can it do? - Backup you stuff via a dynamic configuration (done!) -- Log the Backup progress to a database (planned) +- Log the Backup progress to a database (in progress) - Upload the files to a remote storage of your choice (see [Storage Types](#storage-types)) ## Database Types -- MariaDB (soon) -- MySQL (far future) +- MariaDB (done!) +- MySQL (soon) - MS-SQL (far future) +| Database Type | Config Type | +|-------------------|---------------------------| +| MariaDB | mariadb | + ## Storage types - Local storage (soon) @@ -35,7 +39,7 @@ Please keep in mind that this project is WIP. |---------------------|:----------------:|------------------------------------------------| | localBackupPath | string | Path where local backups are stored | | **sqlConfig** | ---------------- | ---------------------------------------------- | -| sqlType | string | SQL Server Type (not yet used) | +| sqlType | string | See [DatabaseTypes](#database-types) | | sql-address | string | Address to the SQL Server | | sql-port | uint16 | SQL Server Port | | database | string | Database name |