feat(Config): Added remote target path

This commit is contained in:
netbenix 2021-12-16 18:34:35 +01:00
parent b8e9ddd838
commit cad2dd18d3
5 changed files with 14 additions and 8 deletions

View file

@ -5,8 +5,8 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/Azure/azure-storage-file-go/azfile" "github.com/Azure/azure-storage-file-go/azfile"
"github.com/google/uuid"
"github.com/cheggaaa/pb/v3" "github.com/cheggaaa/pb/v3"
"github.com/google/uuid"
"net/url" "net/url"
"os" "os"
"path/filepath" "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") logger := Logging.DetailedLogger("AzureFileStorage", "upload")
file, err := os.Open(fileName) file, err := os.Open(fileName)
@ -43,6 +43,10 @@ func (azure AzureFileStorage) upload(fileName string, backupName string){
logger.Fatal(err) 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))) 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{}))

View file

@ -3,11 +3,11 @@ package StorageTypes
import "scabiosa/SQL" import "scabiosa/SQL"
type Storage interface { type Storage interface {
upload(fileName string, backupName string) upload(fileName string, backupName string, destinationPath string)
} }
func UploadFile(storage Storage, fileName string, backupName string){ func UploadFile(storage Storage, fileName string, backupName string, destinationPath string){
storage.upload(fileName, backupName) storage.upload(fileName, backupName, destinationPath)
} }
func CheckStorageType(storageType string) Storage{ func CheckStorageType(storageType string) Storage{

View file

@ -21,6 +21,7 @@ type Config struct {
BackupName string `json:"backupName"` BackupName string `json:"backupName"`
FolderPath string `json:"folderPath"` FolderPath string `json:"folderPath"`
StorageType string `json:"storageType"` StorageType string `json:"storageType"`
TargetPath string `json:"targetPath"`
CreateLocalBackup bool `json:"createLocalBackup"` CreateLocalBackup bool `json:"createLocalBackup"`
} `json:"foldersToBackup"` } `json:"foldersToBackup"`
} }

View file

@ -13,6 +13,7 @@
"backupName": "", "backupName": "",
"folderPath": "", "folderPath": "",
"storageType": "", "storageType": "",
"targetPath": "",
"createLocalBackup": false "createLocalBackup": false
} }
] ]

View file

@ -18,10 +18,10 @@ func main(){
for _, backupItem := range config.FolderToBackup{ for _, backupItem := range config.FolderToBackup{
storage := StorageTypes.CheckStorageType(backupItem.StorageType) 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) 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 { if !backupItem.CreateLocalBackup {
_ = os.Remove(bakFile) _ = os.Remove(bakFile)
@ -39,7 +39,7 @@ func getTimeSuffix() string{
return "_" + currTime.Format("02-01-2006_15-04") 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") logger := Logging.DetailedLogger("mainThread", "checkTmpPath")
if !createLocalBackup{ if !createLocalBackup{
if _, err := os.Stat("tmp"); os.IsNotExist(err) { if _, err := os.Stat("tmp"); os.IsNotExist(err) {