Updated SQL
This commit is contained in:
parent
fcbaaa092f
commit
675a8dafb3
5 changed files with 32 additions and 21 deletions
|
|
@ -62,7 +62,7 @@ func (mariadb MariaDBConnector) createDefaultTables(){
|
||||||
logger := Logging.DetailedLogger("MariaDB", "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);"
|
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 `DurationToBackup (s)` double null,\n HadErrors tinyint(1) null\n);\n\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"
|
||||||
|
|
||||||
|
|
||||||
db := createMariaDBConnection(mariadb)
|
db := createMariaDBConnection(mariadb)
|
||||||
|
|
@ -86,7 +86,6 @@ func (mariadb MariaDBConnector) createDefaultTables(){
|
||||||
|
|
||||||
func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time){
|
func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time){
|
||||||
logger := Logging.DetailedLogger("MariaDB", "newLogEntry")
|
logger := Logging.DetailedLogger("MariaDB", "newLogEntry")
|
||||||
|
|
||||||
db := createMariaDBConnection(mariadb)
|
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)
|
_, 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)
|
||||||
|
|
@ -97,18 +96,18 @@ func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, bac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (mariadb MariaDBConnector) newBackupEntry(uuid uuid.UUID, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, durationToBackup time.Duration, hadErrors bool){
|
func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string){
|
||||||
logger := Logging.DetailedLogger("MariaDB", "newBackupEntry")
|
logger := Logging.DetailedLogger("MariaDB", "newBackupEntry")
|
||||||
|
|
||||||
db := createMariaDBConnection(mariadb)
|
db := createMariaDBConnection(mariadb)
|
||||||
|
|
||||||
if checkIfBackupEntryExist(db, mariadb, backupName){
|
if checkIfBackupEntryExist(db, mariadb, backupName){
|
||||||
_, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ?, `DurationToBackup (s)` = ?, HadErrors = ? WHERE BackuoName = ?;",lastBackup, durationToBackup, hadErrors, backupName)
|
_, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE BackupName = ?;", lastBackup, backupName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
_, err := db.Query("INSERT INTO `" + mariadb.Database + "`.Backups VALUES (?, ?, ?, ?, ?, ?, ?, ?);", uuid.String(), lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath, durationToBackup, hadErrors)
|
_, err := db.Query("INSERT INTO `" + mariadb.Database + "`.Backups VALUES (?, ?, ?, ?, ?, ?, ?);", uuid.New(), backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ import (
|
||||||
type SQLService interface {
|
type SQLService interface {
|
||||||
createDefaultTables()
|
createDefaultTables()
|
||||||
newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time)
|
newLogEntry(uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time)
|
||||||
newBackupEntry(uuid uuid.UUID, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, durationToBackup time.Duration, hadErrors bool)
|
newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDefaultTables(sqlService SQLService){
|
func CreateDefaultTables(sqlService SQLService){
|
||||||
|
|
@ -20,8 +20,8 @@ func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupN
|
||||||
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
|
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
|
||||||
}
|
}
|
||||||
|
|
||||||
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, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string){
|
||||||
sqlService.newBackupEntry(uuid, backupName, lastBackup, localBackup, filePath, storageType, remotePath, durationToBackup, hadErrors)
|
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSQLInstance() SQLService{
|
func GetSQLInstance() SQLService{
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ type AzureFileStorage struct{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (azure AzureFileStorage) upload(fileName string){
|
func (azure AzureFileStorage) upload(fileName string, backupName string){
|
||||||
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
||||||
|
|
||||||
file, err := os.Open(fileName)
|
file, err := os.Open(fileName)
|
||||||
|
|
@ -48,8 +48,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", backupName, ".bak")
|
||||||
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.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, 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,11 +58,15 @@ func (azure AzureFileStorage) upload(fileName string){
|
||||||
CacheControl: "no-transform",
|
CacheControl: "no-transform",
|
||||||
},
|
},
|
||||||
Progress: func(bytesTransferred int64){
|
Progress: func(bytesTransferred int64){
|
||||||
fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(filepath.Base(fileName), ".bak") ,bytesTransferred, fileSize.Size())
|
fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(backupName, ".bak") ,bytesTransferred, fileSize.Size())
|
||||||
}})
|
}})
|
||||||
|
|
||||||
fmt.Printf("[%s] Upload finished.\n", strings.Trim(filepath.Base(fileName), ".bak"))
|
if err != nil{
|
||||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, filepath.Base(fileName), SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Finished upload.", time.Now())
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("[%s] Upload finished.\n", strings.Trim(backupName, ".bak"))
|
||||||
|
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, SQL.SQLStage_Upload, SQL.REMOTE_AZURE_FILE, "Finished upload.", time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
func readConfig() []byte {
|
func readConfig() []byte {
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,13 @@
|
||||||
package StorageTypes
|
package StorageTypes
|
||||||
|
|
||||||
|
import "scabiosa/SQL"
|
||||||
|
|
||||||
type Storage interface {
|
type Storage interface {
|
||||||
upload(fileName string)
|
upload(fileName string, backupName string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func UploadFile(storage Storage, fileName string){
|
func UploadFile(storage Storage, fileName string, backupName string){
|
||||||
storage.upload(fileName)
|
storage.upload(fileName, backupName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckStorageType(storageType string) Storage{
|
func CheckStorageType(storageType string) Storage{
|
||||||
|
|
@ -13,6 +15,12 @@ func CheckStorageType(storageType string) Storage{
|
||||||
if storageType == "azure-fileshare"{
|
if storageType == "azure-fileshare"{
|
||||||
return GetAzureStorage()
|
return GetAzureStorage()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckRemoteStorageType(storageType string) SQL.RemoteStorageType {
|
||||||
|
if storageType == "azure-fileshare"{
|
||||||
|
return SQL.REMOTE_AZURE_FILE
|
||||||
|
}
|
||||||
|
return 3
|
||||||
}
|
}
|
||||||
6
main.go
6
main.go
|
|
@ -21,15 +21,15 @@ func main(){
|
||||||
storage := StorageTypes.CheckStorageType(backupItem.StorageType)
|
storage := StorageTypes.CheckStorageType(backupItem.StorageType)
|
||||||
destPath := checkTmpPath(config, backupItem.CreateLocalBackup)
|
destPath := checkTmpPath(config, backupItem.CreateLocalBackup)
|
||||||
|
|
||||||
bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath)
|
bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
||||||
fmt.Printf(bakFile)
|
fmt.Printf(bakFile)
|
||||||
StorageTypes.UploadFile(storage, bakFile)
|
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName)
|
||||||
|
|
||||||
if !backupItem.CreateLocalBackup {
|
if !backupItem.CreateLocalBackup {
|
||||||
_ = os.Remove(bakFile)
|
_ = 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.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.StorageType), StorageTypes.GetAzureStorage().TargetDirectory)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue