diff --git a/Commands/GenerateDefaultConfigs.go b/Commands/GenerateDefaultConfigs.go index 3e35915..eedacad 100644 --- a/Commands/GenerateDefaultConfigs.go +++ b/Commands/GenerateDefaultConfigs.go @@ -8,7 +8,7 @@ import ( "scabiosa/Tools" ) -func GenerateNewConfigs() *cli.Command { +func GenerateNewConfigsCommand() *cli.Command { logger := Logging.Logger("generate-configs") return &cli.Command{ diff --git a/Commands/StartBackupProc.go b/Commands/StartBackupProc.go index 97ee12a..58e82ff 100644 --- a/Commands/StartBackupProc.go +++ b/Commands/StartBackupProc.go @@ -12,7 +12,7 @@ import ( "time" ) -func StartBackupProc() *cli.Command { +func StartBackupProcCommand() *cli.Command { logger := Logging.Logger("backup") return &cli.Command{ @@ -21,43 +21,7 @@ func StartBackupProc() *cli.Command { 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) - } - + StartBackupProc() return nil }, OnUsageError: func(cc *cli.Context, err error, isSubcommand bool) error { @@ -69,6 +33,45 @@ func StartBackupProc() *cli.Command { } } +func StartBackupProc() { + 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) + } +} + func getTimeSuffix() string { currTime := time.Now() diff --git a/main.go b/main.go index f6dbfa8..11bd6eb 100644 --- a/main.go +++ b/main.go @@ -21,8 +21,8 @@ func main() { }, Copyright: "(c) 2021-2022 netbenix", Commands: []*cli.Command{ - Commands.StartBackupProc(), - Commands.GenerateNewConfigs(), + Commands.StartBackupProcCommand(), + Commands.GenerateNewConfigsCommand(), }, }