1
0
mirror of https://github.com/chylex/Minecraft-Phantom-Panel.git synced 2025-06-01 19:34:03 +02:00

Change backup world copying to use the dedicated temporary folder

This commit is contained in:
chylex 2023-02-05 11:24:43 +01:00
parent c7354dce0e
commit b3104f9ac3
Signed by: chylex
GPG Key ID: 4DE42C8F19A80548
2 changed files with 24 additions and 15 deletions
Agent/Phantom.Agent.Services/Backups

View File

@ -10,11 +10,15 @@ using Serilog;
namespace Phantom.Agent.Services.Backups; namespace Phantom.Agent.Services.Backups;
sealed class BackupArchiver { sealed class BackupArchiver {
private readonly string destinationBasePath;
private readonly string temporaryBasePath;
private readonly ILogger logger; private readonly ILogger logger;
private readonly InstanceProperties instanceProperties; private readonly InstanceProperties instanceProperties;
private readonly CancellationToken cancellationToken; private readonly CancellationToken cancellationToken;
public BackupArchiver(string loggerName, InstanceProperties instanceProperties, CancellationToken cancellationToken) { public BackupArchiver(string destinationBasePath, string temporaryBasePath, string loggerName, InstanceProperties instanceProperties, CancellationToken cancellationToken) {
this.destinationBasePath = destinationBasePath;
this.temporaryBasePath = temporaryBasePath;
this.logger = PhantomLogger.Create<BackupArchiver>(loggerName); this.logger = PhantomLogger.Create<BackupArchiver>(loggerName);
this.instanceProperties = instanceProperties; this.instanceProperties = instanceProperties;
this.cancellationToken = cancellationToken; this.cancellationToken = cancellationToken;
@ -40,9 +44,11 @@ sealed class BackupArchiver {
return false; return false;
} }
public async Task ArchiveWorld(string destinationPath, BackupCreationResult.Builder resultBuilder) { public async Task ArchiveWorld(BackupCreationResult.Builder resultBuilder) {
string backupFolderPath = Path.Combine(destinationPath, instanceProperties.InstanceGuid.ToString()); string guid = instanceProperties.InstanceGuid.ToString();
string backupFilePath = Path.Combine(backupFolderPath, DateTime.Now.ToString("yyyyMMdd-HHmmss") + ".tar"); string currentDateTime = DateTime.Now.ToString("yyyyMMdd-HHmmss");
string backupFolderPath = Path.Combine(destinationBasePath, guid);
string backupFilePath = Path.Combine(backupFolderPath, currentDateTime + ".tar");
if (File.Exists(backupFilePath)) { if (File.Exists(backupFilePath)) {
resultBuilder.Kind = BackupCreationResultKind.BackupFileAlreadyExists; resultBuilder.Kind = BackupCreationResultKind.BackupFileAlreadyExists;
@ -58,7 +64,8 @@ sealed class BackupArchiver {
return; return;
} }
if (!await CopyWorldAndCreateTarArchive(backupFolderPath, backupFilePath, resultBuilder)) { string temporaryFolderPath = Path.Combine(temporaryBasePath, guid + "_" + currentDateTime);
if (!await CopyWorldAndCreateTarArchive(temporaryFolderPath, backupFilePath, resultBuilder)) {
return; return;
} }
@ -68,9 +75,7 @@ sealed class BackupArchiver {
} }
} }
private async Task<bool> CopyWorldAndCreateTarArchive(string backupFolderPath, string backupFilePath, BackupCreationResult.Builder resultBuilder) { private async Task<bool> CopyWorldAndCreateTarArchive(string temporaryFolderPath, string backupFilePath, BackupCreationResult.Builder resultBuilder) {
string temporaryFolderPath = Path.Combine(backupFolderPath, "temp");
try { try {
if (!await CopyWorldToTemporaryFolder(temporaryFolderPath)) { if (!await CopyWorldToTemporaryFolder(temporaryFolderPath)) {
resultBuilder.Kind = BackupCreationResultKind.CouldNotCopyWorldToTemporaryFolder; resultBuilder.Kind = BackupCreationResultKind.CouldNotCopyWorldToTemporaryFolder;

View File

@ -8,10 +8,12 @@ using Serilog;
namespace Phantom.Agent.Services.Backups; namespace Phantom.Agent.Services.Backups;
sealed partial class BackupManager { sealed partial class BackupManager {
private readonly string basePath; private readonly string destinationBasePath;
private readonly string temporaryBasePath;
public BackupManager(AgentFolders agentFolders) { public BackupManager(AgentFolders agentFolders) {
this.basePath = agentFolders.BackupsFolderPath; this.destinationBasePath = agentFolders.BackupsFolderPath;
this.temporaryBasePath = Path.Combine(agentFolders.TemporaryFolderPath, "backups");
} }
public async Task<BackupCreationResult> CreateBackup(string loggerName, InstanceSession session, CancellationToken cancellationToken) { public async Task<BackupCreationResult> CreateBackup(string loggerName, InstanceSession session, CancellationToken cancellationToken) {
@ -26,22 +28,24 @@ sealed partial class BackupManager {
} }
try { try {
return await new BackupCreator(basePath, loggerName, session, cancellationToken).CreateBackup(); return await new BackupCreator(destinationBasePath, temporaryBasePath, loggerName, session, cancellationToken).CreateBackup();
} finally { } finally {
session.BackupSemaphore.Release(); session.BackupSemaphore.Release();
} }
} }
private sealed class BackupCreator { private sealed class BackupCreator {
private readonly string basePath; private readonly string destinationBasePath;
private readonly string temporaryBasePath;
private readonly string loggerName; private readonly string loggerName;
private readonly ILogger logger; private readonly ILogger logger;
private readonly InstanceSession session; private readonly InstanceSession session;
private readonly BackupCommandListener listener; private readonly BackupCommandListener listener;
private readonly CancellationToken cancellationToken; private readonly CancellationToken cancellationToken;
public BackupCreator(string basePath, string loggerName, InstanceSession session, CancellationToken cancellationToken) { public BackupCreator(string destinationBasePath, string temporaryBasePath, string loggerName, InstanceSession session, CancellationToken cancellationToken) {
this.basePath = basePath; this.destinationBasePath = destinationBasePath;
this.temporaryBasePath = temporaryBasePath;
this.loggerName = loggerName; this.loggerName = loggerName;
this.logger = PhantomLogger.Create<BackupManager>(loggerName); this.logger = PhantomLogger.Create<BackupManager>(loggerName);
this.session = session; this.session = session;
@ -81,7 +85,7 @@ sealed partial class BackupManager {
try { try {
await DisableAutomaticSaving(); await DisableAutomaticSaving();
await SaveAllChunks(); await SaveAllChunks();
await new BackupArchiver(loggerName, session.InstanceProperties, cancellationToken).ArchiveWorld(basePath, resultBuilder); await new BackupArchiver(destinationBasePath, temporaryBasePath, loggerName, session.InstanceProperties, cancellationToken).ArchiveWorld(resultBuilder);
} catch (OperationCanceledException) { } catch (OperationCanceledException) {
resultBuilder.Kind = BackupCreationResultKind.BackupCancelled; resultBuilder.Kind = BackupCreationResultKind.BackupCancelled;
logger.Warning("Backup creation was cancelled."); logger.Warning("Backup creation was cancelled.");