Compare commits
8 Commits
4e076e90cf
...
ba0d08a89e
| Author | SHA1 | Date | |
|---|---|---|---|
| ba0d08a89e | |||
| 95bf055a1c | |||
| b518360409 | |||
| 4d7e11544b | |||
| 4826d124f7 | |||
| 6b6c3a1c7f | |||
| 5734fa9043 | |||
| b0c0155e45 |
90
README.md
90
README.md
@ -21,20 +21,49 @@ A C# application designed to facilitate backup and restore operations to an LTO
|
|||||||
|
|
||||||
1. Clone the repository:
|
1. Clone the repository:
|
||||||
```bash
|
```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:
|
3. Prepare a `configuration.json` file in the application directory with the following structure:
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"TapePath": "YourTapePath",
|
"TapePath": "\\\\.\\Tape0",
|
||||||
|
"WriteDelay": 100,
|
||||||
"Backups": [
|
"Backups": [
|
||||||
{
|
{
|
||||||
"Name": "BackupName",
|
"Name": "Normal test",
|
||||||
"Barcode": "123456",
|
"Barcode": "",
|
||||||
"Source": "path/to/source",
|
"LTOGen": "LTO5",
|
||||||
"Destination": "path/to/destination",
|
"Source": {
|
||||||
"LTOGen": "LTO6"
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
@ -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.
|
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.
|
3. **Restore**: Restores a previously backed-up directory from the tape.
|
||||||
4. **Eject Tape**: Ejects the tape from the drive safely.
|
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
|
### Code Overview
|
||||||
|
|
||||||
@ -72,13 +105,42 @@ Below is an example configuration setup for an LTO-6 tape generation backup oper
|
|||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"TapePath": "\\\\.\\Tape0",
|
"TapePath": "\\\\.\\Tape0",
|
||||||
|
"WriteDelay": 100,
|
||||||
"Backups": [
|
"Backups": [
|
||||||
{
|
{
|
||||||
"Name": "Monthly Backup",
|
"Name": "Normal test",
|
||||||
"Barcode": "MB12345",
|
"Barcode": "",
|
||||||
"Source": "/path/to/source",
|
"LTOGen": "LTO5",
|
||||||
"Destination": "/path/to/restore",
|
"Source": {
|
||||||
"LTOGen": "LTO6"
|
"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"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,7 +59,74 @@ public class Application {
|
|||||||
Console.WriteLine("Tape ejected.");
|
Console.WriteLine("Tape ejected.");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateDescriptor(string directoryPath, string descriptorFilePath, uint blockSize) {
|
public void TapeErase() {
|
||||||
|
using var handler = new TapeDeviceHandler(_tapePath);
|
||||||
|
LoadTape(handler);
|
||||||
|
|
||||||
|
handler.SetMediaParams(LTOBlockSizes.LTO5);
|
||||||
|
|
||||||
|
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.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);
|
var files = Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories);
|
||||||
|
|
||||||
// Define list to hold file descriptors
|
// Define list to hold file descriptors
|
||||||
@ -103,19 +171,26 @@ public class Application {
|
|||||||
});
|
});
|
||||||
|
|
||||||
File.WriteAllText(descriptorFilePath, descriptorJson);
|
File.WriteAllText(descriptorFilePath, descriptorJson);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
|
private void ZeroFillBlocks(TapeDeviceHandler handler, int blocks, uint blockSize) {
|
||||||
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape.");
|
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape.");
|
||||||
Console.WriteLine($"Block Size: {blockSize}.");
|
Console.WriteLine($"Block Size: {blockSize}.");
|
||||||
|
|
||||||
|
var writeError = 0;
|
||||||
|
|
||||||
for (int i = 0; i < blocks; i++) {
|
for (int i = 0; i < blocks; i++) {
|
||||||
handler.WriteData(new byte[blockSize]);
|
writeError = handler.WriteData(new byte[blockSize]);
|
||||||
Thread.Sleep(100);
|
if (writeError != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
Thread.Sleep(_configuration.WriteDelay);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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($"Writing files to tape from: {directoryPath}.");
|
||||||
Console.WriteLine($"Block Size: {blockSize}.");
|
Console.WriteLine($"Block Size: {blockSize}.");
|
||||||
|
|
||||||
@ -146,6 +221,9 @@ public class Application {
|
|||||||
|
|
||||||
var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize;
|
var currentTapeBlock = (descriptorJson.Length + blockSize - 1) / blockSize;
|
||||||
|
|
||||||
|
|
||||||
|
int writeError = 0;
|
||||||
|
|
||||||
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);
|
||||||
@ -159,15 +237,22 @@ public class Application {
|
|||||||
// 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);
|
|
||||||
|
writeError = handler.WriteData(buffer);
|
||||||
|
if (writeError != 0) {
|
||||||
|
Console.WriteLine($"Failed to write file: {filePath}");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
currentTapeBlock++;
|
currentTapeBlock++;
|
||||||
Thread.Sleep(100); // Small delay between blocks
|
Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// write mark to indicate end of files
|
// write mark to indicate end of files
|
||||||
handler.WriteMarks(TapeDeviceHandler.TAPE_FILEMARKS, 1);
|
handler.WriteMarks(TapeDeviceHandler.TAPE_FILEMARKS, 1);
|
||||||
|
Thread.Sleep(_configuration.WriteDelay);
|
||||||
|
|
||||||
// write descriptor to tape
|
// write descriptor to tape
|
||||||
var descriptorData = Encoding.UTF8.GetBytes(descriptorJson);
|
var descriptorData = Encoding.UTF8.GetBytes(descriptorJson);
|
||||||
@ -177,9 +262,13 @@ public class Application {
|
|||||||
var length = Math.Min(blockSize, descriptorData.Length - startIndex);
|
var length = Math.Min(blockSize, descriptorData.Length - startIndex);
|
||||||
byte[] block = new byte[blockSize]; // Initialized with zeros by default
|
byte[] block = new byte[blockSize]; // Initialized with zeros by default
|
||||||
Array.Copy(descriptorData, startIndex, block, 0, length);
|
Array.Copy(descriptorData, startIndex, block, 0, length);
|
||||||
handler.WriteData(block);
|
|
||||||
|
writeError = handler.WriteData(block);
|
||||||
|
if (writeError != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
currentTapeBlock++;
|
currentTapeBlock++;
|
||||||
Thread.Sleep(100); // Small delay between blocks
|
Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks
|
||||||
}
|
}
|
||||||
|
|
||||||
// write 3 0 filled blocks to indicate end of backup
|
// write 3 0 filled blocks to indicate end of backup
|
||||||
@ -187,8 +276,11 @@ public class Application {
|
|||||||
|
|
||||||
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
|
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
|
|
||||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
|
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public BackupDescriptor? FindDescriptor(uint blockSize) {
|
public BackupDescriptor? FindDescriptor(uint blockSize) {
|
||||||
@ -250,13 +342,16 @@ public class Application {
|
|||||||
|
|
||||||
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
|
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
|
|
||||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
|
|
||||||
return null;
|
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("Restoring files to directory: " + restoreDirectoryPath);
|
||||||
Console.WriteLine("Block Size: " + descriptor.BlockSize);
|
Console.WriteLine("Block Size: " + descriptor.BlockSize);
|
||||||
|
|
||||||
@ -269,8 +364,6 @@ public class Application {
|
|||||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
|
|
||||||
handler.WaitForTapeReady();
|
|
||||||
|
|
||||||
foreach (var file in descriptor.Files) {
|
foreach (var file in descriptor.Files) {
|
||||||
// Set position to the start block of the file
|
// Set position to the start block of the file
|
||||||
handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock);
|
handler.SetPosition(TapeDeviceHandler.TAPE_ABSOLUTE_BLOCK, 0, file.StartBlock);
|
||||||
@ -315,6 +408,7 @@ public class Application {
|
|||||||
|
|
||||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||||
Thread.Sleep(2000);
|
Thread.Sleep(2000);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public int CheckMediaSize(string ltoGen) {
|
public int CheckMediaSize(string ltoGen) {
|
||||||
@ -351,7 +445,7 @@ public class Application {
|
|||||||
Console.WriteLine("\nSelect a backup to perform:");
|
Console.WriteLine("\nSelect a backup to perform:");
|
||||||
for (int i = 0; i < _configuration.Backups.Count; i++) {
|
for (int i = 0; i < _configuration.Backups.Count; i++) {
|
||||||
var backupInt = _configuration.Backups[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): ");
|
Console.Write("Enter your choice (or '0' to go back): ");
|
||||||
@ -392,7 +486,7 @@ public class Application {
|
|||||||
Console.WriteLine("\nSelect a backup to restore:");
|
Console.WriteLine("\nSelect a backup to restore:");
|
||||||
for (int i = 0; i < _configuration.Backups.Count; i++) {
|
for (int i = 0; i < _configuration.Backups.Count; i++) {
|
||||||
var backupInt = _configuration.Backups[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): ");
|
Console.Write("Enter your choice (or '0' to go back): ");
|
||||||
|
|||||||
@ -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 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; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
public required string TapePath { get; set; }
|
public required string TapePath { get; set; }
|
||||||
|
public required int WriteDelay { get; set; }
|
||||||
public required List<BackupItem> Backups { get; set; }
|
public required List<BackupItem> Backups { get; set; }
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,8 +18,10 @@ class Program {
|
|||||||
Console.WriteLine("2. Backup");
|
Console.WriteLine("2. Backup");
|
||||||
Console.WriteLine("3. Restore");
|
Console.WriteLine("3. Restore");
|
||||||
Console.WriteLine("4. Eject tape");
|
Console.WriteLine("4. Eject tape");
|
||||||
Console.WriteLine("5. Reload configurations");
|
Console.WriteLine("5. Get device status");
|
||||||
Console.WriteLine("6. Exit");
|
Console.WriteLine("6. Tape Erase (Short)");
|
||||||
|
Console.WriteLine("7. Reload configurations");
|
||||||
|
Console.WriteLine("8. Exit");
|
||||||
Console.Write("Enter your choice: ");
|
Console.Write("Enter your choice: ");
|
||||||
|
|
||||||
var choice = Console.ReadLine();
|
var choice = Console.ReadLine();
|
||||||
@ -38,11 +40,16 @@ class Program {
|
|||||||
case "4":
|
case "4":
|
||||||
app.EjectTape();
|
app.EjectTape();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "5":
|
case "5":
|
||||||
app.LoadConfiguration();
|
app.GetDeviceStatus();
|
||||||
break;
|
break;
|
||||||
case "6":
|
case "6":
|
||||||
|
app.TapeErase();
|
||||||
|
break;
|
||||||
|
case "7":
|
||||||
|
app.LoadConfiguration();
|
||||||
|
break;
|
||||||
|
case "8":
|
||||||
Console.WriteLine("Exiting...");
|
Console.WriteLine("Exiting...");
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
|
|||||||
@ -1,19 +1,41 @@
|
|||||||
{
|
{
|
||||||
"TapePath": "\\\\.\\Tape0",
|
"TapePath": "\\\\.\\Tape0",
|
||||||
|
"WriteDelay": 100,
|
||||||
"Backups": [
|
"Backups": [
|
||||||
{
|
{
|
||||||
"Name": "Test",
|
"Name": "Normal test",
|
||||||
"Barcode": "",
|
"Barcode": "",
|
||||||
"Source": "F:\\LTO\\Backup",
|
"LTOGen": "LTO5",
|
||||||
"Destination": "F:\\LTO\\Restore",
|
"Source": {
|
||||||
"LTOGen": "LTO5"
|
"LocalPath": {
|
||||||
|
"Path": "F:\\LTO\\Backup"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"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": "\\\\nassrv0002.corp.maks-it.com\\data-1\\Users",
|
||||||
|
"PasswordCredentials": {
|
||||||
|
"Username": "",
|
||||||
|
"Password": ""
|
||||||
|
},
|
||||||
|
"Protocol": "SMB"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Destination": {
|
||||||
|
"LocalPath": {
|
||||||
|
"Path": "F:\\LTO\\Restore"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
37
src/MaksIT.LTO.Core/DriverManager.cs
Normal file
37
src/MaksIT.LTO.Core/DriverManager.cs
Normal 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}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
102
src/MaksIT.LTO.Core/NetworkConnection.cs
Normal file
102
src/MaksIT.LTO.Core/NetworkConnection.cs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -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);
|
IntPtr unmanagedPointer = Marshal.AllocHGlobal(data.Length);
|
||||||
try {
|
try {
|
||||||
Marshal.Copy(data, 0, unmanagedPointer, data.Length);
|
Marshal.Copy(data, 0, unmanagedPointer, data.Length);
|
||||||
@ -94,11 +94,14 @@ public partial class TapeDeviceHandler : IDisposable {
|
|||||||
else {
|
else {
|
||||||
int error = Marshal.GetLastWin32Error();
|
int error = Marshal.GetLastWin32Error();
|
||||||
Console.WriteLine($"Write Data: Failed with error code {error}");
|
Console.WriteLine($"Write Data: Failed with error code {error}");
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
Marshal.FreeHGlobal(unmanagedPointer);
|
Marshal.FreeHGlobal(unmanagedPointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] ReadData(uint length) {
|
public byte[] ReadData(uint length) {
|
||||||
@ -151,10 +154,8 @@ public partial class TapeDeviceHandler : IDisposable {
|
|||||||
if (errorCode == 0) // Assuming 0 means success/ready
|
if (errorCode == 0) // Assuming 0 means success/ready
|
||||||
{
|
{
|
||||||
isReady = true;
|
isReady = true;
|
||||||
Console.WriteLine("Tape is ready.");
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Console.WriteLine($"Tape not ready, status code: {errorCode}. Retrying...");
|
|
||||||
Thread.Sleep(1000); // Wait 1 second before checking again
|
Thread.Sleep(1000); // Wait 1 second before checking again
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -325,7 +325,7 @@ public partial class TapeDeviceHandler : IDisposable {
|
|||||||
/// Erase the tape
|
/// Erase the tape
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="type">The type of erase operation. Valid values are <see cref="TAPE_ERASE_SHORT"/> and <see cref="TAPE_ERASE_LONG"/>.</param>
|
/// <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 {
|
TAPE_ERASE erase = new TAPE_ERASE {
|
||||||
Type = type,
|
Type = type,
|
||||||
Immediate = 0
|
Immediate = 0
|
||||||
@ -343,11 +343,14 @@ public partial class TapeDeviceHandler : IDisposable {
|
|||||||
else {
|
else {
|
||||||
int error = Marshal.GetLastWin32Error();
|
int error = Marshal.GetLastWin32Error();
|
||||||
Console.WriteLine($"Erase Tape: Failed with error code {error}");
|
Console.WriteLine($"Erase Tape: Failed with error code {error}");
|
||||||
|
return error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
Marshal.FreeHGlobal(inBuffer);
|
Marshal.FreeHGlobal(inBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user