Compare commits

...

8 Commits

9 changed files with 555 additions and 201 deletions

102
README.md
View File

@ -21,23 +21,52 @@ A C# application designed to facilitate backup and restore operations to an LTO
1. Clone the repository:
```bash
git clone https://github.com/your-username/MaksIT.LTO.Backup.git
git clone https://github.com/MAKS-IT-COM/maksit-lto-backup
```
2. Ensure `.NET Core` is installed on your system.
2. Ensure `.NET8 SDK` is installed on your system.
3. Prepare a `configuration.json` file in the application directory with the following structure:
```json
{
"TapePath": "YourTapePath",
"Backups": [
{
"Name": "BackupName",
"Barcode": "123456",
"Source": "path/to/source",
"Destination": "path/to/destination",
"LTOGen": "LTO6"
}
]
}
{
"TapePath": "\\\\.\\Tape0",
"WriteDelay": 100,
"Backups": [
{
"Name": "Normal test",
"Barcode": "",
"LTOGen": "LTO5",
"Source": {
"LocalPath": {
"Path": "F:\\LTO\\Backup"
}
},
"Destination": {
"LocalPath": {
"Path": "F:\\LTO\\Restore"
}
}
},
{
"Name": "Network test",
"Barcode": "",
"LTOGen": "LTO5",
"Source": {
"RemotePath": {
"Path": "\\\\nassrv0001.corp.maks-it.com\\data-1\\Users",
"PasswordCredentials": {
"Username": "",
"Password": ""
},
"Protocol": "SMB"
}
},
"Destination": {
"LocalPath": {
"Path": "F:\\LTO\\Restore"
}
}
}
]
}
```
## Usage
@ -57,7 +86,11 @@ Upon running, the following options will be presented:
2. **Backup**: Prompts the user to select a backup task from the configured list and initiates the backup process.
3. **Restore**: Restores a previously backed-up directory from the tape.
4. **Eject Tape**: Ejects the tape from the drive safely.
5. **Exit**: Exits the application.
5. **Get device status**: Mostly used for debugging to understand if device is able to write
6. **Tape Erase (Short)**:
7. **Reload configurations**
6. **Exit**: Exits the application.
### Code Overview
@ -72,13 +105,42 @@ Below is an example configuration setup for an LTO-6 tape generation backup oper
```json
{
"TapePath": "\\\\.\\Tape0",
"WriteDelay": 100,
"Backups": [
{
"Name": "Monthly Backup",
"Barcode": "MB12345",
"Source": "/path/to/source",
"Destination": "/path/to/restore",
"LTOGen": "LTO6"
"Name": "Normal test",
"Barcode": "",
"LTOGen": "LTO5",
"Source": {
"LocalPath": {
"Path": "F:\\LTO\\Backup"
}
},
"Destination": {
"LocalPath": {
"Path": "F:\\LTO\\Restore"
}
}
},
{
"Name": "Network test",
"Barcode": "",
"LTOGen": "LTO5",
"Source": {
"RemotePath": {
"Path": "\\\\nassrv0001.corp.maks-it.com\\data-1\\Users",
"PasswordCredentials": {
"Username": "",
"Password": ""
},
"Protocol": "SMB"
}
},
"Destination": {
"LocalPath": {
"Path": "F:\\LTO\\Restore\\Users"
}
}
}
]
}

View File

@ -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,72 +59,11 @@ public class Application {
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
var descriptor = new List<FileDescriptor>();
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
});
File.WriteAllText(descriptorFilePath, descriptorJson);
}
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape.");
Console.WriteLine($"Block Size: {blockSize}.");
for (int i = 0; i < blocks; i++) {
handler.WriteData(new byte[blockSize]);
Thread.Sleep(100);
}
}
public void WriteFilesToTape(string directoryPath, string descriptorFilePath, uint blockSize) {
Console.WriteLine($"Writing files to tape from: {directoryPath}.");
Console.WriteLine($"Block Size: {blockSize}.");
public void TapeErase() {
using var handler = new TapeDeviceHandler(_tapePath);
LoadTape(handler);
handler.SetMediaParams(blockSize);
handler.SetMediaParams(LTOBlockSizes.LTO5);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
@ -134,61 +74,213 @@ public class Application {
handler.Prepare(TapeDeviceHandler.TAPE_LOCK);
Thread.Sleep(2000);
handler.WaitForTapeReady();
// Read descriptor from file system
string descriptorJson = File.ReadAllText(descriptorFilePath);
var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(descriptorJson);
if (descriptor == null) {
throw new InvalidOperationException("Failed to deserialize descriptor.");
}
var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize;
foreach (var file in descriptor.Files) {
var filePath = Path.Combine(directoryPath, file.FilePath);
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;
for (var i = 0; i < file.NumberOfBlocks; i++) {
bytesRead = bufferedStream.Read(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);
}
handler.WriteData(buffer);
currentTapeBlock++;
Thread.Sleep(100); // Small delay between blocks
}
}
// 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++;
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);
handler.Erase(TapeDeviceHandler.TAPE_ERASE_SHORT);
Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
Console.WriteLine("Tape erased.");
}
public void GetDeviceStatus() {
using var handler = new TapeDeviceHandler(_tapePath);
handler.GetStatus();
}
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
var descriptor = new List<FileDescriptor>();
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
});
File.WriteAllText(descriptorFilePath, descriptorJson);
});
}
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape.");
Console.WriteLine($"Block Size: {blockSize}.");
var writeError = 0;
for (int i = 0; i < blocks; i++) {
writeError = handler.WriteData(new byte[blockSize]);
if (writeError != 0)
return;
Thread.Sleep(_configuration.WriteDelay);
}
}
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}.");
using var handler = new TapeDeviceHandler(_tapePath);
LoadTape(handler);
handler.SetMediaParams(blockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
handler.Prepare(TapeDeviceHandler.TAPE_TENSION);
Thread.Sleep(2000);
handler.Prepare(TapeDeviceHandler.TAPE_LOCK);
Thread.Sleep(2000);
handler.WaitForTapeReady();
// Read descriptor from file system
string descriptorJson = File.ReadAllText(descriptorFilePath);
var descriptor = JsonSerializer.Deserialize<BackupDescriptor>(descriptorJson);
if (descriptor == null) {
throw new InvalidOperationException("Failed to deserialize descriptor.");
}
var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize;
int writeError = 0;
foreach (var file in descriptor.Files) {
var filePath = Path.Combine(directoryPath, file.FilePath);
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;
for (var i = 0; i < file.NumberOfBlocks; i++) {
bytesRead = bufferedStream.Read(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);
}
writeError = handler.WriteData(buffer);
if (writeError != 0) {
Console.WriteLine($"Failed to write file: {filePath}");
return;
}
currentTapeBlock++;
Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks
}
}
// write mark to indicate end of files
handler.WriteMarks(TapeDeviceHandler.TAPE_FILEMARKS, 1);
Thread.Sleep(_configuration.WriteDelay);
// 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);
writeError = handler.WriteData(block);
if (writeError != 0)
return;
currentTapeBlock++;
Thread.Sleep(_configuration.WriteDelay); // 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) {
@ -250,71 +342,73 @@ public class Application {
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
return null;
}
public void RestoreDirectory(BackupDescriptor descriptor, string restoreDirectoryPath) {
Console.WriteLine("Restoring files to directory: " + restoreDirectoryPath);
Console.WriteLine("Block Size: " + descriptor.BlockSize);
public void RestoreDirectory(BackupDescriptor descriptor, WorkingFolder workingFolder) {
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);
Thread.Sleep(2000);
handler.SetMediaParams(descriptor.BlockSize);
handler.WaitForTapeReady();
foreach (var file in descriptor.Files) {
// Set position to the start block of the file
handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
var filePath = Path.Combine(restoreDirectoryPath, file.FilePath);
var directoryPath = Path.GetDirectoryName(filePath);
if (directoryPath != null) {
Directory.CreateDirectory(directoryPath);
}
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);
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) {
var buffer = new byte[descriptor.BlockSize];
var filePath = Path.Combine(restoreDirectoryPath, file.FilePath);
var directoryPath = Path.GetDirectoryName(filePath);
if (directoryPath != null) {
Directory.CreateDirectory(directoryPath);
}
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);
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;
fileStream.Write(buffer, 0, bytesToWrite);
// check md5 checksum of restored file with the one in descriptor
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
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}");
}
}
}
}
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
Thread.Sleep(2000);
});
}
public int CheckMediaSize(string ltoGen) {
@ -351,7 +445,7 @@ public class Application {
Console.WriteLine("\nSelect a backup to perform:");
for (int i = 0; i < _configuration.Backups.Count; i++) {
var backupInt = _configuration.Backups[i];
Console.WriteLine($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {backupInt.Barcode}, Source: {backupInt.Source}, Destination: {backupInt.Destination}");
Console.WriteLine($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {(string.IsNullOrEmpty(backupInt.Barcode) ? "None" : backupInt.Barcode)}, Source: {backupInt.Source}, Destination: {backupInt.Destination}");
}
Console.Write("Enter your choice (or '0' to go back): ");
@ -392,7 +486,7 @@ public class Application {
Console.WriteLine("\nSelect a backup to restore:");
for (int i = 0; i < _configuration.Backups.Count; i++) {
var backupInt = _configuration.Backups[i];
Console.WriteLine($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {backupInt.Barcode}, Source: {backupInt.Source}, Destination: {backupInt.Destination}");
Console.WriteLine($"{i + 1}. Backup Name: {backupInt.Name}, Bar code {(string.IsNullOrEmpty(backupInt.Barcode) ? "None" : backupInt.Barcode)}, Source: {backupInt.Source}, Destination: {backupInt.Destination}");
}
Console.Write("Enter your choice (or '0' to go back): ");

View File

@ -1,14 +1,40 @@
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 string 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; }
}
public class Configuration {
public required string TapePath { get; set; }
public required int WriteDelay { get; set; }
public required List<BackupItem> Backups { get; set; }
}

View File

@ -18,8 +18,10 @@ class Program {
Console.WriteLine("2. Backup");
Console.WriteLine("3. Restore");
Console.WriteLine("4. Eject tape");
Console.WriteLine("5. Reload configurations");
Console.WriteLine("6. Exit");
Console.WriteLine("5. Get device status");
Console.WriteLine("6. Tape Erase (Short)");
Console.WriteLine("7. Reload configurations");
Console.WriteLine("8. Exit");
Console.Write("Enter your choice: ");
var choice = Console.ReadLine();
@ -38,11 +40,16 @@ class Program {
case "4":
app.EjectTape();
break;
case "5":
app.LoadConfiguration();
app.GetDeviceStatus();
break;
case "6":
app.TapeErase();
break;
case "7":
app.LoadConfiguration();
break;
case "8":
Console.WriteLine("Exiting...");
return;
default:

View File

@ -1,19 +1,41 @@
{
"TapePath": "\\\\.\\Tape0",
"WriteDelay": 100,
"Backups": [
{
"Name": "Test",
"Name": "Normal test",
"Barcode": "",
"Source": "F:\\LTO\\Backup",
"Destination": "F:\\LTO\\Restore",
"LTOGen": "LTO5"
"LTOGen": "LTO5",
"Source": {
"LocalPath": {
"Path": "F:\\LTO\\Backup"
}
},
"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": "\\\\nassrv0002.corp.maks-it.com\\data-1\\Users",
"PasswordCredentials": {
"Username": "",
"Password": ""
},
"Protocol": "SMB"
}
},
"Destination": {
"LocalPath": {
"Path": "F:\\LTO\\Restore"
}
}
}
]
}

View File

@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MaksIT.LTO.Core;
public class DriverManager {
public static void RestartDriver(string deviceName) {
string script = $@"
$device = Get-PnpDevice -FriendlyName '{deviceName}'
Disable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
Start-Sleep -Seconds 5
Enable-PnpDevice -InstanceId $device.InstanceId -Confirm:$false
";
ProcessStartInfo psi = new ProcessStartInfo {
FileName = "powershell.exe",
Arguments = $"-NoProfile -ExecutionPolicy Bypass -Command \"{script}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
using (Process process = Process.Start(psi)) {
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
Console.WriteLine(output);
if (!string.IsNullOrEmpty(error)) {
Console.WriteLine($"Error: {error}");
}
}
}
}

View File

@ -0,0 +1,102 @@
using System.Net;
using System.Runtime.InteropServices;
namespace MaksIT.LTO.Core;
//public void RestoreFilesFromSmbShare(string smbPath, string username, string password, string domain, string restoreDirectory) {
// var credentials = new NetworkCredential(username, password, domain);
// using (new NetworkConnection(smbPath, credentials)) {
// var files = Directory.GetFiles(smbPath, "*.*", SearchOption.AllDirectories);
// foreach (var file in files) {
// var relativePath = Path.GetRelativePath(smbPath, file);
// var destinationPath = Path.Combine(restoreDirectory, relativePath);
// Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
// File.Copy(file, destinationPath, overwrite: true);
// Console.WriteLine($"Restored file: {file} to {destinationPath}");
// }
// }
//}
public class NetworkConnection : IDisposable {
private readonly string _networkName;
public NetworkConnection(string networkName, NetworkCredential credentials) {
_networkName = networkName;
var netResource = new NetResource {
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplayType.Share,
RemoteName = networkName
};
var result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);
if (result != 0) {
throw new InvalidOperationException($"Error connecting to remote share: {result}");
}
}
~NetworkConnection() {
Dispose(false);
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
WNetCancelConnection2(_networkName, 0, true);
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags, bool force);
[StructLayout(LayoutKind.Sequential)]
public class NetResource {
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplayType DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int {
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
}
public enum ResourceType : int {
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8
}
public enum ResourceDisplayType : int {
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
}

View File

@ -82,7 +82,7 @@ public partial class TapeDeviceHandler : IDisposable {
}
public void WriteData(byte[] data) {
public int WriteData(byte[] data) {
IntPtr unmanagedPointer = Marshal.AllocHGlobal(data.Length);
try {
Marshal.Copy(data, 0, unmanagedPointer, data.Length);
@ -94,11 +94,14 @@ public partial class TapeDeviceHandler : IDisposable {
else {
int error = Marshal.GetLastWin32Error();
Console.WriteLine($"Write Data: Failed with error code {error}");
return error;
}
}
finally {
Marshal.FreeHGlobal(unmanagedPointer);
}
return 0;
}
public byte[] ReadData(uint length) {
@ -151,10 +154,8 @@ public partial class TapeDeviceHandler : IDisposable {
if (errorCode == 0) // Assuming 0 means success/ready
{
isReady = true;
Console.WriteLine("Tape is ready.");
}
else {
Console.WriteLine($"Tape not ready, status code: {errorCode}. Retrying...");
Thread.Sleep(1000); // Wait 1 second before checking again
}
}

View File

@ -325,7 +325,7 @@ public partial class TapeDeviceHandler : IDisposable {
/// Erase the tape
/// </summary>
/// <param name="type">The type of erase operation. Valid values are <see cref="TAPE_ERASE_SHORT"/> and <see cref="TAPE_ERASE_LONG"/>.</param>
public void Erase(uint type) {
public int Erase(uint type) {
TAPE_ERASE erase = new TAPE_ERASE {
Type = type,
Immediate = 0
@ -343,11 +343,14 @@ public partial class TapeDeviceHandler : IDisposable {
else {
int error = Marshal.GetLastWin32Error();
Console.WriteLine($"Erase Tape: Failed with error code {error}");
return error;
}
}
finally {
Marshal.FreeHGlobal(inBuffer);
}
return 0;
}
/// <summary>