-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackupRetention.cs
More file actions
29 lines (28 loc) · 1.3 KB
/
Copy pathBackupRetention.cs
File metadata and controls
29 lines (28 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
namespace DbBackupTool
{
// Every run gets its own timestamped subfolder (dump/log/option-file etc
// together), so retention counts whole backup runs, not loose files - a
// provider that produces several files (Oracle's .dmp+.log, Mongo's whole
// dump directory) can never desync from one that produces a single file
// (MySQL/Postgres's .sql/.dump). This is what makes pruning itself fully
// DB-independent: it never has to know what a "backup" looks like per engine.
public static class BackupRetention
{
public static void Prune(string backupFolder, string label, int retentionCount, JobLog log)
{
// Folder names are "{label}_{yyyyMMdd_HHmmss}" - that format sorts
// correctly as a plain string, so no timestamp parsing is needed here.
var dirs = new DirectoryInfo(backupFolder)
.GetDirectories($"{label}_*")
.OrderByDescending(d => d.Name)
.Skip(retentionCount)
.ToList();
foreach (var dir in dirs)
{
log.Info($"Pruning old backup: {dir.FullName}");
try { dir.Delete(recursive: true); }
catch (Exception ex) { log.Error($"Could not delete {dir.FullName}: {ex.Message}"); }
}
}
}
}