Skip to content

Commit

Permalink
Loop over included DBs for initialization if available
Browse files Browse the repository at this point in the history
Loop over included database list if specified instead of trying to get a list of DBs by enumerating folders.   This is the easier and more reliable option if it's available.  It would work with paths like this where we have an AG folder per DB (e.g. basic availability groups)
\\SERVER\BACKUPS\FULL\{DatabaseName}_AG\{DatabaseName}

Fix order by for disk based initialization.
  • Loading branch information
DavidWiseman committed Jun 29, 2023
1 parent 1e7eb9e commit cad2ddb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
22 changes: 16 additions & 6 deletions DatabaseInitializerFromAzBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,22 @@ protected override void PollForNewDBs()
var dbRoot = Config.FullBackupPathTemplate[..Config.FullBackupPathTemplate.IndexOf(Config.DatabaseToken, StringComparison.OrdinalIgnoreCase)];
Log.Debug("Az DB Path : {path}", dbRoot);

var folders = GetFoldersForAzBlob(dbRoot);
Log.Debug("Folders: {count}", folders.Count);
Parallel.ForEach(folders,
new ParallelOptions() { MaxDegreeOfParallelism = Config.MaxThreads },
ProcessDB
);
if (Config.IncludedDatabases.Count > 0)
{
Parallel.ForEach(Config.IncludedDatabases,
new ParallelOptions() { MaxDegreeOfParallelism = Config.MaxThreads },
ProcessDB
);
}
else
{
var folders = GetFoldersForAzBlob(dbRoot);
Log.Debug("Folders: {count}", folders.Count);
Parallel.ForEach(folders,
new ParallelOptions() { MaxDegreeOfParallelism = Config.MaxThreads },
ProcessDB
);
}
}

protected override void DoProcessDB(string db)
Expand Down
31 changes: 21 additions & 10 deletions DatabaseInitializerFromDisk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,27 @@ public override bool IsValidated
protected override void PollForNewDBs()
{
if (string.IsNullOrEmpty(Config.FullBackupPathTemplate)) return;

var dbRoot = Config.FullBackupPathTemplate[..Config.FullBackupPathTemplate.IndexOf(Config.DatabaseToken, StringComparison.OrdinalIgnoreCase)];

Parallel.ForEach(System.IO.Directory.EnumerateDirectories(dbRoot),
new ParallelOptions() { MaxDegreeOfParallelism = Config.MaxThreads },
dbFolder =>
{
var db = Path.GetFileName(dbFolder);
ProcessDB(db);
});
var dbRoot = Config.FullBackupPathTemplate[
..Config.FullBackupPathTemplate.IndexOf(Config.DatabaseToken, StringComparison.OrdinalIgnoreCase)];

if (Config.IncludedDatabases.Count > 0)
{
Parallel.ForEach(Config.IncludedDatabases,
new ParallelOptions() { MaxDegreeOfParallelism = Config.MaxThreads },
ProcessDB);
}
else
{

Parallel.ForEach(System.IO.Directory.EnumerateDirectories(dbRoot),
new ParallelOptions() { MaxDegreeOfParallelism = Config.MaxThreads },
dbFolder =>
{
var db = Path.GetFileName(dbFolder);
ProcessDB(db);
});
}
}

protected override void DoProcessDB(string db)
Expand Down Expand Up @@ -92,7 +103,7 @@ public static List<string> GetFilesForLastBackup(string folder)

var files = directory.GetFiles("*.bak")
.Where(f => f.LastWriteTimeUtc >= DateTime.UtcNow.AddDays(-Config.MaxBackupAgeForInitialization))
.OrderBy(f => f.LastWriteTimeUtc)
.OrderByDescending(f => f.LastWriteTimeUtc)
.ToList();

FileInfo? previousFile = null;
Expand Down

0 comments on commit cad2ddb

Please sign in to comment.