From 813dcded54d6072cf1b13850053f344bcef6a9cb Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 09:30:57 +0100 Subject: [PATCH 01/14] docs(README): Updated Readme --- README.md | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 9f64c57..c6255e7 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,11 @@ Trello board: [Click me!](https://trello.com/b/6zWLE6Jm) - Log the Backup progress to a database - Upload the files to a remote storage of your choice (see [Storage Types](#storage-types)) +## Planned features for the Future! +- Backup restore +- DB Upgrade +- Service for scheduled updates +- (Maybe) a web interface ## Database Types - MariaDB @@ -21,9 +26,9 @@ Trello board: [Click me!](https://trello.com/b/6zWLE6Jm) ## Storage types -- Local storage (soon) -- Azure Blob Storage (planned) +- Local storage - Azure File Share +- Azure Blob Storage (planned) - S3 Bucket (far future) - Dropbox (far future) - OneDrive (far future) @@ -32,24 +37,26 @@ Trello board: [Click me!](https://trello.com/b/6zWLE6Jm) | Storage Type | Config Type | |-------------------------|--------------------------| | Azure File Share | azure-fileshare | +| Local Storage | none | ## Config Explaination ### config.json -| Field | Type | Description | -|---------------------|:----------------:|------------------------------------------------| -| localBackupPath | string | Path where local backups are stored | -| **sqlConfig** | ---------------- | ---------------------------------------------- | -| enableSQL | boolean | Enable/Disables the SQL entries -| sqlType | string | See [DatabaseTypes](#database-types) | -| sql-address | string | Address to the SQL Server | -| sql-port | uint16 | SQL Server Port | -| database | string | Database name | -| db-user | string | SQL username from user which should be used | -| db-password | string | SQL password from user which should be used | -| **foldersToBackup** | ---------------- | ---------------------------------------------- | -| backupName | string | .bak file name | -| folderPath | string | Path to folder which should be backed up | -| storageType | string | See [StorageTypes](#storage-types) | -| createLocalBackup | boolean | Sets if .bak file should also be saved locally | \ No newline at end of file +| Field | Type | Description | +|---------------------------|:----------------:|------------------------------------------------| +| localBackupPath | string | Path where local backups are stored | +| **sqlConfig** | ---------------- | ---------------------------------------------- | +| enableSQL | boolean | Enable/Disables the SQL entries +| sqlType | string | See [DatabaseTypes](#database-types) | +| sql-address | string | Address to the SQL Server | +| sql-port | uint16 | SQL Server Port | +| database | string | Database name | +| db-user | string | SQL username from user which should be used | +| db-password | string | SQL password from user which should be used | +| **foldersToBackup** | ---------------- | ---------------------------------------------- | +| backupName | string | .bak file name | +| folderPath | string | Path to folder which should be backed up | +| remoteStorageType | string | See [StorageTypes](#storage-types) | +| targetPath | string | Sets the targetPath for local backups | +| createLocalBackup | boolean | Sets if .bak file should also be saved locally | \ No newline at end of file From bc5af92c2741f4ae9741c27b0da8ba0f5b79d2af Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 09:44:57 +0100 Subject: [PATCH 02/14] feat(Config): Generate config if non-existent --- Tools/Config.go | 33 ++++++++++++++++++++++++++++++++- main.go | 1 + 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Tools/Config.go b/Tools/Config.go index 49642d2..0f07684 100644 --- a/Tools/Config.go +++ b/Tools/Config.go @@ -36,9 +36,40 @@ func readConfig() []byte { return file } +func CheckIfConfigExists(){ + logger := Logging.DetailedLogger("ConfigHandler", "CheckIfConfigExists") + + if _, err := os.Stat("config/config.json"); os.IsNotExist(err){ + _, fileErr := os.OpenFile("config/config.json", os.O_CREATE, 0775) + if fileErr != nil{ + logger.Fatal(fileErr) + } + generateDefaultConfig() + } +} + +func generateDefaultConfig() { + logger := Logging.DetailedLogger("ConfigHandler", "GenerateDefaultConfig") + + var config Config + var conf []byte + + conf, err := json.MarshalIndent(config, "", "\t") + //conf, err := json.Marshal(config) + if err != nil { + logger.Fatal(err) + } + + err = os.WriteFile("config/config.json", conf, 0755) + if err != nil { + logger.Fatal(err) + } + +} func GetConfig() Config { - logger := Logging.DetailedLogger("ConfigHandler", "GetConfig()") + + logger := Logging.DetailedLogger("ConfigHandler", "GetConfig") var config Config err := json.Unmarshal(readConfig(), &config) diff --git a/main.go b/main.go index 0d7157e..27e63b3 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( ) func main(){ + Tools.CheckIfConfigExists() config := Tools.GetConfig() SQL.CreateDefaultTables(SQL.GetSQLInstance()) From 2dc09f096c99da06a84efb3289d13fe631699ee4 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 09:52:16 +0100 Subject: [PATCH 03/14] style(MariaDB): Made default SQL look more 'nice' --- SQL/MariaDBConnector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index ccd3ad3..6a42052 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -61,8 +61,8 @@ func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB{ func (mariadb MariaDBConnector) createDefaultTables(){ logger := Logging.DetailedLogger("MariaDB", "createDefaultTables") - eventLogSQL := "create table " + mariadb.Database +".EventLog\n(\n UUID text null,\n LogType enum ('INFO', 'WARNING', 'ERROR', 'FATAL') null,\n BackupName varchar(256) null,\n Stage enum ('COMPRESS', 'UPLOAD', 'DELETE TMP') null,\n RemoteStorage enum ('AZURE-FILE', 'AZURE-BLOB', 'NONE') null,\n Description text null,\n Timestamp datetime null\n);" - backupSQL := "create table " + mariadb.Database +".Backups\n(\n UUID text null,\n BackupName varchar(256) null,\n LastBackup datetime null,\n LocalBackup tinyint(1) null,\n FilePath varchar(256) null,\n RemoteStorage enum ('AZURE-FILE', 'AZURE-BLOB', 'NONE') null,\n RemotePath varchar(256) null\n);\n\n" + eventLogSQL := "create table " + mariadb.Database +".EventLog(UUID text null, LogType enum ('INFO', 'WARNING', 'ERROR', 'FATAL') 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, 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);" db := createMariaDBConnection(mariadb) From 9c8549ac7228803a0b790005074b8f7b87160d65 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 09:56:18 +0100 Subject: [PATCH 04/14] feat(MariaDB): Added Hostname to tables --- SQL/MariaDBConnector.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 6a42052..34e6490 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -4,6 +4,7 @@ import ( "database/sql" _ "github.com/go-sql-driver/mysql" "github.com/google/uuid" + "os" "scabiosa/Logging" "scabiosa/Tools" "strconv" @@ -61,10 +62,9 @@ func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB{ 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, 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, 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);" - - + 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);" + db := createMariaDBConnection(mariadb) if !checkIfBackupTableExist(db, mariadb){ @@ -88,7 +88,9 @@ func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, bac logger := Logging.DetailedLogger("MariaDB", "newLogEntry") db := createMariaDBConnection(mariadb) - _, err := db.Query("INSERT INTO `" + mariadb.Database + "`.EventLog VALUES (?, ?, ?, ?, ?, ?, ?);", uuid.String(), strconv.FormatInt(int64(logType), 10), backupName, stage, strconv.FormatInt(int64(storageType), 10), description ,timestamp) + 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{ logger.Fatal(err) } @@ -98,16 +100,17 @@ func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, bac func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string){ logger := Logging.DetailedLogger("MariaDB", "newBackupEntry") - db := createMariaDBConnection(mariadb) + hostname, _ := os.Hostname() + if checkIfBackupEntryExist(db, mariadb, backupName){ _, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE BackupName = ?;", lastBackup, backupName) if err != nil { logger.Fatal(err) } } else { - _, err := db.Query("INSERT INTO `" + mariadb.Database + "`.Backups VALUES (?, ?, ?, ?, ?, ?, ?);", uuid.New(), backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath) + _, err := db.Query("INSERT INTO `" + mariadb.Database + "`.Backups VALUES (?, ?, ?, ?, ?, ?, ?, ?);", uuid.New(), hostname, backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath) if err != nil { logger.Fatal(err) } From 2c421ffbf43110889ea0e77021153776b5b00aaa Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 10:39:05 +0100 Subject: [PATCH 05/14] style(MariaDB): Removes some empty lines --- SQL/MariaDBConnector.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 34e6490..29eef9d 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -31,7 +31,6 @@ func GetMariaDBInstance(config Tools.Config) MariaDBConnector { 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 } @@ -64,7 +63,7 @@ func (mariadb MariaDBConnector) 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);" - + db := createMariaDBConnection(mariadb) if !checkIfBackupTableExist(db, mariadb){ @@ -97,7 +96,6 @@ func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, bac } - func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string){ logger := Logging.DetailedLogger("MariaDB", "newBackupEntry") db := createMariaDBConnection(mariadb) From 46b0c6013f76314b1dcaa77e79b091a5a871f699 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 10:40:10 +0100 Subject: [PATCH 06/14] docs(README): Updated Readme --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index c6255e7..55b0b6b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ Trello board: [Click me!](https://trello.com/b/6zWLE6Jm) ## Planned features for the Future! - Backup restore -- DB Upgrade - Service for scheduled updates - (Maybe) a web interface From 524f83ec00470b0a334b3d2324a0c214089a875a Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:02:48 +0100 Subject: [PATCH 07/14] refactor(Config): Renamed targetPath to LocalTargetPath --- Tools/Config.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tools/Config.go b/Tools/Config.go index 0f07684..87feb19 100644 --- a/Tools/Config.go +++ b/Tools/Config.go @@ -21,8 +21,9 @@ type Config struct { BackupName string `json:"backupName"` FolderPath string `json:"folderPath"` RemoteStorageType string `json:"remoteStorageType"` - TargetPath string `json:"targetPath"` - CreateLocalBackup bool `json:"createLocalBackup"` + RemoteTargetPath string `json:"remoteTargetPath"` + CreateLocalBackup bool `json:"createLocalBackup"` + LocalTargetPath string `json:"LocalTargetPath"` } `json:"foldersToBackup"` } From 1f32937baf5486054767f6580e639d6a9d147e1b Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:03:49 +0100 Subject: [PATCH 08/14] refactor(Config): Changed order --- config/config.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/config/config.json b/config/config.json index 21652af..6cbb73d 100644 --- a/config/config.json +++ b/config/config.json @@ -12,9 +12,10 @@ { "backupName": "", "folderPath": "", - "storageType": "", - "targetPath": "", - "createLocalBackup": false + "remoteStorageType": "", + "remoteTargetPath": "", + "createLocalBackup": false, + "localTargetPath": "" } ] } \ No newline at end of file From ab27acb960d2bdd9f21bf3a372eb078d1df99223 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:04:33 +0100 Subject: [PATCH 09/14] fix(Compression): No longer shows wrong Name in output --- Compressor/Compression.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Compressor/Compression.go b/Compressor/Compression.go index 54d0d3f..eba1707 100644 --- a/Compressor/Compression.go +++ b/Compressor/Compression.go @@ -39,7 +39,7 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){ zr, _ := gzip.NewWriterLevel(fileToWrite, flate.BestCompression) tw := tar.NewWriter(zr) - go fmt.Printf("[%s] Start compression...\n", filepath.Base(folderPath)) + go fmt.Printf("[%s] Start compression...\n", backupName) SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, 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) @@ -60,7 +60,7 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){ logger.Fatal(err) } - go fmt.Printf("[%s] Compressing: %s (%d bytes)\n", filepath.Base(folderPath) ,relPath, fi.Size()) + go fmt.Printf("[%s] Compressing: %s (%d bytes)\n", backupName, relPath, fi.Size()) if _, err := io.Copy(tw, data); err != nil { logger.Fatal(err) } @@ -78,6 +78,6 @@ func compress(fileToWrite *os.File, folderPath string, backupName string){ } - go fmt.Printf("[%s] Compression Done.\n", filepath.Base(folderPath)) + go fmt.Printf("[%s] Compression Done.\n", backupName) SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now()) } \ No newline at end of file From 23c2bc3226f15801dacea39da31724d33fad6e76 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:04:56 +0100 Subject: [PATCH 10/14] feat(SQL): Implemented LocalTargetPath --- SQL/MariaDBConnector.go | 14 +++++++------- SQL/SQLInterface.go | 6 +++--- main.go | 13 +++++++------ 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 29eef9d..44816a2 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -43,8 +43,8 @@ func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool { return true } -func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string) bool { - rows, _ := db.Query("SELECT * FROM `" + mariadb.Database + "`.Backups WHERE BackupName = '" + backupName + "';") +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; } return true } @@ -62,7 +62,7 @@ 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);" + 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) @@ -96,19 +96,19 @@ func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, bac } -func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath 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){ - _, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE BackupName = ?;", lastBackup, backupName) + if checkIfBackupEntryExist(db, mariadb, backupName, hostname){ + _, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE Hostname = ? AND BackupName = ?;", lastBackup, 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) + _, 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) } diff --git a/SQL/SQLInterface.go b/SQL/SQLInterface.go index a80ce67..8137b5c 100644 --- a/SQL/SQLInterface.go +++ b/SQL/SQLInterface.go @@ -9,7 +9,7 @@ import ( type SQLService interface { createDefaultTables() newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) - newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string) + newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) } func CreateDefaultTables(sqlService SQLService){ @@ -26,10 +26,10 @@ func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupN } } -func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string){ +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{ - sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath) + sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath) } } diff --git a/main.go b/main.go index 27e63b3..17399c1 100644 --- a/main.go +++ b/main.go @@ -24,22 +24,23 @@ func main(){ if backupItem.RemoteStorageType != "none"{ storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType) - destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.TargetPath) + destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath) } else { - destPath = backupItem.TargetPath + 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.TargetPath) + StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath) } if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "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()) - } - SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), StorageTypes.GetAzureStorage().TargetDirectory) + SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now()) + } + + SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.LocalTargetPath, backupItem.LocalTargetPath) } } From 1a1bcd171b6e7938a9f4315dd4088d47f3d94231 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:06:58 +0100 Subject: [PATCH 11/14] fix(MariaDB): Fixed error when creating default table That happend when the schema name has special charaters in it --- SQL/MariaDBConnector.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 44816a2..04bc603 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -61,8 +61,8 @@ func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB{ 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) From fb105ecf7d80d6edd9e89a0cc8e2b6cb9b1d8afb Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:10:14 +0100 Subject: [PATCH 12/14] fix(SQL): LocalTargetPath is no longer in RemoteTargetPath --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 17399c1..3da8e2d 100644 --- a/main.go +++ b/main.go @@ -40,7 +40,7 @@ func main(){ SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now()) } - SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.LocalTargetPath, backupItem.LocalTargetPath) + SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath) } } From af2124dbc378d4b167abe98f3a126213cb68ba9f Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:18:55 +0100 Subject: [PATCH 13/14] fix(SQL): Added safety measures if StorageType = none --- main.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.go b/main.go index 3da8e2d..4e15540 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,10 @@ func main(){ 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) } From a0d8162f3ead8f5263e9ffc00564e0ff8af6c520 Mon Sep 17 00:00:00 2001 From: netbenix Date: Thu, 23 Dec 2021 11:19:26 +0100 Subject: [PATCH 14/14] feat(MariaDB): BackupEntry now always gets an Update on multiple fields --- SQL/MariaDBConnector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SQL/MariaDBConnector.go b/SQL/MariaDBConnector.go index 04bc603..864e338 100644 --- a/SQL/MariaDBConnector.go +++ b/SQL/MariaDBConnector.go @@ -103,7 +103,7 @@ func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup tim hostname, _ := os.Hostname() if checkIfBackupEntryExist(db, mariadb, backupName, hostname){ - _, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE Hostname = ? AND BackupName = ?;", lastBackup, hostname, backupName) + _, 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) }