Started implementing SQL Logs.
Only MariaDB yet. Interface usage comes soon.
This commit is contained in:
parent
077459c6e0
commit
33b44a6ea5
3 changed files with 27 additions and 4 deletions
|
|
@ -6,10 +6,13 @@ import (
|
||||||
"compress/flate"
|
"compress/flate"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/google/uuid"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
|
"scabiosa/SQL"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateBakFile(filename string, folderPath string, destinationPath string) string {
|
func CreateBakFile(filename string, folderPath string, destinationPath string) string {
|
||||||
|
|
@ -29,6 +32,10 @@ func CreateBakFile(filename string, folderPath string, destinationPath string) s
|
||||||
logger.Fatal(err)
|
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())
|
||||||
|
|
||||||
|
|
||||||
return fileName
|
return fileName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -40,7 +47,8 @@ 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.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 {
|
||||||
|
|
@ -78,4 +86,6 @@ 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.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, filepath.Base(folderPath), SQL.SQLStage_Compress, SQL.REMOTE_NONE, "Compression complete.", time.Now())
|
||||||
}
|
}
|
||||||
|
|
@ -5,11 +5,14 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/Azure/azure-storage-file-go/azfile"
|
"github.com/Azure/azure-storage-file-go/azfile"
|
||||||
|
"github.com/google/uuid"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
|
"scabiosa/SQL"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AzureFileStorage struct{
|
type AzureFileStorage struct{
|
||||||
|
|
@ -46,6 +49,8 @@ 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.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{
|
||||||
|
|
@ -58,6 +63,8 @@ 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.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 {
|
||||||
|
|
|
||||||
12
main.go
12
main.go
|
|
@ -1,25 +1,31 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
"os"
|
"os"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
|
"scabiosa/SQL"
|
||||||
"scabiosa/StorageTypes"
|
"scabiosa/StorageTypes"
|
||||||
|
"scabiosa/Tools"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main(){
|
func main(){
|
||||||
config := GetConfig()
|
config := Tools.GetConfig()
|
||||||
|
|
||||||
|
SQL.CreateDefaultTables(SQL.GetMariaDBInstance())
|
||||||
|
|
||||||
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 := 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.GetMariaDBInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -33,7 +39,7 @@ func getTimeSuffix() string{
|
||||||
return "_" + currTime.Format("02-01-2006_15-04")
|
return "_" + currTime.Format("02-01-2006_15-04")
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkTmpPath(config Config, createLocalBackup bool) string{
|
func checkTmpPath(config Tools.Config, createLocalBackup bool) string{
|
||||||
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
||||||
if !createLocalBackup{
|
if !createLocalBackup{
|
||||||
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
|
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
|
||||||
|
|
|
||||||
Reference in a new issue