(feature): local backup and restore tested

This commit is contained in:
Maksym Sadovnychyy 2024-11-01 08:31:43 -07:00
parent b518360409
commit 95bf055a1c
4 changed files with 57 additions and 23 deletions

View File

@ -42,7 +42,7 @@ public class Application {
public void LoadTape(TapeDeviceHandler handler) { public void LoadTape(TapeDeviceHandler handler) {
handler.Prepare(TapeDeviceHandler.TAPE_LOAD); handler.Prepare(TapeDeviceHandler.TAPE_LOAD);
handler.WaitForTapeReady(); Thread.Sleep(2000);
Console.WriteLine("Tape loaded."); Console.WriteLine("Tape loaded.");
} }
@ -54,11 +54,36 @@ public class Application {
public void EjectTape(TapeDeviceHandler handler) { public void EjectTape(TapeDeviceHandler handler) {
handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD); handler.Prepare(TapeDeviceHandler.TAPE_UNLOAD);
handler.WaitForTapeReady(); Thread.Sleep(2000);
Console.WriteLine("Tape ejected."); 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() { public void GetDeviceStatus() {
using var handler = new TapeDeviceHandler(_tapePath); using var handler = new TapeDeviceHandler(_tapePath);
handler.GetStatus(); handler.GetStatus();
@ -176,13 +201,13 @@ public class Application {
handler.SetMediaParams(blockSize); handler.SetMediaParams(blockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.Prepare(TapeDeviceHandler.TAPE_TENSION); handler.Prepare(TapeDeviceHandler.TAPE_TENSION);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.Prepare(TapeDeviceHandler.TAPE_LOCK); handler.Prepare(TapeDeviceHandler.TAPE_LOCK);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.WaitForTapeReady(); handler.WaitForTapeReady();
@ -214,8 +239,10 @@ public class Application {
} }
writeError = handler.WriteData(buffer); writeError = handler.WriteData(buffer);
if (writeError != 0) if (writeError != 0) {
Console.WriteLine($"Failed to write file: {filePath}");
return; return;
}
currentTapeBlock++; currentTapeBlock++;
Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks Thread.Sleep(_configuration.WriteDelay); // Small delay between blocks
@ -248,9 +275,10 @@ public class Application {
ZeroFillBlocks(handler, 3, blockSize); ZeroFillBlocks(handler, 3, blockSize);
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK); handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
handler.WaitForTapeReady(); Thread.Sleep(2000);
}); });
} }
@ -266,10 +294,10 @@ public class Application {
handler.SetMediaParams(blockSize); handler.SetMediaParams(blockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_SPACE_FILEMARKS, 0, 1); handler.SetPosition(TapeDeviceHandler.TAPE_SPACE_FILEMARKS, 0, 1);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.WaitForTapeReady(); handler.WaitForTapeReady();
@ -313,9 +341,10 @@ public class Application {
handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK); handler.Prepare(TapeDeviceHandler.TAPE_UNLOCK);
handler.WaitForTapeReady(); Thread.Sleep(2000);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
handler.WaitForTapeReady(); Thread.Sleep(2000);
return null; return null;
} }
@ -333,14 +362,12 @@ public class Application {
handler.SetMediaParams(descriptor.BlockSize); handler.SetMediaParams(descriptor.BlockSize);
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
handler.WaitForTapeReady(); 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);
handler.WaitForTapeReady(); Thread.Sleep(2000);
var filePath = Path.Combine(restoreDirectoryPath, file.FilePath); var filePath = Path.Combine(restoreDirectoryPath, file.FilePath);
var directoryPath = Path.GetDirectoryName(filePath); var directoryPath = Path.GetDirectoryName(filePath);
@ -380,7 +407,7 @@ public class Application {
} }
handler.SetPosition(TapeDeviceHandler.TAPE_REWIND); handler.SetPosition(TapeDeviceHandler.TAPE_REWIND);
handler.WaitForTapeReady(); Thread.Sleep(2000);
}); });
} }

View File

@ -19,8 +19,9 @@ class Program {
Console.WriteLine("3. Restore"); Console.WriteLine("3. Restore");
Console.WriteLine("4. Eject tape"); Console.WriteLine("4. Eject tape");
Console.WriteLine("5. Get device status"); Console.WriteLine("5. Get device status");
Console.WriteLine("6. Reload configurations"); Console.WriteLine("6. Tape Erase (Short)");
Console.WriteLine("7. Exit"); 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();
@ -43,9 +44,12 @@ class Program {
app.GetDeviceStatus(); app.GetDeviceStatus();
break; break;
case "6": case "6":
app.LoadConfiguration(); app.TapeErase();
break; break;
case "7": case "7":
app.LoadConfiguration();
break;
case "8":
Console.WriteLine("Exiting..."); Console.WriteLine("Exiting...");
return; return;
default: default:

View File

@ -1,6 +1,6 @@
{ {
"TapePath": "\\\\.\\Tape0", "TapePath": "\\\\.\\Tape0",
"WriteDelay": 100, "WriteDelay": 1000,
"Backups": [ "Backups": [
{ {
"Name": "Normal test", "Name": "Normal test",
@ -8,7 +8,7 @@
"LTOGen": "LTO5", "LTOGen": "LTO5",
"Source": { "Source": {
"LocalPath": { "LocalPath": {
"Path": "F:\\LTO\\Backup" "Path": "D:\\Program Files"
} }
}, },
"Destination": { "Destination": {

View File

@ -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>