feat(SQL): Implemented LocalTargetPath
This commit is contained in:
parent
ab27acb960
commit
23c2bc3226
3 changed files with 17 additions and 16 deletions
|
|
@ -43,8 +43,8 @@ func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string) bool {
|
func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string, hostname string) bool {
|
||||||
rows, _ := db.Query("SELECT * FROM `" + mariadb.Database + "`.Backups WHERE BackupName = '" + backupName + "';")
|
rows, _ := db.Query("SELECT * FROM `" + mariadb.Database + "`.Backups WHERE Hostname = ? AND BackupName = ?;", hostname, backupName)
|
||||||
if !rows.Next(){ return false; }
|
if !rows.Next(){ return false; }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ func (mariadb MariaDBConnector) createDefaultTables(){
|
||||||
logger := Logging.DetailedLogger("MariaDB", "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);"
|
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);"
|
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)
|
db := createMariaDBConnection(mariadb)
|
||||||
|
|
||||||
|
|
@ -96,19 +96,19 @@ func (mariadb MariaDBConnector) newLogEntry(uuid uuid.UUID, logType LogType, bac
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mariadb MariaDBConnector) newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath 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")
|
logger := Logging.DetailedLogger("MariaDB", "newBackupEntry")
|
||||||
db := createMariaDBConnection(mariadb)
|
db := createMariaDBConnection(mariadb)
|
||||||
|
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
|
|
||||||
if checkIfBackupEntryExist(db, mariadb, backupName){
|
if checkIfBackupEntryExist(db, mariadb, backupName, hostname){
|
||||||
_, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE BackupName = ?;", lastBackup, backupName)
|
_, err := db.Query("UPDATE `" + mariadb.Database + "`.Backups SET LastBackup = ? WHERE Hostname = ? AND BackupName = ?;", lastBackup, hostname, 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.New(), hostname, backupName, lastBackup, localBackup, filePath, strconv.FormatInt(int64(storageType), 10), remotePath)
|
_, 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 {
|
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(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string)
|
newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string)
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateDefaultTables(sqlService SQLService){
|
func CreateDefaultTables(sqlService SQLService){
|
||||||
|
|
@ -26,10 +26,10 @@ func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupN
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string){
|
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string){
|
||||||
config := Tools.GetConfig()
|
config := Tools.GetConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if config.SQLConfig.EnableSQL{
|
||||||
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath)
|
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
9
main.go
9
main.go
|
|
@ -24,22 +24,23 @@ func main(){
|
||||||
|
|
||||||
if backupItem.RemoteStorageType != "none"{
|
if backupItem.RemoteStorageType != "none"{
|
||||||
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
|
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
|
||||||
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.TargetPath)
|
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
|
||||||
} else {
|
} else {
|
||||||
destPath = backupItem.TargetPath
|
destPath = backupItem.LocalTargetPath
|
||||||
}
|
}
|
||||||
|
|
||||||
bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
||||||
|
|
||||||
if backupItem.RemoteStorageType != "none"{
|
if backupItem.RemoteStorageType != "none"{
|
||||||
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.TargetPath)
|
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none"{
|
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none"{
|
||||||
_ = 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.RemoteStorageType), StorageTypes.GetAzureStorage().TargetDirectory)
|
|
||||||
|
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.LocalTargetPath, backupItem.LocalTargetPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue