(feature): handlig smb paths init

This commit is contained in:
Maksym Sadovnychyy 2024-11-01 10:40:21 +01:00
parent b0c0155e45
commit 5734fa9043
3 changed files with 236 additions and 147 deletions

View File

@ -4,6 +4,7 @@ using System.Diagnostics.CodeAnalysis;
using MaksIT.LTO.Core; using MaksIT.LTO.Core;
using MaksIT.LTO.Backup.Entities; using MaksIT.LTO.Backup.Entities;
using System.Net;
namespace MaksIT.LTO.Backup; namespace MaksIT.LTO.Backup;
public class Application { public class Application {
@ -58,51 +59,89 @@ public class Application {
Console.WriteLine("Tape ejected."); Console.WriteLine("Tape ejected.");
} }
public void CreateDescriptor(string directoryPath, string descriptorFilePath, uint blockSize) {
var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
// Define list to hold file descriptors public void PathAccessWrapper(WorkingFolder workingFolder, Action<string> myAction) {
var descriptor = new List<FileDescriptor>();
uint currentTapeBlock = 0;
foreach (var filePath in files) { if (workingFolder.LocalPath != null) {
var fileInfo = new FileInfo(filePath); var localPath = workingFolder.LocalPath.Path;
var relativePath = Path.GetRelativePath(directoryPath, filePath); var path = workingFolder.LocalPath.Path;
var numberOfBlocks = (uint)((fileInfo.Length + blockSize - 1) / blockSize);
// Optional: Calculate a simple hash for file integrity (e.g., MD5) myAction(path);
using var md5 = System.Security.Cryptography.MD5.Create(); }
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); else if (workingFolder.RemotePath != null) {
using var bufferedStream = new BufferedStream(fileStream, (int)blockSize); var remotePath = workingFolder.RemotePath;
byte[] buffer = new byte[blockSize]; if (remotePath.Protocol == "SMB") {
int bytesRead; NetworkCredential? networkCredential = default;
while ((bytesRead = bufferedStream.Read(buffer, 0, buffer.Length)) > 0) {
md5.TransformBlock(buffer, 0, bytesRead, null, 0); 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);
}
} }
md5.TransformFinalBlock(Array.Empty<byte>(), 0, 0); }
string fileHash = BitConverter.ToString(md5.Hash).Replace("-", "").ToLower(); }
descriptor.Add(new FileDescriptor { public void CreateDescriptor(WorkingFolder workingFolder, string descriptorFilePath, uint blockSize) {
StartBlock = currentTapeBlock, // Position of the file on the tape
NumberOfBlocks = numberOfBlocks, // Number of blocks used by the file PathAccessWrapper(workingFolder, (directoryPath) => {
FilePath = relativePath, var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
FileSize = fileInfo.Length,
CreationTime = fileInfo.CreationTime, // Define list to hold file descriptors
LastModifiedTime = fileInfo.LastWriteTime, var descriptor = new List<FileDescriptor>();
FileHash = fileHash uint currentTapeBlock = 0;
foreach (var filePath in files) {
var fileInfo = new FileInfo(filePath);
var relativePath = Path.GetRelativePath(directoryPath, filePath);
var numberOfBlocks = (uint)((fileInfo.Length + blockSize - 1) / blockSize);
// Optional: Calculate a simple hash for file integrity (e.g., MD5)
using var md5 = System.Security.Cryptography.MD5.Create();
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using var bufferedStream = new BufferedStream(fileStream, (int)blockSize);
byte[] buffer = new byte[blockSize];
int bytesRead;
while ((bytesRead = bufferedStream.Read(buffer, 0, buffer.Length)) > 0) {
md5.TransformBlock(buffer, 0, bytesRead, null, 0);
}
md5.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
string fileHash = BitConverter.ToString(md5.Hash).Replace("-", "").ToLower();
descriptor.Add(new FileDescriptor {
StartBlock = currentTapeBlock, // Position of the file on the tape
NumberOfBlocks = numberOfBlocks, // Number of blocks used by the file
FilePath = relativePath,
FileSize = fileInfo.Length,
CreationTime = fileInfo.CreationTime,
LastModifiedTime = fileInfo.LastWriteTime,
FileHash = fileHash
});
currentTapeBlock += numberOfBlocks;
}
// Convert descriptor list to JSON and include BlockSize
string descriptorJson = JsonSerializer.Serialize(new BackupDescriptor {
BlockSize = blockSize,
Files = descriptor
}); });
currentTapeBlock += numberOfBlocks; File.WriteAllText(descriptorFilePath, descriptorJson);
}
// Convert descriptor list to JSON and include BlockSize
string descriptorJson = JsonSerializer.Serialize(new BackupDescriptor {
BlockSize = blockSize,
Files = descriptor
}); });
File.WriteAllText(descriptorFilePath, descriptorJson);
} }
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) { private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
@ -115,80 +154,83 @@ public class Application {
} }
} }
public void WriteFilesToTape(string directoryPath, string descriptorFilePath, uint blockSize) { public void WriteFilesToTape(WorkingFolder workingFolder, string descriptorFilePath, uint blockSize) {
Console.WriteLine($"Writing files to tape from: {directoryPath}."); PathAccessWrapper(workingFolder, (directoryPath) => {
Console.WriteLine($"Block Size: {blockSize}."); Console.WriteLine($"Writing files to tape from: {directoryPath}.");
Console.WriteLine($"Block Size: {blockSize}.");
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapePath);
LoadTape(handler); LoadTape(handler);
handler.SetMediaParams(blockSize); handler.SetMediaParams(blockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000); Thread.Sleep(2000);
handler.Prepare(TapeDeviceHandler.TAPE_TENSION); handler.Prepare(TapeDeviceHandler.TAPE_TENSION);
Thread.Sleep(2000); Thread.Sleep(2000);
handler.Prepare(TapeDeviceHandler.TAPE_LOCK); handler.Prepare(TapeDeviceHandler.TAPE_LOCK);
Thread.Sleep(2000); Thread.Sleep(2000);
handler.WaitForTapeReady(); handler.WaitForTapeReady();
// Read descriptor from file system // Read descriptor from file system
string descriptorJson = File.ReadAllText(descriptorFilePath); string descriptorJson = File.ReadAllText(descriptorFilePath);
var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(descriptorJson); var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(descriptorJson);
if (descriptor == null) { if (descriptor == null) {
throw new InvalidOperationException("Failed to deserialize descriptor."); throw new InvalidOperationException("Failed to deserialize descriptor.");
} }
var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize; var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize;
foreach (var file in descriptor.Files) { foreach (var file in descriptor.Files) {
var filePath = Path.Combine(directoryPath, file.FilePath); var filePath = Path.Combine(directoryPath, file.FilePath);
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
using var bufferedStream = new BufferedStream(fileStream, (int)blockSize); using var bufferedStream = new BufferedStream(fileStream, (int)blockSize);
byte[] buffer = new byte[blockSize]; byte[] buffer = new byte[blockSize];
int bytesRead; int bytesRead;
for (var i = 0; i < file.NumberOfBlocks; i++) { for (var i = 0; i < file.NumberOfBlocks; i++) {
bytesRead = bufferedStream.Read(buffer, 0, buffer.Length); bytesRead = bufferedStream.Read(buffer, 0, buffer.Length);
if (bytesRead < buffer.Length) { if (bytesRead < buffer.Length) {
// Zero-fill the remaining part of the buffer if the last block is smaller than blockSize // Zero-fill the remaining part of the buffer if the last block is smaller than blockSize
Array.Clear(buffer, bytesRead, buffer.Length - bytesRead); Array.Clear(buffer, bytesRead, buffer.Length - bytesRead);
}
handler.WriteData(buffer);
currentTapeBlock++;
Thread.Sleep(100); // Small delay between blocks
} }
handler.WriteData(buffer); }
// write mark to indicate end of files
handler.WriteMarks(TapeDeviceHandler.TAPE_FILEMARKS, 1);
// write descriptor to tape
var descriptorData = Encoding.UTF8.GetBytes(descriptorJson);
var descriptorBlocks = (descriptorData.Length + blockSize - 1) / blockSize;
for (int i = 0; i < descriptorBlocks; i++) {
var startIndex = i * blockSize;
var length = Math.Min(blockSize, descriptorData.Length - startIndex);
byte[] block = new byte[blockSize]; // Initialized with zeros by default
Array.Copy(descriptorData, startIndex, block, 0, length);
handler.WriteData(block);
currentTapeBlock++; currentTapeBlock++;
Thread.Sleep(100); // Small delay between blocks Thread.Sleep(100); // Small delay between blocks
} }
}
// write 3 0 filled blocks to indicate end of backup
ZeroFillBlocks(handler, 3, blockSize);
// write mark to indicate end of files handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
handler.WriteMarks(TapeDeviceHandler.TAPE_FILEMARKS, 1); Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
// write descriptor to tape });
var descriptorData = Encoding.UTF8.GetBytes(descriptorJson);
var descriptorBlocks = (descriptorData.Length + blockSize - 1) / blockSize;
for (int i = 0; i < descriptorBlocks; i++) {
var startIndex = i * blockSize;
var length = Math.Min(blockSize, descriptorData.Length - startIndex);
byte[] block = new byte[blockSize]; // Initialized with zeros by default
Array.Copy(descriptorData, startIndex, block, 0, length);
handler.WriteData(block);
currentTapeBlock++;
Thread.Sleep(100); // Small delay between blocks
}
// write 3 0 filled blocks to indicate end of backup
ZeroFillBlocks(handler, 3, blockSize);
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
} }
public BackupDescriptor? FindDescriptor(uint blockSize) { public BackupDescriptor? FindDescriptor(uint blockSize) {
@ -256,65 +298,68 @@ public class Application {
return null; return null;
} }
public void RestoreDirectory(BackupDescriptor descriptor, string restoreDirectoryPath) { public void RestoreDirectory(BackupDescriptor descriptor, WorkingFolder workingFolder) {
Console.WriteLine("Restoring files to directory: " + restoreDirectoryPath);
Console.WriteLine("Block Size: " + descriptor.BlockSize);
using var handler = new TapeDeviceHandler(_tapePath); PathAccessWrapper(workingFolder, (restoreDirectoryPath) => {
Console.WriteLine("Restoring files to directory: " + restoreDirectoryPath);
Console.WriteLine("Block Size: " + descriptor.BlockSize);
LoadTape(handler); using var handler = new TapeDeviceHandler(_tapePath);
handler.SetMediaParams(descriptor.BlockSize); LoadTape(handler);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetMediaParams(descriptor.BlockSize);
Thread.Sleep(2000);
handler.WaitForTapeReady(); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
foreach (var file in descriptor.Files) {
// Set position to the start block of the file
handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock);
Thread.Sleep(2000); Thread.Sleep(2000);
var filePath = Path.Combine(restoreDirectoryPath, file.FilePath); handler.WaitForTapeReady();
var directoryPath = Path.GetDirectoryName(filePath);
if (directoryPath != null) {
Directory.CreateDirectory(directoryPath);
}
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { foreach (var file in descriptor.Files) {
var buffer = new byte[descriptor.BlockSize]; // Set position to the start block of the file
handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock);
Thread.Sleep(2000);
for (var i = 0; i < file.NumberOfBlocks; i++) { var filePath = Path.Combine(restoreDirectoryPath, file.FilePath);
var bytesRead = handler.ReadData(buffer, 0, buffer.Length); var directoryPath = Path.GetDirectoryName(filePath);
if (bytesRead < buffer.Length) { if (directoryPath != null) {
// Zero-fill the remaining part of the buffer if the last block is smaller than blockSize Directory.CreateDirectory(directoryPath);
Array.Clear(buffer, bytesRead, buffer.Length - bytesRead); }
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
var buffer = new byte[descriptor.BlockSize];
for (var i = 0; i < file.NumberOfBlocks; i++) {
var bytesRead = handler.ReadData(buffer, 0, buffer.Length);
if (bytesRead < buffer.Length) {
// Zero-fill the remaining part of the buffer if the last block is smaller than blockSize
Array.Clear(buffer, bytesRead, buffer.Length - bytesRead);
}
var bytesToWrite = (i == file.NumberOfBlocks - 1) ? (int)(file.FileSize % descriptor.BlockSize) : buffer.Length;
fileStream.Write(buffer, 0, bytesToWrite);
} }
}
var bytesToWrite = (i == file.NumberOfBlocks - 1) ? (int)(file.FileSize % descriptor.BlockSize) : buffer.Length; // check md5 checksum of restored file with the one in descriptor
fileStream.Write(buffer, 0, bytesToWrite); using (var md5 = System.Security.Cryptography.MD5.Create()) {
using (var fileStreamRead = new FileStream(filePath, FileMode.Open, FileAccess.Read)) {
var fileHash = md5.ComputeHash(fileStreamRead);
var fileHashString = BitConverter.ToString(fileHash).Replace("-", "").ToLower();
if (fileHashString != file.FileHash) {
Console.WriteLine($"Checksum mismatch for file: {filePath}");
}
else {
Console.WriteLine($"Restored file: {filePath}");
}
}
} }
} }
// check md5 checksum of restored file with the one in descriptor handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
using (var md5 = System.Security.Cryptography.MD5.Create()) { Thread.Sleep(2000);
using (var fileStreamRead = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { });
var fileHash = md5.ComputeHash(fileStreamRead);
var fileHashString = BitConverter.ToString(fileHash).Replace("-", "").ToLower();
if (fileHashString != file.FileHash) {
Console.WriteLine($"Checksum mismatch for file: {filePath}");
}
else {
Console.WriteLine($"Restored file: {filePath}");
}
}
}
}
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
} }
public int CheckMediaSize(string ltoGen) { public int CheckMediaSize(string ltoGen) {

View File

@ -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 class BackupItem {
public required string Name { get; set; } public required string Name { get; set; }
public required string Barcode { get; set; } public required string Barcode { get; set; }
public required string Source { get; set; } public required WorkingFolder Source { get; set; }
public required string Destination { get; set; } public required WorkingFolder Destination { get; set; }
public required string LTOGen { get; set; } public required string LTOGen { get; set; }
} }

View File

@ -2,18 +2,37 @@
"TapePath": "\\\\.\\Tape0", "TapePath": "\\\\.\\Tape0",
"Backups": [ "Backups": [
{ {
"Name": "Test", "Name": "Normal test",
"Barcode": "", "Barcode": "",
"Source": "F:\\LTO\\Backup", "LTOGen": "LTO5",
"Destination": "F:\\LTO\\Restore", "Source": {
"LTOGen": "LTO5" "LocalPath": {
"Path": "D:\\Drivers"
}
},
"Destination": {
"LocalPath": {
"Path": "F:\\LTO\\Restore"
}
}
}, },
{ {
"Name": "Drivers", "Name": "Network test",
"Barcode": "", "Barcode": "",
"Source": "D:\\Drivers", "LTOGen": "LTO5",
"Destination": "F:\\LTO\\Restore", "Source": {
"LTOGen": "LTO5" "RemotePath": {
"Path": "\\\\nasrv0001.corp.maks-it.com\\LTO\\Backup",
"Credentials": {
"Username": "user",
"Password": "password"
}
}
},
"Destination": {
"Path": "F:\\LTO\\Restore"
}
} }
}
] ]
} }