Refactoring

SQL Interface
This commit is contained in:
netbenix 2021-11-23 09:42:15 +01:00 committed by GitHub
commit 8a2174ba7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 30 additions and 21 deletions

View file

@ -1,4 +1,4 @@
package main package Compressor
import ( import (
"archive/tar" "archive/tar"
@ -32,8 +32,7 @@ func CreateBakFile(filename string, folderPath string, destinationPath string) s
logger.Fatal(err) logger.Fatal(err)
} }
//TODO Remove Hardcoded SQL Instance SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now())
SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "File successfully written.", time.Now())
return fileName return fileName
@ -47,8 +46,7 @@ func compress(folderPath string, buf io.Writer){
tw := tar.NewWriter(zr) tw := tar.NewWriter(zr)
fmt.Printf("[%s] Start compression...\n", filepath.Base(folderPath)) fmt.Printf("[%s] Start compression...\n", filepath.Base(folderPath))
//TODO Remove Hardcoded SQL Instance SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Start compression", time.Now())
SQL.NewLogEntry(SQL.GetMariaDBInstance(), 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 { filepath.Walk(folderPath, func(file string, fi os.FileInfo, err error) error {
header, err := tar.FileInfoHeader(fi, file) header, err := tar.FileInfoHeader(fi, file)
if err != nil { if err != nil {
@ -86,6 +84,5 @@ func compress(folderPath string, buf io.Writer){
fmt.Printf("[%s] Compression Done.\n", filepath.Base(folderPath)) fmt.Printf("[%s] Compression Done.\n", filepath.Base(folderPath))
//TODO Remove Hardcoded SQL Instance SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now())
SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now())
} }

View file

@ -4,15 +4,19 @@ Please keep in mind that this project is WIP.
## What can it do? ## What can it do?
- Backup you stuff via a dynamic configuration (done!) - 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)) - Upload the files to a remote storage of your choice (see [Storage Types](#storage-types))
## Database Types ## Database Types
- MariaDB (soon) - MariaDB (done!)
- MySQL (far future) - MySQL (soon)
- MS-SQL (far future) - MS-SQL (far future)
| Database Type | Config Type |
|-------------------|---------------------------|
| MariaDB | mariadb |
## Storage types ## Storage types
- Local storage (soon) - Local storage (soon)
@ -35,7 +39,7 @@ Please keep in mind that this project is WIP.
|---------------------|:----------------:|------------------------------------------------| |---------------------|:----------------:|------------------------------------------------|
| localBackupPath | string | Path where local backups are stored | | localBackupPath | string | Path where local backups are stored |
| **sqlConfig** | ---------------- | ---------------------------------------------- | | **sqlConfig** | ---------------- | ---------------------------------------------- |
| sqlType | string | SQL Server Type (not yet used) | | sqlType | string | See [DatabaseTypes](#database-types) |
| sql-address | string | Address to the SQL Server | | sql-address | string | Address to the SQL Server |
| sql-port | uint16 | SQL Server Port | | sql-port | uint16 | SQL Server Port |
| database | string | Database name | | database | string | Database name |

View file

@ -18,8 +18,7 @@ type MariaDBConnector struct {
DbPassword string DbPassword string
} }
func GetMariaDBInstance() MariaDBConnector{ func GetMariaDBInstance(config Tools.Config) MariaDBConnector {
config := Tools.GetConfig()
var mariadb MariaDBConnector var mariadb MariaDBConnector
mariadb.Address = config.SQLConfig.SqlAddress mariadb.Address = config.SQLConfig.SqlAddress

View file

@ -2,6 +2,7 @@ package SQL
import ( import (
"github.com/google/uuid" "github.com/google/uuid"
"scabiosa/Tools"
"time" "time"
) )
@ -22,3 +23,13 @@ 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){ 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) 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(config)}
}
return nil
}

View file

@ -49,8 +49,7 @@ func (azure AzureFileStorage) upload(fileName string){
ctx := context.Background() ctx := context.Background()
fmt.Printf("[%s] Starting upload to Azure File Share...\n", strings.Trim(filepath.Base(fileName), ".bak")) fmt.Printf("[%s] Starting upload to Azure File Share...\n", strings.Trim(filepath.Base(fileName), ".bak"))
//TODO Remove Hardcoded SQL Instance SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now())
SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Starting upload.", time.Now())
err = azfile.UploadFileToAzureFile(ctx, file, fileURL, err = azfile.UploadFileToAzureFile(ctx, file, fileURL,
azfile.UploadToAzureFileOptions{ azfile.UploadToAzureFileOptions{
@ -63,8 +62,7 @@ func (azure AzureFileStorage) upload(fileName string){
}}) }})
fmt.Printf("[%s] Upload finished.\n", strings.Trim(filepath.Base(fileName), ".bak")) fmt.Printf("[%s] Upload finished.\n", strings.Trim(filepath.Base(fileName), ".bak"))
//TODO Remove Hardcoded SQL Instance SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Finished upload.", time.Now())
SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Finished upload.", time.Now())
} }
func readConfig() []byte { func readConfig() []byte {

View file

@ -3,6 +3,7 @@ package main
import ( import (
"github.com/google/uuid" "github.com/google/uuid"
"os" "os"
"scabiosa/Compressor"
"scabiosa/Logging" "scabiosa/Logging"
"scabiosa/SQL" "scabiosa/SQL"
"scabiosa/StorageTypes" "scabiosa/StorageTypes"
@ -13,19 +14,18 @@ import (
func main(){ func main(){
config := Tools.GetConfig() config := Tools.GetConfig()
SQL.CreateDefaultTables(SQL.GetMariaDBInstance()) SQL.CreateDefaultTables(SQL.GetSQLInstance())
for _, backupItem := range config.FolderToBackup{ for _, backupItem := range config.FolderToBackup{
storage := StorageTypes.CheckStorageType(backupItem.StorageType) storage := StorageTypes.CheckStorageType(backupItem.StorageType)
destPath := checkTmpPath(config, backupItem.CreateLocalBackup) destPath := checkTmpPath(config, backupItem.CreateLocalBackup)
bakFile := CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath) bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath)
StorageTypes.UploadFile(storage, destPath + string(os.PathSeparator) + bakFile) StorageTypes.UploadFile(storage, destPath + string(os.PathSeparator) + bakFile)
if !backupItem.CreateLocalBackup { if !backupItem.CreateLocalBackup {
_ = os.Remove(destPath + string(os.PathSeparator) + bakFile) _ = os.Remove(destPath + string(os.PathSeparator) + bakFile)
//TODO Remove Hardcoded SQL Instance SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now())
SQL.NewLogEntry(SQL.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now())
} }
} }