internal/Get-FilteredRestoreFile.ps1

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#ValidationTags#Messaging,FlowControl,Pipeline,CodeStyle#

function Get-FilteredRestoreFile {
    <#
        .SYNOPSIS
            Internal Function to Filter a set of SQL Server backup files
         
        .DESCRIPTION
            Takes an array of FileSystem Objects and then filters them down by date to get a potential Restore set
            First step is to pass them to a SQLInstance to be parsed with Read-DBABackupHeader
            The we find the last full backup before the RestorePoint.
            Then filter for and Diff backups between the full backup and the RestorePoint
            Tnen find the T-log backups needed to bridge the gap up until the RestorePoint
         
        .PARAMETER Files
            The files to filter
         
        .PARAMETER SqlInstance
            The Sql Instance from which the backup headers are retrieved for comparison.
         
        .PARAMETER RestoreTime
            The time constraint between backup time and up to when the transaction logs are retrieved.
         
        .PARAMETER SqlCredential
            The Password to connect to the Sql Instance with.
         
        .PARAMETER IgnoreLogBackup
            Whether the transaction logs should be ignored.
         
        .PARAMETER TrustDbBackupHistory
            Whether to trust the database backup history.
     
        .PARAMETER Silent
            Replaces user friendly yellow warnings with bloody red exceptions of doom!
            Use this if you want the function to throw terminating errors you want to catch.
         
        .PARAMETER Continue
            Continues restoring a database from a point in time
 
        .PARAMETER ContinuePoints
            The points to continue the restore from
 
        .PARAMETER DatabaseName
            Used when a restore is being continued with a name change
         
        .EXAMPLE
            Get-FilteredRestoreFile -Files $Files -SqlInstance $SqlInstance
     
            Filters out the backupfiles when compared to that sql instance's backup heades.
         
        .NOTES
            Additional information about the function.
    #>

    [CmdletBinding()]
    Param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [object[]]
        $Files,
        
        [Alias('SqlServer')]
        [parameter(Mandatory = $true)]


        [DbaInstanceParameter]
        $SqlInstance,
        
        [DateTime]
        $RestoreTime = (Get-Date).AddYears(1),
        
        [System.Management.Automation.PSCredential]
        $SqlCredential,
        
        [switch]
        $IgnoreLogBackup,
        
        [switch]
        $TrustDbBackupHistory,
        
        [switch]
        $Silent,

        [switch]
        $Continue,

        [object]
        $ContinuePoints,

        [string]
        $DatabaseName,

        [string]
        $AzureCredential
    )
    begin {
        Write-Message -Level InternalComment -Message 'Starting'
        Write-Message -Level Debug -Message "Bound parameters: $($PSBoundParameters.Keys -join ", ")"
        
        $sqlInstanceParameterSplat = @{
            SqlInstance = $SqlInstance
        }
        if ($SqlCredential) { $sqlInstanceParameterSplat["SqlCredential"] = $SqlCredential }
        
        $allSqlBackupDetails = @()
        $outResults = @()
        $internalFiles = @()
    }
    process {
        foreach ($file in $files) {
            $internalFiles += $file
        }
    }
    end {
        if ($TrustDbBackupHistory) {
            Write-Message -Level Verbose -Message "Processing trusted backup history"
            
            $tmpInternalFiles = @()
            foreach ($row in $internalFiles) {
                if ($row.FullName.Count -gt 1) {
                    foreach ($filename in $row.FullName) {
                        $newIF = $row | Select-Object *
                        $NewIf.fullName = $filename.ToString()
                        $NewIf.BackupPath = $filename.ToString()
                        $tmpInternalFiles += $NewIf
                    }
                }
                else {
                    $tmpInternalFiles += $row
                }
            }
            $internalFiles = $tmpInternalFiles
            
            $allSqlBackupDetails += $internalFiles | Where-Object { $_.Type -eq 'Full' } | select-object *, @{ Name = "BackupTypeDescription"; Expression = { "Database" } }, @{ Name = "BackupType"; Expression = { "1" } }
            $allSqlBackupDetails += $internalFiles | Where-Object { $_.Type -eq 'Log' } | select-object *, @{ Name = "BackupTypeDescription"; Expression = { "Transaction Log" } }, @{ Name = "BackupType"; Expression = { "2" } }
            $allSqlBackupDetails += $internalFiles | Where-Object { $_.Type -eq 'Differential' } | select-object *, @{ Name = "BackupTypeDescription"; Expression = { "Database Differential" } }, @{ Name = "BackupType"; Expression = { "5" } }
        }
        else {
            Write-Message -Level Verbose -Message "Read File headers (Read-DBABackupHeader)"        
            $AllSQLBackupdetails = $InternalFiles | ForEach-Object{if($_.fullname -ne $null){$_.Fullname}else{$_}} | Read-DBAbackupheader -sqlinstance $sqlinstance -SqlCredential $SqlCredential -AzureCredential $AzureCredential
        }

        Write-Message -Level Verbose -Message "$($AllSQLBackupdetails.count) Files to filter"
        $Databases = $AllSQLBackupdetails  | Group-Object -Property Servername, DatabaseName
        Write-Message -Level Verbose -Message "$(($Databases | Measure-Object).count) database to process"

        if ($Continue) {
            IF ($DatabaseName -ne '') {
                if (($Databases | Measure-Object).count -gt 1) {
                    Stop-Function -Message "More than 1 db restore for 1 db - exiting"
                    return
                } else {
                    $dbrec = $DatabaseName
                }
                
            }
            else {
                Write-Message -Level Verbose -Message "Continue set, so filtering to these databases :$($continuePoints.Database -join ',')"
                #$ignore = $Databases | Where-Object {$_.DatabaseName -notin ($continuePoints.Database)} | select-Object DatabaseName
                #Write-Verbose "Ignoring these: $($ignore -join ',')"
                $Databases = $Databases | Where-Object {(($_.Name -split ',')[1]).trim() -in ($continuePoints.Database)}
                
            }
        } 
        
        foreach ($Database in $Databases) {

            $Results = @()
            Write-Message -Level VeryVerbose -Message "Find Newest Full backup - $($_.DatabaseName)"

            $ServerName, $databaseName = $Database.Name.split(',').trim()

            Write-verbose "dbname = $databasename"
            $SQLBackupdetails = $AllSQLBackupdetails | Where-Object {$_.ServerName -eq $ServerName -and $_.DatabaseName -eq $DatabaseName.trim()}
            #If we're continuing a restore, then we aren't going to be needing a full backup....
            $TlogStartlsn = 0
            if (!($continue)) {
                $Fullbackup = $SQLBackupdetails | where-object {$_.BackupTypeDescription -eq 'Database' -and $_.BackupStartDate -lt $RestoreTime} | Sort-Object -Property BackupStartDate -descending | Select-Object -First 1
                $TlogStartlsn = $Fullbackup.FirstLSN
                if ($Fullbackup -eq $null) {
                    Stop-Function -Message "No Full backup found to anchor the restore" -Continue -Target $database
                    break
                }
                #This scans for striped full backups to build the results
                $Results += $SQLBackupdetails | where-object {$_.BackupTypeDescription -eq "Database" -and $_.FirstLSN -eq $FullBackup.FirstLSN -and $_.BackupSetGUID -eq $FullBackup.BackupSetGUID}
            }
            else {
                Write-Message -Level VeryVerbose -Message "Continuing restore, setting fake fullbackup"
                if ($null -ne $dbrec) {
                     $dbfilter = $dbrec
                 }
                 else {
                     $dbfilter= $DatabaseName
                 }
                $Fullbackup = $ContinuePoints | Where-Object {$_.Database -eq $dbfilter}
                $TLogStartLsn = $Fullbackup.redo_start_lsn
                
            }    
           Write-Message -Level Verbose -Message "Got a Full backup, now to find diffs if they exist"
            
            #Get latest Differential Backup
            #If we're doing a continue and the last restore wasn't a full db we can't use a diff, so skip
            if ($null -eq $lastrestore -or $lastrestore -eq 'Database'  ) {
                $DiffbackupsLSN = ($SQLBackupdetails | Where-Object {$_.BackupTypeDescription -eq 'Database Differential' -and $_.DatabaseBackupLSN -eq $Fullbackup.FirstLsn -and $_.BackupStartDate -lt $RestoreTime} | Sort-Object -Property BackupStartDate -descending | Select-Object -First 1).FirstLSN
                #Scan for striped differential backups
                $Diffbackups = $SqlBackupDetails | Where-Object {$_.BackupTypeDescription -eq 'Database Differential' -and $_.DatabaseBackupLSN -eq $Fullbackup.FirstLsn -and $_.FirstLSN -eq $DiffBackupsLSN}
                if ($null -ne $Diffbackups) {
                    Write-Message -Level Verbose -Message "we have at least one diff so look for tlogs after the last one"
                    #If we have a Diff backup, we only need T-log backups post that point
                    $TlogStartLSN = ($DiffBackups | select-object -Property LastLsn -first 1).LastLsn
                    $Results += $Diffbackups
                }
            }
            
            if ($fullBackup.RecoverModel -eq 'SIMPLE' -or $IgnoreLogBackup) {
                Write-Message -Level Verbose -Message "Database in simple mode or IgnoreLogBackup is true, skipping Transaction logs" -Target $database
            }
            else {
                write-verbose " frfID - $($Fullbackup.FirstRecoveryForkID)"
                write-verbose "tstart - $TlogStartLSN"
 
                Write-Message -Level Verbose -Message "Got a Full/Diff backups, now find all Tlogs needed"
                $AllTlogs = $SQLBackupdetails | Where-Object {$_.BackupTypeDescription -eq 'Transaction Log'} 
                $Filteredlogs = $SQLBackupdetails | Where-Object {$_.BackupTypeDescription -eq 'Transaction Log' -and $_.FirstRecoveryForkID -eq $Fullbackup.FirstRecoveryForkID -and $_.LastLSN -gt $TlogStartLSN -and $_.BackupStartDate -lt $RestoreTime}
                # -and $_.BackupStartDate -ge $LogStartDate
                $GroupedLogs = $FilteredLogs | Group-Object -Property LastLSN, FirstLSN
                $Tlogs = @()
                foreach ($LogGroup in $GroupedLogs) {
                    $Tlogs += $LogGroup.Group | Where-Object {$_.BackupSetGUID -eq ($LogGroup.Group | sort-Object -Property BackupStartDate -Descending | select-object -first 1).BackupSetGUID}
                }
                Write-Message -Level SomewhatVerbose -Message "Filtered $($allTlogs.count) log backups down to $($tLogs.count)" -Target $database
                $results += $tLogs
                #Catch the last Tlog that covers the restore time!
                $tLogfinal = $sqlBackupdetails | Where-Object { $_.BackupTypeDescription -eq 'Transaction Log' -and $_.BackupStartDate -gt $RestoreTime } | Sort-Object -Property LastLSN | Select-Object -First 1
                $results += $tLogfinal
                $tLogCount = ($results | Where-Object { $_.BackupTypeDescription -eq 'Transaction Log' }).count
                Write-Message -Level SomewhatVerbose -Message "$tLogCount Transaction Log backups found" -Target $database
            }
            
            [PSCustomObject]@{
                ID = $databaseName
                Values = $results
            }
        }
        
        Write-Message -Level InternalComment -Message 'Stopping'
    }
}