🐛 备份时忽略权限不足的文件

This commit is contained in:
li-chx 2025-11-14 16:05:33 +08:00
parent d80b60af17
commit f2001e2338
1 changed files with 16 additions and 7 deletions

View File

@ -111,7 +111,8 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
{
// 构建 tar 命令参数
// tar -czf output.tar.gz -C /parent1 folder1 -C /parent2 folder2
var args = new StringBuilder($"-czf {targetFile}");
// --ignore-failed-read: 忽略无法读取的文件,继续备份
var args = new StringBuilder($"-czf {targetFile} --ignore-failed-read");
foreach (var sourceDir in sourceDirs)
{
@ -143,10 +144,18 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
await stderrTask;
await proc.WaitForExitAsync(ct);
if (proc.ExitCode != 0)
// tar 返回码说明:
// 0 = 成功
// 1 = 一些文件有问题,但备份继续(使用 --ignore-failed-read 时)
// 2 = 致命错误
if (proc.ExitCode > 1)
{
throw new InvalidOperationException($"tar exited with code {proc.ExitCode}");
}
else if (proc.ExitCode == 1)
{
_logger.LogWarning("tar completed with warnings (exit code 1), some files may have been skipped");
}
}
private async Task ConsumeStreamToLoggerAsync(StreamReader err, ILogger log, CancellationToken ct)