Refactoring
SQL Interface
This commit is contained in:
commit
8a2174ba7e
6 changed files with 30 additions and 21 deletions
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package Compressor
|
||||
|
||||
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())
|
||||
}
|
||||
12
README.md
12
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 |
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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(config)}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
@ -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 {
|
||||
|
|
|
|||
8
main.go
8
main.go
|
|
@ -3,6 +3,7 @@ package main
|
|||
import (
|
||||
"github.com/google/uuid"
|
||||
"os"
|
||||
"scabiosa/Compressor"
|
||||
"scabiosa/Logging"
|
||||
"scabiosa/SQL"
|
||||
"scabiosa/StorageTypes"
|
||||
|
|
@ -13,19 +14,18 @@ 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 := 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 {
|
||||
_ = 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())
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue