Skip to content

Commit 57db3ad

Browse files
Merge pull request dataplat#2729 from sqlcollaborative/olafix
Now does ola properly, and will ignore logs
2 parents 932203e + 5545729 commit 57db3ad

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

functions/Get-DbaBackupInformation.ps1

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ function Get-DbaBackupInformation {
5353
.PARAMETER PassThru
5454
When data is exported the cmdlet will return no other output, this switch means it will also return the normal output which can be then piped into another command
5555
56+
.PARAMETER MaintenanceSolution
57+
This switch tells the function that the folder is the root of a Ola Hallengren backup folder
58+
59+
.PARAMETER IgnoreLogBackup
60+
This switch only works with MaintenanceSoltion, as we can then now that all file in LOG are to be ignored.
61+
5662
.PARAMETER Import
5763
When specified along with a path the command will import a previously exported
5864
@@ -89,12 +95,20 @@ function Get-DbaBackupInformation {
8995
This lets you keep a record of all backup history from the last month on hand to speed up refreshes
9096
9197
.EXAMPLE
92-
$Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backupps
98+
$Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backups
9399
$Backups += Get-DbaBackupInformation -SqlInstance Server2 -NoXpDirTree -Path c:\backups
94100
95101
Scan the unc folder \\network\backups with Server1, and then scan the C:\backups folder on
96102
Server2 not using xp_dirtree, adding the results to the first set.
103+
104+
.EXAMPLE
105+
$Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backups -MaintenanceSolution
97106
107+
When MaintenanceSolution is indicated we know we are dealing with the output from Ola Hallengren's backup scripts. So we make sure that a FULL folder exists in the first level of Path, if not we shortcut scanning all the files as we have nothing to work with
108+
.EXAMPLE
109+
$Backups = Get-DbaBackupInformation -SqlInstance Server1 -Path \\network\backups -MaintenanceSolution -IgnoreLogBackup
110+
111+
As we know we are dealing with an Ola Hallengren style backup folder from the MaintenanceSolution switch, when IgnoreLogBackup is also included we can ignore the LOG folder to skip any scanning of log backups. Note this also means then WON'T be restored
98112
#>
99113
[CmdletBinding( DefaultParameterSetName="Create")]
100114
param (
@@ -112,6 +126,8 @@ function Get-DbaBackupInformation {
112126
[parameter(ParameterSetName="Create")]
113127
[switch]$DirectoryRecurse,
114128
[switch]$EnableException,
129+
[switch]$MaintenanceSolution,
130+
[switch]$IgnoreLogBackup,
115131
[string]$ExportPath,
116132
[parameter(ParameterSetName="Import")]
117133
[switch]$Import,
@@ -153,6 +169,14 @@ function Get-DbaBackupInformation {
153169
return
154170
}
155171
}
172+
173+
if($true -eq $MaintenanceSolution){
174+
$NoXpDirTree = $True
175+
}
176+
177+
if ($true -eq $IgnoreLogBackup -and $true -ne $MaintenanceSolution){
178+
Write-Message -Message "IgnoreLogBackup can only by used with Maintenance Soultion. Will not be used" -Level Warning
179+
}
156180
}
157181
process {
158182
if (Test-FunctionInterrupt) { return }
@@ -195,15 +219,30 @@ function Get-DbaBackupInformation {
195219
ForEach ($f in $path) {
196220
Write-Message -Level VeryVerbose -Message "Not using sql for $f"
197221
if ($f -is [System.IO.FileSystemInfo]){
198-
if ($f.PsIsContainer -eq $true){
222+
if ($f.PsIsContainer -eq $true -and $true -ne $MaintenanceSolution){
199223
Write-Message -Level VeryVerbose -Message "folder $($f.fullname)"
200224
$Files = Get-ChildItem -Path $f.fullname -File -Recurse:$DirectoryRecurse
201225
}
226+
elseif ($f.PsIsContainer -eq $true -and $true -eq $MaintenanceSolution){
227+
$Files += Get-OlaHRestoreFile -Path $f.fullname -IgnoreLogBackup:$IgnoreLogBackup
228+
}
229+
elseif ($true -eq $MaintenanceSolution){
230+
$Files += Get-OlaHRestoreFile -Path $f.fullname -IgnoreLogBackup:$IgnoreLogBackup
231+
}
202232
else {
203233
Write-Message -Level VeryVerbose -Message "File"
204234
$Files += $f.fullname
205235
}
206236
}
237+
else{
238+
if ($true -eq $MaintenanceSolution){
239+
$Files += Get-OlaHRestoreFile -Path $f -IgnoreLogBackup:$IgnoreLogBackup
240+
}
241+
else {
242+
Write-Message -Level VeryVerbose -Message "File"
243+
$Files += $f
244+
}
245+
}
207246
}
208247
}
209248

functions/Restore-DbaDatabase.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ function Restore-DbaDatabase {
493493
if ($f -is [System.IO.FileSystemInfo]){
494494
$f = $f.fullname
495495
}
496-
$BackupHistory += $f | Get-DbaBackupInformation -SqlInstance $RestoreInstance -DirectoryRecurse:$DirectoryRecurse
496+
$BackupHistory += $f | Get-DbaBackupInformation -SqlInstance $RestoreInstance -DirectoryRecurse:$DirectoryRecurse -MaintenanceSolution:$MaintenanceSolutionBackup -IgnoreLogBackup:$IgnoreLogBackup
497497
}
498498
}
499499

tests/Get-DbaBackupInformation.Tests.ps1

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,28 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
2727
$db2 = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname2
2828
$db2 | Backup-DbaDatabase -Type Full -BackupDirectory $DestBackupDir
2929
$db2 | Backup-DbaDatabase -Type Differential -BackupDirectory $DestBackupDir
30-
$db2 | Backup-DbaDatabase -Type Log -BackupDirectory $DestBackupDir
30+
$db2 | Backup-DbaDatabase -Type Log -BackupDirectory $DestBackupDir
3131

32+
$DestBackupDirOla = 'C:\Temp\GetBackupsOla'
33+
if (-Not(Test-Path $DestBackupDirOla)) {
34+
New-Item -Type Container -Path $DestBackupDirOla
35+
New-Item -Type Container -Path $DestBackupDirOla\FULL
36+
New-Item -Type Container -Path $DestBackupDirOla\DIFF
37+
New-Item -Type Container -Path $DestBackupDirOla\LOG
38+
}
39+
else {
40+
Remove-Item $DestBackupDirOla\FULL\*
41+
Remove-Item $DestBackupDirOla\DIFF\*
42+
Remove-Item $DestBackupDirOla\LOG\*
43+
}
44+
45+
$dbname3 = "dbatoolsci_BackuphistoryOla_$random"
46+
$null = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname3 | Remove-DbaDatabase -Confirm:$false
47+
$null = Restore-DbaDatabase -SqlInstance $script:instance1 -Path $script:appveyorlabrepo\singlerestore\singlerestore.bak -DatabaseName $dbname3 -DestinationFilePrefix $dbname3
48+
$db3 = Get-DbaDatabase -SqlInstance $script:instance1 -Database $dbname3
49+
$db3 | Backup-DbaDatabase -Type Full -BackupDirectory "$DestBackupDirOla\FULL"
50+
$db3 | Backup-DbaDatabase -Type Differential -BackupDirectory "$DestBackupDirOla\Diff"
51+
$db3 | Backup-DbaDatabase -Type Log -BackupDirectory "$DestBackupDirOla\LOG"
3252
}
3353

3454
AfterAll {
@@ -74,4 +94,38 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
7494
}
7595
}
7696

97+
Context "Test Maintenance solution options" {
98+
$results = Get-DbaBackupInformation -SqlInstance $script:instance1 -Path $DestBackupDirOla -MaintenanceSolution
99+
It "Should be 3 backups returned" {
100+
$results.count | Should Be 3
101+
}
102+
It "Should Be 1 full backup" {
103+
($results | Where-Object {$_.Type -eq 'Database'}).count | Should be 1
104+
}
105+
It "Should be 1 log backups" {
106+
($results | Where-Object {$_.Type -eq 'Transaction Log'}).count | Should be 1
107+
}
108+
It "Should only be backups of $dbname3"{
109+
($results | Where-Object {$_.Database -ne $dbname3 }).count | Should Be 0
110+
}
111+
$ResultsSanLog = Get-DbaBackupInformation -SqlInstance $script:instance1 -Path $DestBackupDirOla -MaintenanceSolution -IgnoreLogBackup
112+
It "Should be 2 backups returned" {
113+
$ResultsSanLog.count | Should Be 2
114+
}
115+
It "Should Be 1 full backup" {
116+
($ResultsSanLog | Where-Object {$_.Type -eq 'Database'}).count | Should be 1
117+
}
118+
It "Should be 0 log backups" {
119+
($resultsSanLog | Where-Object {$_.Type -eq 'Transaction Log'}).count | Should be 0
120+
}
121+
$ResultsSanLog = Get-DbaBackupInformation -SqlInstance $script:instance1 -Path $DestBackupDirOla -IgnoreLogBackup -WarningVariable warnvar -WarningAction SilentlyContinue
122+
It "Should Warn if IgnoreLogBackup without Maintenance Solution" {
123+
($WarnVar -match "IgnoreLogBackup can only by used with Maintenance Soultion. Will not be used") | Should Be $True
124+
}
125+
It "Should ignore IgnoreLogBackup and return 3 backups" {
126+
$resultsSanLog.count | Should Be 3
127+
}
128+
129+
}
130+
77131
}

0 commit comments

Comments
 (0)