Merge branch 'dev' of https://git.maks-it.com/MAKS-IT/maksit-lto-backup into dev
This commit is contained in:
commit
0643209039
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:
|
||||
```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",
|
||||
"TapePath": "\\\\.\\Tape0",
|
||||
"WriteDelay": 100,
|
||||
"Backups": [
|
||||
{
|
||||
"Name": "BackupName",
|
||||
"Barcode": "123456",
|
||||
"Source": "path/to/source",
|
||||
"Destination": "path/to/destination",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -42,7 +42,7 @@ public class Application {
|
||||
|
||||
public void LoadTape(TapeDeviceHandler handler) {
|
||||
handler.Prepare(TapeDeviceHandler.TAPE_LOAD);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
Console.WriteLine("Tape loaded.");
|
||||
}
|
||||
@ -54,11 +54,41 @@ public class Application {
|
||||
|
||||
public void EjectTape(TapeDeviceHandler handler) {
|
||||
handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
Console.WriteLine("Tape ejected.");
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
@ -148,9 +178,14 @@ public class Application {
|
||||
Console.WriteLine($"Writing {blocks} zero-filled blocks to tape.");
|
||||
Console.WriteLine($"Block Size: {blockSize}.");
|
||||
|
||||
var writeError = 0;
|
||||
|
||||
for (int i = 0; i < blocks; i++) {
|
||||
handler.WriteData(new byte[blockSize]);
|
||||
handler.WaitForTapeReady();
|
||||
writeError = handler.WriteData(new byte[blockSize]);
|
||||
if (writeError != 0)
|
||||
return;
|
||||
|
||||
Thread.Sleep(_configuration.WriteDelay);
|
||||
}
|
||||
}
|
||||
|
||||
@ -166,13 +201,13 @@ public class Application {
|
||||
handler.SetMediaParams(blockSize);
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.Prepare(TapeDeviceHandler.TAPE_TENSION);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.Prepare(TapeDeviceHandler.TAPE_LOCK);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.WaitForTapeReady();
|
||||
|
||||
@ -186,6 +221,9 @@ public class Application {
|
||||
|
||||
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);
|
||||
@ -199,15 +237,22 @@ public class Application {
|
||||
// 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);
|
||||
|
||||
writeError = handler.WriteData(buffer);
|
||||
if (writeError != 0) {
|
||||
Console.WriteLine($"Failed to write file: {filePath}");
|
||||
return;
|
||||
}
|
||||
|
||||
currentTapeBlock++;
|
||||
handler.WaitForTapeReady();
|
||||
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);
|
||||
@ -217,18 +262,23 @@ public class Application {
|
||||
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);
|
||||
|
||||
writeError = handler.WriteData(block);
|
||||
if (writeError != 0)
|
||||
return;
|
||||
|
||||
currentTapeBlock++;
|
||||
handler.WaitForTapeReady();
|
||||
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);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
});
|
||||
}
|
||||
@ -244,10 +294,10 @@ public class Application {
|
||||
handler.SetMediaParams(blockSize);
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_SPACE_FILEMARKS, 0, 1);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.WaitForTapeReady();
|
||||
|
||||
@ -291,9 +341,10 @@ public class Application {
|
||||
|
||||
|
||||
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
return null;
|
||||
}
|
||||
@ -311,14 +362,12 @@ public class Application {
|
||||
handler.SetMediaParams(descriptor.BlockSize);
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
handler.WaitForTapeReady();
|
||||
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
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.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
|
||||
var filePath = Path.Combine(restoreDirectoryPath, file.FilePath);
|
||||
var directoryPath = Path.GetDirectoryName(filePath);
|
||||
@ -358,7 +407,7 @@ public class Application {
|
||||
}
|
||||
|
||||
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
|
||||
handler.WaitForTapeReady();
|
||||
Thread.Sleep(2000);
|
||||
});
|
||||
}
|
||||
|
||||
@ -396,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): ");
|
||||
@ -437,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): ");
|
||||
|
||||
@ -12,7 +12,7 @@ public class LocalPath : PathBase {
|
||||
|
||||
public class PasswordCredentials {
|
||||
public required string Username { get; set; }
|
||||
public required SecureString Password { get; set; }
|
||||
public required string Password { get; set; }
|
||||
}
|
||||
|
||||
public class RemotePath : PathBase {
|
||||
@ -35,5 +35,6 @@ public class BackupItem {
|
||||
|
||||
public class Configuration {
|
||||
public required string TapePath { get; set; }
|
||||
public required int WriteDelay { get; set; }
|
||||
public required List<BackupItem> Backups { get; set; }
|
||||
}
|
||||
|
||||
@ -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:
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
{
|
||||
"TapePath": "\\\\.\\Tape0",
|
||||
"WriteDelay": 100,
|
||||
"Backups": [
|
||||
{
|
||||
"Name": "Normal test",
|
||||
@ -7,7 +8,7 @@
|
||||
"LTOGen": "LTO5",
|
||||
"Source": {
|
||||
"LocalPath": {
|
||||
"Path": "D:\\Drivers"
|
||||
"Path": "F:\\LTO\\Backup"
|
||||
}
|
||||
},
|
||||
"Destination": {
|
||||
@ -22,14 +23,16 @@
|
||||
"LTOGen": "LTO5",
|
||||
"Source": {
|
||||
"RemotePath": {
|
||||
"Path": "\\\\nasrv0001.corp.maks-it.com\\LTO\\Backup",
|
||||
"Credentials": {
|
||||
"Username": "user",
|
||||
"Password": "password"
|
||||
}
|
||||
"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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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) {
|
||||
|
||||
@ -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>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user