diff --git a/StorageTypes/AzureFileStorage.go b/StorageTypes/AzureFileStorage.go index 73ba9b3..142be30 100644 --- a/StorageTypes/AzureFileStorage.go +++ b/StorageTypes/AzureFileStorage.go @@ -5,8 +5,8 @@ import ( "encoding/json" "fmt" "github.com/Azure/azure-storage-file-go/azfile" - "github.com/google/uuid" "github.com/cheggaaa/pb/v3" + "github.com/google/uuid" "net/url" "os" "path/filepath" @@ -24,7 +24,7 @@ type AzureFileStorage struct{ } -func (azure AzureFileStorage) upload(fileName string, backupName string){ +func (azure AzureFileStorage) upload(fileName string, backupName string, destinationPath string){ logger := Logging.DetailedLogger("AzureFileStorage", "upload") file, err := os.Open(fileName) @@ -43,6 +43,10 @@ func (azure AzureFileStorage) upload(fileName string, backupName string){ logger.Fatal(err) } + if destinationPath != ""{ + 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{})) diff --git a/StorageTypes/StorageInterface.go b/StorageTypes/StorageInterface.go index 43e243f..f9fda1f 100644 --- a/StorageTypes/StorageInterface.go +++ b/StorageTypes/StorageInterface.go @@ -3,11 +3,11 @@ package StorageTypes import "scabiosa/SQL" type Storage interface { - upload(fileName string, backupName string) + upload(fileName string, backupName string, destinationPath string) } -func UploadFile(storage Storage, fileName string, backupName string){ - storage.upload(fileName, backupName) +func UploadFile(storage Storage, fileName string, backupName string, destinationPath string){ + storage.upload(fileName, backupName, destinationPath) } func CheckStorageType(storageType string) Storage{ diff --git a/Tools/Config.go b/Tools/Config.go index 49fe55e..a28b034 100644 --- a/Tools/Config.go +++ b/Tools/Config.go @@ -21,6 +21,7 @@ type Config struct { BackupName string `json:"backupName"` FolderPath string `json:"folderPath"` StorageType string `json:"storageType"` + TargetPath string `json:"targetPath"` CreateLocalBackup bool `json:"createLocalBackup"` } `json:"foldersToBackup"` } diff --git a/config/config.json b/config/config.json index b2368c1..21652af 100644 --- a/config/config.json +++ b/config/config.json @@ -13,6 +13,7 @@ "backupName": "", "folderPath": "", "storageType": "", + "targetPath": "", "createLocalBackup": false } ] diff --git a/main.go b/main.go index e7dc98c..e9136b1 100644 --- a/main.go +++ b/main.go @@ -18,10 +18,10 @@ func main(){ for _, backupItem := range config.FolderToBackup{ storage := StorageTypes.CheckStorageType(backupItem.StorageType) - destPath := checkTmpPath(config, backupItem.CreateLocalBackup) + destPath := checkTmpPath(config, backupItem.CreateLocalBackup, storage) bakFile := Compressor.CreateBakFile(backupItem.BackupName + getTimeSuffix(), backupItem.FolderPath, destPath, backupItem.BackupName) - StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName) + StorageTypes.UploadFile(storage, bakFile, backupItem.BackupName, backupItem.TargetPath) if !backupItem.CreateLocalBackup { _ = os.Remove(bakFile) @@ -39,7 +39,7 @@ func getTimeSuffix() string{ return "_" + currTime.Format("02-01-2006_15-04") } -func checkTmpPath(config Tools.Config, createLocalBackup bool) string{ +func checkTmpPath(config Tools.Config, createLocalBackup bool, storage StorageTypes.Storage) string{ logger := Logging.DetailedLogger("mainThread", "checkTmpPath") if !createLocalBackup{ if _, err := os.Stat("tmp"); os.IsNotExist(err) {