✨ 追加对普通文件的备份功能
This commit is contained in:
parent
8929a4f99a
commit
d80b60af17
|
|
@ -1,10 +1,11 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SQLBackupToCOS;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace SQLBackupToCOS;
|
||||
|
||||
public class BackupService(ILogger<BackupService> logger, IConfiguration config, COSService cosService) : BackgroundService
|
||||
{
|
||||
private readonly ILogger<BackupService> _logger = logger;
|
||||
|
|
@ -39,6 +40,7 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
|
|||
|
||||
private async Task RunOnceAsync(CancellationToken ct)
|
||||
{
|
||||
var extraDir = _config.GetValue<string>("extraDir") ?? "/needBackup";
|
||||
var url = _config.GetValue<string>("Database:Host") ?? "172.17.0.1";
|
||||
var user = _config.GetValue<string>("Database:User") ?? "backupUser";
|
||||
var password = _config.GetValue<string>("Database:Password") ?? "";
|
||||
|
|
@ -49,7 +51,6 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
|
|||
var timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
var dumpDir = $"/data/dumps/dump-{timestamp}";
|
||||
var finalDump = $"/data/dumps/dump-{timestamp}.tar.gz";
|
||||
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(dumpDir);
|
||||
|
|
@ -91,7 +92,8 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
|
|||
}
|
||||
|
||||
_logger.LogInformation("Compressing backup directory to {File}", finalDump);
|
||||
await CompressDirectoryAsync(dumpDir, finalDump, ct);
|
||||
|
||||
await CompressDirectoryAsync([dumpDir, extraDir], finalDump, ct);
|
||||
|
||||
Directory.Delete(dumpDir, recursive: true);
|
||||
_logger.LogInformation("Backup completed: {File}", finalDump);
|
||||
|
|
@ -105,18 +107,37 @@ public class BackupService(ILogger<BackupService> logger, IConfiguration config,
|
|||
}
|
||||
}
|
||||
|
||||
private async Task CompressDirectoryAsync(string sourceDir, string targetFile, CancellationToken ct)
|
||||
private async Task CompressDirectoryAsync(string[] sourceDirs, string targetFile, CancellationToken ct)
|
||||
{
|
||||
// 使用 tar + gzip 压缩目录
|
||||
// 构建 tar 命令参数
|
||||
// tar -czf output.tar.gz -C /parent1 folder1 -C /parent2 folder2
|
||||
var args = new StringBuilder($"-czf {targetFile}");
|
||||
|
||||
foreach (var sourceDir in sourceDirs)
|
||||
{
|
||||
if (Directory.Exists(sourceDir))
|
||||
{
|
||||
var parentDir = Path.GetDirectoryName(sourceDir);
|
||||
var folderName = Path.GetFileName(sourceDir);
|
||||
args.Append($" -C {parentDir} {folderName}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Directory not found, skipping: {Dir}", sourceDir);
|
||||
}
|
||||
}
|
||||
|
||||
var psi = new ProcessStartInfo
|
||||
{
|
||||
FileName = "tar",
|
||||
Arguments = $"-czf {targetFile} -C {Path.GetDirectoryName(sourceDir)} {Path.GetFileName(sourceDir)}",
|
||||
Arguments = args.ToString(),
|
||||
RedirectStandardError = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
};
|
||||
|
||||
_logger.LogInformation("Running tar command: tar {Args}", args.ToString());
|
||||
|
||||
using var proc = Process.Start(psi) ?? throw new InvalidOperationException("Failed to start tar");
|
||||
var stderrTask = ConsumeStreamToLoggerAsync(proc.StandardError, _logger, ct);
|
||||
await stderrTask;
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
}
|
||||
},
|
||||
"BackupIntervalMinutes": 60,
|
||||
"extraDir": "EXTRAPATH",
|
||||
"Database": {
|
||||
"Host": "172.17.0.1",
|
||||
"User": "SQLUSER",
|
||||
|
|
|
|||
|
|
@ -15,3 +15,6 @@ services:
|
|||
COS__SecretKey: SECRETKEY
|
||||
COS__FilePath: PATH
|
||||
BackupIntervalMinutes: 1440
|
||||
extraDir: EXTRAPATH
|
||||
volumes:
|
||||
- srcDir:EXTRAPATH/srcDir:ro
|
||||
|
|
|
|||
Loading…
Reference in New Issue