refactor(SQL): Now has seperate config
This commit is contained in:
parent
c0f93788fb
commit
67af246a61
3 changed files with 132 additions and 66 deletions
|
|
@ -12,70 +12,76 @@ import (
|
|||
)
|
||||
|
||||
type MariaDBConnector struct {
|
||||
Address string
|
||||
Port uint16
|
||||
Database string
|
||||
DbUser string
|
||||
Address string
|
||||
Port uint16
|
||||
Database string
|
||||
DbUser string
|
||||
DbPassword string
|
||||
}
|
||||
|
||||
func GetMariaDBInstance(config Tools.Config) MariaDBConnector {
|
||||
func GetMariaDBInstance(sqlConfig Tools.SQLConfig) MariaDBConnector {
|
||||
var mariadb MariaDBConnector
|
||||
|
||||
mariadb.Address = config.SQLConfig.SqlAddress
|
||||
mariadb.Port = config.SQLConfig.SqlPort
|
||||
mariadb.Database = config.SQLConfig.Database
|
||||
mariadb.DbUser = config.SQLConfig.DbUser
|
||||
mariadb.DbPassword = config.SQLConfig.DbPassword
|
||||
mariadb.Address = sqlConfig.SqlAddress
|
||||
mariadb.Port = sqlConfig.SqlPort
|
||||
mariadb.Database = sqlConfig.Database
|
||||
mariadb.DbUser = sqlConfig.DbUser
|
||||
mariadb.DbPassword = sqlConfig.DbPassword
|
||||
|
||||
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 }
|
||||
if !rows.Next() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
||||
rows, _ := db.Query("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = 'Backups';", mariadb.Database)
|
||||
if !rows.Next(){ return false }
|
||||
if !rows.Next() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
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; }
|
||||
rows, _ := db.Query("SELECT * FROM `"+mariadb.Database+"`.Backups WHERE Hostname = ? AND BackupName = ?;", hostname, backupName)
|
||||
if !rows.Next() {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB{
|
||||
func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB {
|
||||
logger := Logging.DetailedLogger("MariaDB", "createConnection")
|
||||
db, err := sql.Open("mysql", mariadb.DbUser + ":" + mariadb.DbPassword + "@(" + mariadb.Address + ":" +strconv.Itoa(int(mariadb.Port))+ ")/" + mariadb.Database)
|
||||
if err != nil{
|
||||
db, err := sql.Open("mysql", mariadb.DbUser+":"+mariadb.DbPassword+"@("+mariadb.Address+":"+strconv.Itoa(int(mariadb.Port))+")/"+mariadb.Database)
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func (mariadb MariaDBConnector) createDefaultTables(){
|
||||
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)
|
||||
|
||||
if !checkIfBackupTableExist(db, mariadb){
|
||||
if !checkIfBackupTableExist(db, mariadb) {
|
||||
_, err := db.Exec(backupSQL)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
if !checkIfEventLogTableExist(db, mariadb){
|
||||
if !checkIfEventLogTableExist(db, mariadb) {
|
||||
_, err := db.Exec(eventLogSQL)
|
||||
if err != nil{
|
||||
if err != nil {
|
||||
logger.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -83,34 +89,34 @@ func (mariadb MariaDBConnector) createDefaultTables(){
|
|||
_ = db.Close()
|
||||
}
|
||||
|
||||
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")
|
||||
db := createMariaDBConnection(mariadb)
|
||||
|
||||
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{
|
||||
_, 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath 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, hostname){
|
||||
_, 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 checkIfBackupEntryExist(db, mariadb, backupName, hostname) {
|
||||
_, 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)
|
||||
}
|
||||
} else {
|
||||
_, err := db.Query("INSERT INTO `" + mariadb.Database + "`.Backups VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", uuid.New(), hostname, backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath, localPath)
|
||||
_, 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,35 +12,40 @@ type SQLService interface {
|
|||
newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string)
|
||||
}
|
||||
|
||||
func CreateDefaultTables(sqlService SQLService){
|
||||
config := Tools.GetConfig()
|
||||
if config.SQLConfig.EnableSQL{
|
||||
func CreateDefaultTables(sqlService SQLService) {
|
||||
sqlConfig := Tools.GetSQLConfig()
|
||||
if sqlConfig.EnableSQL {
|
||||
sqlService.createDefaultTables()
|
||||
}
|
||||
}
|
||||
|
||||
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time){
|
||||
config := Tools.GetConfig()
|
||||
if config.SQLConfig.EnableSQL{
|
||||
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
|
||||
sqlConfig := Tools.GetSQLConfig()
|
||||
if sqlConfig.EnableSQL {
|
||||
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
|
||||
}
|
||||
}
|
||||
|
||||
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{
|
||||
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
|
||||
sqlConfig := Tools.GetSQLConfig()
|
||||
if sqlConfig.EnableSQL {
|
||||
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
|
||||
}
|
||||
}
|
||||
|
||||
func GetSQLInstance() SQLService{
|
||||
config := Tools.GetConfig()
|
||||
func GetSQLInstance() SQLService {
|
||||
sqlConfig := Tools.GetSQLConfig()
|
||||
|
||||
if !config.SQLConfig.EnableSQL { return nil }
|
||||
if !sqlConfig.EnableSQL {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch config.SQLConfig.SqlType {
|
||||
case "mariadb": {return GetMariaDBInstance(config)}
|
||||
switch sqlConfig.SqlType {
|
||||
case "mariadb":
|
||||
{
|
||||
return GetMariaDBInstance(sqlConfig)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue