(feature): handlig smb paths init
This commit is contained in:
parent
b0c0155e45
commit
5734fa9043
@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
using MaksIT.LTO.Core;
|
||||
using MaksIT.LTO.Backup.Entities;
|
||||
using System.Net;
|
||||
|
||||
namespace MaksIT.LTO.Backup;
|
||||
public class Application {
|
||||
@ -58,7 +59,44 @@ public class Application {
|
||||
Console.WriteLine("Tape ejected.");
|
||||
}
|
||||
|
||||
public void CreateDescriptor(string directoryPath, string descriptorFilePath, uint blockSize) {
|
||||
|
||||
public void PathAccessWrapper(WorkingFolder workingFolder, Action<string> myAction) {
|
||||
|
||||
if (workingFolder.LocalPath != null) {
|
||||
var localPath = workingFolder.LocalPath.Path;
|
||||
var path = workingFolder.LocalPath.Path;
|
||||
|
||||
myAction(path);
|
||||
}
|
||||
else if (workingFolder.RemotePath != null) {
|
||||
var remotePath = workingFolder.RemotePath;
|
||||
|
||||
if (remotePath.Protocol == "SMB") {
|
||||
NetworkCredential? networkCredential = default;
|
||||
|
||||
if (remotePath.PasswordCredentials != null) {
|
||||
var username = remotePath.PasswordCredentials.Username;
|
||||
var password = remotePath.PasswordCredentials.Password;
|
||||
|
||||
networkCredential = new NetworkCredential(username, password);
|
||||
}
|
||||
|
||||
var smbPath = remotePath.Path;
|
||||
|
||||
if (networkCredential == null) {
|
||||
throw new InvalidOperationException("Network credentials are required for remote paths.");
|
||||
}
|
||||
|
||||
using (new NetworkConnection(smbPath, networkCredential)) {
|
||||
myAction(smbPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CreateDescriptor(WorkingFolder workingFolder, string descriptorFilePath, uint blockSize) {
|
||||
|
||||
PathAccessWrapper(workingFolder, (directoryPath) => {
|
||||
var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
|
||||
|
||||
// Define list to hold file descriptors
|
||||
@ -103,6 +141,7 @@ public class Application {
|
||||
});
|
||||
|
||||
File.WriteAllText(descriptorFilePath, descriptorJson);
|
||||
});
|
||||
}
|
||||
|
||||
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
|
||||
@ -115,7 +154,8 @@ public class Application {
|
||||
}
|
||||
}
|
||||
|
||||
public void WriteFilesToTape(string directoryPath, string descriptorFilePath, uint blockSize) {
|
||||
public void WriteFilesToTape(WorkingFolder workingFolder, string descriptorFilePath, uint blockSize) {
|
||||
PathAccessWrapper(workingFolder, (directoryPath) => {
|
||||
Console.WriteLine($"Writing files to tape from: {directoryPath}.");
|
||||
Console.WriteLine($"Block Size: {blockSize}.");
|
||||
|
||||
@ -189,6 +229,8 @@ public class Application {
|
||||
Thread.Sleep(2000);
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
Thread.Sleep(2000);
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public BackupDescriptor? FindDescriptor(uint blockSize) {
|
||||
@ -256,7 +298,9 @@ public class Application {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RestoreDirectory(BackupDescriptor descriptor, string restoreDirectoryPath) {
|
||||
public void RestoreDirectory(BackupDescriptor descriptor, WorkingFolder workingFolder) {
|
||||
|
||||
PathAccessWrapper(workingFolder, (restoreDirectoryPath) => {
|
||||
Console.WriteLine("Restoring files to directory: " + restoreDirectoryPath);
|
||||
Console.WriteLine("Block Size: " + descriptor.BlockSize);
|
||||
|
||||
@ -315,6 +359,7 @@ public class Application {
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
Thread.Sleep(2000);
|
||||
});
|
||||
}
|
||||
|
||||
public int CheckMediaSize(string ltoGen) {
|
||||
|
||||
@ -1,10 +1,35 @@
|
||||
namespace MaksIT.LTO.Backup;
|
||||
using System.Security;
|
||||
|
||||
namespace MaksIT.LTO.Backup;
|
||||
|
||||
public abstract class PathBase {
|
||||
public required string Path { get; set; }
|
||||
}
|
||||
|
||||
public class LocalPath : PathBase {
|
||||
// Additional properties specific to local paths can be added here
|
||||
}
|
||||
|
||||
public class PasswordCredentials {
|
||||
public required string Username { get; set; }
|
||||
public required SecureString Password { get; set; }
|
||||
}
|
||||
|
||||
public class RemotePath : PathBase {
|
||||
public PasswordCredentials? PasswordCredentials { get; set; }
|
||||
public required string Protocol { get; set; } // e.g., SMB, FTP, etc.
|
||||
}
|
||||
|
||||
public class WorkingFolder {
|
||||
public LocalPath? LocalPath { get; set; }
|
||||
public RemotePath? RemotePath { get; set; }
|
||||
}
|
||||
|
||||
public class BackupItem {
|
||||
public required string Name { get; set; }
|
||||
public required string Barcode { get; set; }
|
||||
public required string Source { get; set; }
|
||||
public required string Destination { get; set; }
|
||||
public required WorkingFolder Source { get; set; }
|
||||
public required WorkingFolder Destination { get; set; }
|
||||
public required string LTOGen { get; set; }
|
||||
}
|
||||
|
||||
|
||||
@ -2,18 +2,37 @@
|
||||
"TapePath": "\\\\.\\Tape0",
|
||||
"Backups": [
|
||||
{
|
||||
"Name": "Test",
|
||||
"Name": "Normal test",
|
||||
"Barcode": "",
|
||||
"Source": "F:\\LTO\\Backup",
|
||||
"Destination": "F:\\LTO\\Restore",
|
||||
"LTOGen": "LTO5"
|
||||
"LTOGen": "LTO5",
|
||||
"Source": {
|
||||
"LocalPath": {
|
||||
"Path": "D:\\Drivers"
|
||||
}
|
||||
},
|
||||
"Destination": {
|
||||
"LocalPath": {
|
||||
"Path": "F:\\LTO\\Restore"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"Name": "Drivers",
|
||||
"Name": "Network test",
|
||||
"Barcode": "",
|
||||
"Source": "D:\\Drivers",
|
||||
"Destination": "F:\\LTO\\Restore",
|
||||
"LTOGen": "LTO5"
|
||||
"LTOGen": "LTO5",
|
||||
"Source": {
|
||||
"RemotePath": {
|
||||
"Path": "\\\\nasrv0001.corp.maks-it.com\\LTO\\Backup",
|
||||
"Credentials": {
|
||||
"Username": "user",
|
||||
"Password": "password"
|
||||
}
|
||||
}
|
||||
},
|
||||
"Destination": {
|
||||
"Path": "F:\\LTO\\Restore"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user