commit
82c99aa75f
12 changed files with 413 additions and 193 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -17,3 +17,6 @@ scabiosa
|
||||||
|
|
||||||
#tmp folder
|
#tmp folder
|
||||||
tmp/
|
tmp/
|
||||||
|
|
||||||
|
#config folder
|
||||||
|
config/
|
||||||
111
Commands/GenerateDefaultConfigs.go
Normal file
111
Commands/GenerateDefaultConfigs.go
Normal file
|
|
@ -0,0 +1,111 @@
|
||||||
|
package Commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"os"
|
||||||
|
"scabiosa/Logging"
|
||||||
|
"scabiosa/Tools"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GenerateNewConfigs() *cli.Command {
|
||||||
|
logger := Logging.Logger("generate-configs")
|
||||||
|
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "generate-config",
|
||||||
|
Usage: "Generates the default configs",
|
||||||
|
Description: "Creates the specified configs",
|
||||||
|
HelpName: "generate-config",
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
err := os.RemoveAll("config/")
|
||||||
|
os.Mkdir("config", 0755)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var sqlConfig Tools.SQLConfig
|
||||||
|
var input string
|
||||||
|
var inputInt uint8
|
||||||
|
fmt.Printf("Want to use SQL? [Y]/[N]: ")
|
||||||
|
fmt.Scanf("%s", &input)
|
||||||
|
if input[0] == 'Y' || input[0] == 'y' {
|
||||||
|
sqlConfig.EnableSQL = true
|
||||||
|
fmt.Printf("What SQL Type do you want to use?\n")
|
||||||
|
fmt.Printf("[0] MariaDB\n")
|
||||||
|
fmt.Printf("\nSelection: ")
|
||||||
|
fmt.Scanf("%d", &inputInt)
|
||||||
|
switch inputInt {
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
sqlConfig.SqlType = "mariadb"
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
fmt.Printf("Invalid input!")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\n\nSQL Address: ")
|
||||||
|
fmt.Scanf("%s", &sqlConfig.SqlAddress)
|
||||||
|
fmt.Printf("\nSQL Port: ")
|
||||||
|
fmt.Scanf("%d", &sqlConfig.SqlPort)
|
||||||
|
fmt.Printf("\nSQL Database: ")
|
||||||
|
fmt.Scanf("%s", &sqlConfig.Database)
|
||||||
|
fmt.Printf("\nSQL User: ")
|
||||||
|
fmt.Scanf("%s", &sqlConfig.DbUser)
|
||||||
|
fmt.Printf("\nSQL Password: ")
|
||||||
|
fmt.Scanf("%s", &sqlConfig.DbPassword)
|
||||||
|
|
||||||
|
Tools.GenerateSQLConfig(sqlConfig)
|
||||||
|
fmt.Printf("SQL config created!")
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sqlConfig.EnableSQL = false
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("\n\nWhich storage do you want to use?\n")
|
||||||
|
fmt.Printf("[0]\tNone\n")
|
||||||
|
fmt.Printf("[1]\tAzure File Share\n")
|
||||||
|
fmt.Printf("\nSelection: ")
|
||||||
|
fmt.Scanf("%d", &inputInt)
|
||||||
|
switch inputInt {
|
||||||
|
case 0:
|
||||||
|
{
|
||||||
|
fmt.Printf("Reminder: remoteStorageType = none\n")
|
||||||
|
//Do (nearly) nothing! :D
|
||||||
|
}
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
var azure Tools.AzureConfig
|
||||||
|
fmt.Printf("\n\nStorageAccount Name: ")
|
||||||
|
fmt.Scanf("%s", &azure.StorageAccountName)
|
||||||
|
fmt.Printf("\nStorageAccount Key: ")
|
||||||
|
fmt.Scanf("%s", &azure.StorageAccountKey)
|
||||||
|
fmt.Printf("\nFileshare Name: ")
|
||||||
|
fmt.Scanf("%s", &azure.FileshareName)
|
||||||
|
Tools.GenerateAzureConfig(azure)
|
||||||
|
fmt.Printf("\nAzure config created!\n")
|
||||||
|
fmt.Printf("Reminder: remoteStorageType = azure-file\n")
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
fmt.Printf("Invalid input!")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Tools.GenerateBaseConfig()
|
||||||
|
fmt.Printf("All configs generated!\n")
|
||||||
|
fmt.Printf("Please modify the config.json with your backup entries.\n")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
91
Commands/StartBackupProc.go
Normal file
91
Commands/StartBackupProc.go
Normal file
|
|
@ -0,0 +1,91 @@
|
||||||
|
package Commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"github.com/urfave/cli/v2"
|
||||||
|
"os"
|
||||||
|
"scabiosa/Compressor"
|
||||||
|
"scabiosa/Logging"
|
||||||
|
"scabiosa/SQL"
|
||||||
|
"scabiosa/StorageTypes"
|
||||||
|
"scabiosa/Tools"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StartBackupProc() *cli.Command {
|
||||||
|
logger := Logging.Logger("backup")
|
||||||
|
|
||||||
|
return &cli.Command{
|
||||||
|
Name: "backup",
|
||||||
|
Usage: "Starts backup process",
|
||||||
|
Description: "Compresses and uploads/stores the backups",
|
||||||
|
HelpName: "backup",
|
||||||
|
Action: func(c *cli.Context) error {
|
||||||
|
Tools.CheckIfConfigExists()
|
||||||
|
config := Tools.GetConfig()
|
||||||
|
|
||||||
|
SQL.CreateDefaultTables(SQL.GetSQLInstance())
|
||||||
|
|
||||||
|
for _, backupItem := range config.FolderToBackup {
|
||||||
|
|
||||||
|
var storage StorageTypes.Storage
|
||||||
|
var destPath string
|
||||||
|
|
||||||
|
if backupItem.RemoteStorageType != "none" {
|
||||||
|
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
|
||||||
|
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
|
||||||
|
} else {
|
||||||
|
destPath = backupItem.LocalTargetPath
|
||||||
|
}
|
||||||
|
|
||||||
|
bakFile := Compressor.CreateBakFile(backupItem.BackupName+getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
||||||
|
|
||||||
|
if backupItem.RemoteStorageType != "none" {
|
||||||
|
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none" {
|
||||||
|
backupItem.LocalTargetPath = "NONE"
|
||||||
|
|
||||||
|
_ = os.Remove(bakFile)
|
||||||
|
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file", time.Now())
|
||||||
|
}
|
||||||
|
|
||||||
|
if backupItem.RemoteStorageType == "none" {
|
||||||
|
backupItem.CreateLocalBackup = true
|
||||||
|
backupItem.RemoteTargetPath = "NONE"
|
||||||
|
}
|
||||||
|
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error {
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getTimeSuffix() string {
|
||||||
|
currTime := time.Now()
|
||||||
|
|
||||||
|
return "_" + currTime.Format("02-01-2006_15-04")
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkTmpPath(createLocalBackup bool, targetPath string) string {
|
||||||
|
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
||||||
|
if !createLocalBackup {
|
||||||
|
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
|
||||||
|
dirErr := os.Mkdir("tmp", 0775)
|
||||||
|
if dirErr != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "tmp"
|
||||||
|
}
|
||||||
|
|
||||||
|
return targetPath
|
||||||
|
}
|
||||||
|
|
@ -19,63 +19,69 @@ type MariaDBConnector struct {
|
||||||
DbPassword string
|
DbPassword string
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetMariaDBInstance(config Tools.Config) MariaDBConnector {
|
func GetMariaDBInstance(sqlConfig Tools.SQLConfig) MariaDBConnector {
|
||||||
var mariadb MariaDBConnector
|
var mariadb MariaDBConnector
|
||||||
|
|
||||||
mariadb.Address = config.SQLConfig.SqlAddress
|
mariadb.Address = sqlConfig.SqlAddress
|
||||||
mariadb.Port = config.SQLConfig.SqlPort
|
mariadb.Port = sqlConfig.SqlPort
|
||||||
mariadb.Database = config.SQLConfig.Database
|
mariadb.Database = sqlConfig.Database
|
||||||
mariadb.DbUser = config.SQLConfig.DbUser
|
mariadb.DbUser = sqlConfig.DbUser
|
||||||
mariadb.DbPassword = config.SQLConfig.DbPassword
|
mariadb.DbPassword = sqlConfig.DbPassword
|
||||||
|
|
||||||
return mariadb
|
return mariadb
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfEventLogTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
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)
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfBackupTableExist(db *sql.DB, mariadb MariaDBConnector) bool {
|
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)
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkIfBackupEntryExist(db *sql.DB, mariadb MariaDBConnector, backupName string, hostname string) bool {
|
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)
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB{
|
func createMariaDBConnection(mariadb MariaDBConnector) *sql.DB {
|
||||||
logger := Logging.DetailedLogger("MariaDB", "createConnection")
|
logger := Logging.DetailedLogger("MariaDB", "createConnection")
|
||||||
db, err := sql.Open("mysql", mariadb.DbUser + ":" + mariadb.DbPassword + "@(" + mariadb.Address + ":" +strconv.Itoa(int(mariadb.Port))+ ")/" + mariadb.Database)
|
db, err := sql.Open("mysql", mariadb.DbUser+":"+mariadb.DbPassword+"@("+mariadb.Address+":"+strconv.Itoa(int(mariadb.Port))+")/"+mariadb.Database)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mariadb MariaDBConnector) createDefaultTables(){
|
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, LocalPath 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)
|
||||||
|
|
||||||
if !checkIfBackupTableExist(db, mariadb){
|
if !checkIfBackupTableExist(db, mariadb) {
|
||||||
_, err := db.Exec(backupSQL)
|
_, err := db.Exec(backupSQL)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !checkIfEventLogTableExist(db, mariadb){
|
if !checkIfEventLogTableExist(db, mariadb) {
|
||||||
_, err := db.Exec(eventLogSQL)
|
_, err := db.Exec(eventLogSQL)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -83,32 +89,32 @@ func (mariadb MariaDBConnector) createDefaultTables(){
|
||||||
_ = db.Close()
|
_ = 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")
|
logger := Logging.DetailedLogger("MariaDB", "newLogEntry")
|
||||||
db := createMariaDBConnection(mariadb)
|
db := createMariaDBConnection(mariadb)
|
||||||
|
|
||||||
hostname, _ := os.Hostname()
|
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)
|
_, 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{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
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")
|
logger := Logging.DetailedLogger("MariaDB", "newBackupEntry")
|
||||||
db := createMariaDBConnection(mariadb)
|
db := createMariaDBConnection(mariadb)
|
||||||
|
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
|
|
||||||
if checkIfBackupEntryExist(db, mariadb, backupName, 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)
|
_, 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 {
|
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, 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 {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,34 +12,39 @@ type SQLService interface {
|
||||||
newBackupEntry(backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath 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) {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if sqlConfig.EnableSQL {
|
||||||
sqlService.createDefaultTables()
|
sqlService.createDefaultTables()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time){
|
func NewLogEntry(sqlService SQLService, uuid uuid.UUID, logType LogType, backupName string, stage SQLStage, storageType RemoteStorageType, description string, timestamp time.Time) {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if sqlConfig.EnableSQL {
|
||||||
sqlService.newLogEntry(uuid, logType, backupName, stage, storageType, description, timestamp)
|
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){
|
func NewBackupEntry(sqlService SQLService, backupName string, lastBackup time.Time, localBackup bool, filePath string, storageType RemoteStorageType, remotePath string, localPath string) {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
if config.SQLConfig.EnableSQL{
|
if sqlConfig.EnableSQL {
|
||||||
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
|
sqlService.newBackupEntry(backupName, lastBackup, localBackup, filePath, storageType, remotePath, localPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSQLInstance() SQLService{
|
func GetSQLInstance() SQLService {
|
||||||
config := Tools.GetConfig()
|
sqlConfig := Tools.GetSQLConfig()
|
||||||
|
|
||||||
if !config.SQLConfig.EnableSQL { return nil }
|
if !sqlConfig.EnableSQL {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
switch config.SQLConfig.SqlType {
|
switch sqlConfig.SqlType {
|
||||||
case "mariadb": {return GetMariaDBInstance(config)}
|
case "mariadb":
|
||||||
|
{
|
||||||
|
return GetMariaDBInstance(sqlConfig)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
|
|
@ -12,19 +12,18 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
"scabiosa/SQL"
|
"scabiosa/SQL"
|
||||||
|
"scabiosa/Tools"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AzureFileStorage struct{
|
type AzureFileStorage struct {
|
||||||
FileshareName string `json:"fileshareName"`
|
FileshareName string
|
||||||
TargetDirectory string `json:"targetDirectory"`
|
StorageAccountName string
|
||||||
StorageAccountName string `json:"storageAccountName"`
|
StorageAccountKey string
|
||||||
StorageAccountKey string `json:"storageAccountKey"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string) {
|
||||||
func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string){
|
|
||||||
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
logger := Logging.DetailedLogger("AzureFileStorage", "upload")
|
||||||
|
|
||||||
file, err := os.Open(fileName)
|
file, err := os.Open(fileName)
|
||||||
|
|
@ -39,15 +38,11 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
|
||||||
}
|
}
|
||||||
|
|
||||||
credential, err := azfile.NewSharedKeyCredential(azure.StorageAccountName, azure.StorageAccountKey)
|
credential, err := azfile.NewSharedKeyCredential(azure.StorageAccountName, azure.StorageAccountKey)
|
||||||
if err != nil{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if destinationPath != ""{
|
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s/%s/%s", azure.StorageAccountName, azure.FileshareName, destinationPath, filepath.Base(fileName)))
|
||||||
azure.TargetDirectory = destinationPath
|
|
||||||
}
|
|
||||||
|
|
||||||
u, _ := url.Parse(fmt.Sprintf("https://%s.file.core.windows.net/%s/%s/%s", azure.StorageAccountName, azure.FileshareName ,azure.TargetDirectory, filepath.Base(fileName)))
|
|
||||||
|
|
||||||
fileURL := azfile.NewFileURL(*u, azfile.NewPipeline(credential, azfile.PipelineOptions{}))
|
fileURL := azfile.NewFileURL(*u, azfile.NewPipeline(credential, azfile.PipelineOptions{}))
|
||||||
|
|
||||||
|
|
@ -56,7 +51,6 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
|
||||||
fmt.Printf("[%s] Starting upload to Azure File Share...\n", backupName, ".bak")
|
fmt.Printf("[%s] Starting upload to Azure File Share...\n", backupName, ".bak")
|
||||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupName, 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())
|
||||||
|
|
||||||
|
|
||||||
progressBar := pb.StartNew(int(fileSize.Size()))
|
progressBar := pb.StartNew(int(fileSize.Size()))
|
||||||
progressBar.Set(pb.Bytes, true)
|
progressBar.Set(pb.Bytes, true)
|
||||||
err = azfile.UploadFileToAzureFile(ctx, file, fileURL,
|
err = azfile.UploadFileToAzureFile(ctx, file, fileURL,
|
||||||
|
|
@ -65,12 +59,12 @@ func (azure AzureFileStorage) upload(fileName string, backupName string, destina
|
||||||
FileHTTPHeaders: azfile.FileHTTPHeaders{
|
FileHTTPHeaders: azfile.FileHTTPHeaders{
|
||||||
CacheControl: "no-transform",
|
CacheControl: "no-transform",
|
||||||
},
|
},
|
||||||
Progress: func(bytesTransferred int64){
|
Progress: func(bytesTransferred int64) {
|
||||||
progressBar.SetCurrent(bytesTransferred)
|
progressBar.SetCurrent(bytesTransferred)
|
||||||
//fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(backupName, ".bak") ,bytesTransferred, fileSize.Size())
|
//fmt.Printf("[%s] Uploaded %d of %d bytes.\n", strings.Trim(backupName, ".bak") ,bytesTransferred, fileSize.Size())
|
||||||
}})
|
}})
|
||||||
|
|
||||||
if err != nil{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
progressBar.Finish()
|
progressBar.Finish()
|
||||||
|
|
@ -82,23 +76,27 @@ func readConfig() []byte {
|
||||||
logger := Logging.DetailedLogger("AzureFileStorage", "readConfig")
|
logger := Logging.DetailedLogger("AzureFileStorage", "readConfig")
|
||||||
|
|
||||||
file, err := os.ReadFile("config/azure.json")
|
file, err := os.ReadFile("config/azure.json")
|
||||||
if err != nil{
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func GetAzureStorage() AzureFileStorage {
|
func GetAzureStorage() AzureFileStorage {
|
||||||
logger := Logging.DetailedLogger("AzureFileStorage", "GetAzureStorage")
|
logger := Logging.DetailedLogger("AzureFileStorage", "GetAzureStorage")
|
||||||
|
|
||||||
var azureStorage AzureFileStorage
|
var azureConfig Tools.AzureConfig
|
||||||
|
var azureFileShare AzureFileStorage
|
||||||
|
|
||||||
jsonErr := json.Unmarshal(readConfig(), &azureStorage)
|
jsonErr := json.Unmarshal(readConfig(), &azureConfig)
|
||||||
if jsonErr != nil{
|
if jsonErr != nil {
|
||||||
logger.Fatal(jsonErr)
|
logger.Fatal(jsonErr)
|
||||||
}
|
}
|
||||||
|
|
||||||
return azureStorage
|
azureFileShare.StorageAccountName = azureConfig.StorageAccountName
|
||||||
|
azureFileShare.StorageAccountKey = azureConfig.StorageAccountKey
|
||||||
|
azureFileShare.FileshareName = azureConfig.FileshareName
|
||||||
|
|
||||||
|
return azureFileShare
|
||||||
}
|
}
|
||||||
|
|
@ -2,13 +2,12 @@ package Tools
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type SQLConfig struct {
|
||||||
LocalBackupPath string `json:"localBackupPath"`
|
|
||||||
SQLConfig struct{
|
|
||||||
EnableSQL bool `json:"enableSQL"`
|
EnableSQL bool `json:"enableSQL"`
|
||||||
SqlType string `json:"sqlType"`
|
SqlType string `json:"sqlType"`
|
||||||
SqlAddress string `json:"sql-address"`
|
SqlAddress string `json:"sql-address"`
|
||||||
|
|
@ -16,8 +15,16 @@ type Config struct {
|
||||||
Database string `json:"database"`
|
Database string `json:"database"`
|
||||||
DbUser string `json:"db-user"`
|
DbUser string `json:"db-user"`
|
||||||
DbPassword string `json:"db-password"`
|
DbPassword string `json:"db-password"`
|
||||||
} `json:"sqlConfig"`
|
}
|
||||||
FolderToBackup []struct{
|
|
||||||
|
type AzureConfig struct {
|
||||||
|
FileshareName string `json:"fileshareName"`
|
||||||
|
StorageAccountName string `json:"storageAccountName"`
|
||||||
|
StorageAccountKey string `json:"storageAccountKey"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
FolderToBackup []struct {
|
||||||
BackupName string `json:"backupName"`
|
BackupName string `json:"backupName"`
|
||||||
FolderPath string `json:"folderPath"`
|
FolderPath string `json:"folderPath"`
|
||||||
RemoteStorageType string `json:"remoteStorageType"`
|
RemoteStorageType string `json:"remoteStorageType"`
|
||||||
|
|
@ -37,35 +44,86 @@ func readConfig() []byte {
|
||||||
return file
|
return file
|
||||||
}
|
}
|
||||||
|
|
||||||
func CheckIfConfigExists(){
|
func readSQLConfig() []byte {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "readSQLConfig")
|
||||||
|
|
||||||
|
file, err := os.ReadFile("config/sql-config.json")
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
return file
|
||||||
|
}
|
||||||
|
|
||||||
|
func CheckIfConfigExists() {
|
||||||
logger := Logging.DetailedLogger("ConfigHandler", "CheckIfConfigExists")
|
logger := Logging.DetailedLogger("ConfigHandler", "CheckIfConfigExists")
|
||||||
|
|
||||||
if _, err := os.Stat("config/config.json"); os.IsNotExist(err){
|
if _, err := os.Stat("config/config.json"); os.IsNotExist(err) {
|
||||||
_, fileErr := os.OpenFile("config/config.json", os.O_CREATE, 0775)
|
_, fileErr := os.OpenFile("config/config.json", os.O_CREATE, 0775)
|
||||||
if fileErr != nil{
|
if fileErr != nil {
|
||||||
logger.Fatal(fileErr)
|
logger.Fatal(fileErr)
|
||||||
}
|
}
|
||||||
generateDefaultConfig()
|
fmt.Printf("No configs detected. Please use 'scabiosa generate-config'\n")
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateDefaultConfig() {
|
func GenerateBaseConfig() {
|
||||||
logger := Logging.DetailedLogger("ConfigHandler", "GenerateDefaultConfig")
|
logger := Logging.DetailedLogger("ConfigHandler", "GenerateBaseConfig")
|
||||||
|
var baseConfig Config
|
||||||
|
|
||||||
var config Config
|
conf, err := json.MarshalIndent(baseConfig, "", "\t")
|
||||||
var conf []byte
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
conf, err := json.MarshalIndent(config, "", "\t")
|
}
|
||||||
//conf, err := json.Marshal(config)
|
for _, s := range baseConfig.FolderToBackup {
|
||||||
|
s.BackupName = ""
|
||||||
|
}
|
||||||
|
err = os.WriteFile("config/config.json", conf, 0775)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = os.WriteFile("config/config.json", conf, 0755)
|
}
|
||||||
|
|
||||||
|
func GenerateAzureConfig(azure AzureConfig) {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "GenerateAzureConfig")
|
||||||
|
|
||||||
|
conf, err := json.MarshalIndent(azure, "", "\t")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile("config/azure.json", conf, 0775)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateSQLConfig(sqlConfig SQLConfig) {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "GenerateSQLConfig")
|
||||||
|
|
||||||
|
conf, err := json.MarshalIndent(sqlConfig, "", "\t")
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile("config/sql-config.json", conf, 0775)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetSQLConfig() SQLConfig {
|
||||||
|
logger := Logging.DetailedLogger("ConfigHandler", "GetSQLConfig")
|
||||||
|
var sqlConfig SQLConfig
|
||||||
|
|
||||||
|
err := json.Unmarshal(readSQLConfig(), &sqlConfig)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetConfig() Config {
|
func GetConfig() Config {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
{
|
|
||||||
"storageAccountName": "",
|
|
||||||
"storageAccountKey": "",
|
|
||||||
"fileshareName": "",
|
|
||||||
"targetDirectory": ""
|
|
||||||
}
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
{
|
|
||||||
"localBackupPath": "",
|
|
||||||
"sqlConfig": {
|
|
||||||
"enableSQL": false,
|
|
||||||
"sql-address": "",
|
|
||||||
"sql-port": 0,
|
|
||||||
"database": "",
|
|
||||||
"db-user": "",
|
|
||||||
"db-password": ""
|
|
||||||
},
|
|
||||||
"foldersToBackup": [
|
|
||||||
{
|
|
||||||
"backupName": "",
|
|
||||||
"folderPath": "",
|
|
||||||
"remoteStorageType": "",
|
|
||||||
"remoteTargetPath": "",
|
|
||||||
"createLocalBackup": false,
|
|
||||||
"localTargetPath": ""
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
4
go.mod
4
go.mod
|
|
@ -9,11 +9,13 @@ require (
|
||||||
github.com/cheggaaa/pb/v3 v3.0.8
|
github.com/cheggaaa/pb/v3 v3.0.8
|
||||||
github.com/go-sql-driver/mysql v1.6.0
|
github.com/go-sql-driver/mysql v1.6.0
|
||||||
github.com/google/uuid v1.3.0
|
github.com/google/uuid v1.3.0
|
||||||
|
github.com/urfave/cli/v2 v2.3.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Azure/azure-pipeline-go v0.2.1 // indirect
|
github.com/Azure/azure-pipeline-go v0.2.1 // indirect
|
||||||
github.com/VividCortex/ewma v1.1.1 // indirect
|
github.com/VividCortex/ewma v1.1.1 // indirect
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d // indirect
|
||||||
github.com/fatih/color v1.10.0 // indirect
|
github.com/fatih/color v1.10.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.8 // indirect
|
github.com/mattn/go-colorable v0.1.8 // indirect
|
||||||
github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149 // indirect
|
github.com/mattn/go-ieproxy v0.0.0-20190610004146-91bb50d98149 // indirect
|
||||||
|
|
@ -21,6 +23,8 @@ require (
|
||||||
github.com/mattn/go-runewidth v0.0.12 // indirect
|
github.com/mattn/go-runewidth v0.0.12 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/rivo/uniseg v0.2.0 // indirect
|
github.com/rivo/uniseg v0.2.0 // indirect
|
||||||
|
github.com/russross/blackfriday/v2 v2.0.1 // indirect
|
||||||
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
||||||
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
|
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
|
||||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
|
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 // indirect
|
||||||
golang.org/x/text v0.3.0 // indirect
|
golang.org/x/text v0.3.0 // indirect
|
||||||
|
|
|
||||||
11
go.sum
11
go.sum
|
|
@ -2,10 +2,13 @@ github.com/Azure/azure-pipeline-go v0.2.1 h1:OLBdZJ3yvOn2MezlWvbrBMTEUQC72zAftRZ
|
||||||
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
|
github.com/Azure/azure-pipeline-go v0.2.1/go.mod h1:UGSo8XybXnIGZ3epmeBw7Jdz+HiUVpqIlpz/HKHylF4=
|
||||||
github.com/Azure/azure-storage-file-go v0.8.0 h1:OX8DGsleWLUE6Mw4R/OeWEZMvsTIpwN94J59zqKQnTI=
|
github.com/Azure/azure-storage-file-go v0.8.0 h1:OX8DGsleWLUE6Mw4R/OeWEZMvsTIpwN94J59zqKQnTI=
|
||||||
github.com/Azure/azure-storage-file-go v0.8.0/go.mod h1:3w3mufGcMjcOJ3w+4Gs+5wsSgkT7xDwWWqMMIrXtW4c=
|
github.com/Azure/azure-storage-file-go v0.8.0/go.mod h1:3w3mufGcMjcOJ3w+4Gs+5wsSgkT7xDwWWqMMIrXtW4c=
|
||||||
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
|
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
|
||||||
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
|
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
|
||||||
github.com/cheggaaa/pb/v3 v3.0.8 h1:bC8oemdChbke2FHIIGy9mn4DPJ2caZYQnfbRqwmdCoA=
|
github.com/cheggaaa/pb/v3 v3.0.8 h1:bC8oemdChbke2FHIIGy9mn4DPJ2caZYQnfbRqwmdCoA=
|
||||||
github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA=
|
github.com/cheggaaa/pb/v3 v3.0.8/go.mod h1:UICbiLec/XO6Hw6k+BHEtHeQFzzBH4i2/qk/ow1EJTA=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
|
||||||
|
|
@ -35,10 +38,16 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
||||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
|
||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
|
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
|
||||||
|
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
|
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
|
||||||
|
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
|
||||||
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
|
||||||
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
|
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
||||||
|
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
|
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8=
|
||||||
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||||
|
|
@ -51,5 +60,7 @@ golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57 h1:F5Gozwx4I1xtr/sr/8CFbb57i
|
||||||
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20210403161142-5e06dd20ab57/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
|
||||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
|
|
|
||||||
80
main.go
80
main.go
|
|
@ -1,74 +1,34 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/google/uuid"
|
"github.com/urfave/cli/v2"
|
||||||
"os"
|
"os"
|
||||||
"scabiosa/Compressor"
|
"scabiosa/Commands"
|
||||||
"scabiosa/Logging"
|
"scabiosa/Logging"
|
||||||
"scabiosa/SQL"
|
|
||||||
"scabiosa/StorageTypes"
|
|
||||||
"scabiosa/Tools"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main(){
|
func main() {
|
||||||
Tools.CheckIfConfigExists()
|
logger := Logging.Logger("mainThread")
|
||||||
config := Tools.GetConfig()
|
|
||||||
|
|
||||||
SQL.CreateDefaultTables(SQL.GetSQLInstance())
|
app := &cli.App{
|
||||||
|
Name: "scabiosa",
|
||||||
for _, backupItem := range config.FolderToBackup{
|
Usage: "Backup Util",
|
||||||
|
Authors: []*cli.Author{
|
||||||
var storage StorageTypes.Storage
|
{
|
||||||
var destPath string
|
Name: "netbenix",
|
||||||
|
Email: "netbenix@codenoodles.de",
|
||||||
if backupItem.RemoteStorageType != "none"{
|
},
|
||||||
storage = StorageTypes.CheckStorageType(backupItem.RemoteStorageType)
|
},
|
||||||
destPath = checkTmpPath(backupItem.CreateLocalBackup, backupItem.LocalTargetPath)
|
Copyright: "(c) 2021-2022 netbenix",
|
||||||
} else {
|
Commands: []*cli.Command{
|
||||||
destPath = backupItem.LocalTargetPath
|
Commands.StartBackupProc(),
|
||||||
|
Commands.GenerateNewConfigs(),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName)
|
err := app.Run(os.Args)
|
||||||
|
if err != nil {
|
||||||
if backupItem.RemoteStorageType != "none"{
|
|
||||||
StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.RemoteTargetPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !backupItem.CreateLocalBackup && backupItem.RemoteStorageType != "none"{
|
|
||||||
backupItem.LocalTargetPath = "NONE"
|
|
||||||
|
|
||||||
_ = os.Remove(bakFile)
|
|
||||||
SQL.NewLogEntry(SQL.GetSQLInstance(), uuid.New(), SQL.LogInfo, backupItem.BackupName, SQL.SQLStage_DeleteTmp, SQL.REMOTE_NONE, "Deleted tmp file" ,time.Now())
|
|
||||||
}
|
|
||||||
|
|
||||||
if backupItem.RemoteStorageType == "none" {
|
|
||||||
backupItem.CreateLocalBackup = true
|
|
||||||
backupItem.RemoteTargetPath = "NONE"
|
|
||||||
}
|
|
||||||
SQL.NewBackupEntry(SQL.GetSQLInstance(), backupItem.BackupName, time.Now(), backupItem.CreateLocalBackup, backupItem.FolderPath, StorageTypes.CheckRemoteStorageType(backupItem.RemoteStorageType), backupItem.RemoteTargetPath, backupItem.LocalTargetPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func getTimeSuffix() string{
|
|
||||||
currTime := time.Now()
|
|
||||||
|
|
||||||
return "_" + currTime.Format("02-01-2006_15-04")
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkTmpPath(createLocalBackup bool, targetPath string) string{
|
|
||||||
logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
|
|
||||||
if !createLocalBackup {
|
|
||||||
if _, err := os.Stat("tmp"); os.IsNotExist(err) {
|
|
||||||
dirErr := os.Mkdir("tmp", 0775)
|
|
||||||
if dirErr != nil {
|
|
||||||
logger.Fatal(err)
|
logger.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return "tmp"
|
|
||||||
}
|
|
||||||
|
|
||||||
return targetPath
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Reference in a new issue