🐛 修复文件清理逻辑

This commit is contained in:
li_chx 2026-03-22 13:12:58 +08:00
parent f41bd03f03
commit 2fd4ca7e6a
Signed by: li_chx
GPG Key ID: 70D4985BB8180E92
1 changed files with 21 additions and 1 deletions

View File

@ -186,7 +186,6 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
await CompressDirectoryAsync([dumpDir, extraDir], finalDump, ct);
Directory.Delete(dumpDir, recursive: true);
_logger.LogInformation("Backup completed: {File}", finalDump);
_outputService.CleanOutputDirFiles();
@ -198,6 +197,27 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
_logger.LogError(ex, "Error during backup process.");
throw;
}
finally
{
try
{
if (Directory.Exists(dumpDir))
{
Directory.Delete(dumpDir, recursive: true);
_logger.LogInformation("Cleaned temporary dump directory: {Dir}", dumpDir);
}
if (File.Exists(finalDump))
{
File.Delete(finalDump);
_logger.LogInformation("Cleaned temporary archive file: {File}", finalDump);
}
}
catch (Exception cleanupEx)
{
_logger.LogWarning(cleanupEx, "Failed to clean temporary files in /data/dumps.");
}
}
}
private async Task CompressDirectoryAsync(string[] sourceDirs, string targetFile, CancellationToken ct)