internal/Test-DbaRestoreVersion.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
function Test-DbaRestoreVersion
{
<#
.SYNOPSIS
Checks that the restore files are from a version of SQL Server that can be restored on the target version
 
.DESCRIPTION
Finds the anchoring Full backup (or multiple if it's a striped set).
Then filters to ensure that all the backups are from that anchor point (LastLSN) and that they're all on the same RecoveryForkID
Then checks that we have either enough Diffs and T-log backups to get to where we want to go. And checks that there is no break between
LastLSN and FirstLSN in sequential files
  
.PARAMETER FilterdRestoreFiles
This is just an object consisting of the output from Read-DbaBackupHeader. Normally this will have been filtered down to a restorable chain
before arriving here. (ie; only 1 anchoring Full backup)
 
.PARAMETER SqlServer
Sql Server Instance against which the restore is going to be performed
 
.PARAMETER SqlCredential
Credential for connectin to SqlServer
 
.NOTES
Original Author: Stuart Moore (@napalmgram), stuart-moore.com
 
dbatools PowerShell module (https://dbatools.io, clemaire@gmail.com)
Copyright (C) 2016 Chrissy LeMaire
 
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
 
.EXAMPLE
Test-DbaRestoreVersion -FilteredRestoreFiles $FilteredFiles -SqlServer server1\instance1
 
Checks that the Restore chain in $FilteredFiles is compatiable with the SQL Server version of server1\instance1
 
#>

[CmdletBinding()]
Param (
        [parameter(Mandatory = $true)]
        [Alias("ServerInstance", "SqlInstance")]
[object]$SqlServer,
        [parameter(Mandatory = $true)]
        [object[]]$FilteredRestoreFiles,
        [System.Management.Automation.PSCredential]$SqlCredential
        
)
    $FunctionName =(Get-PSCallstack)[0].Command
    $RestoreVersion = ($FilteredRestoreFiles.SoftwareVersionMajor | Measure-Object -average).average
    Write-Verbose "$FunctionName - RestoreVersion is $RestoreVersion"
    #Test to make sure we don't have an upgrade mid backup chain, there's a reason I'm paranoid..
    if ([int]$RestoreVersion -ne $RestoreVersion)
    {
        Write-Warning "$FunctionName - Version number change during backups - $RestoreVersion"
        return $false
        break
    }
    #Can't restore backwards
    $Server = Connect-SqlServer -SqlServer $SqlServer -SqlCredential $SqlCredential
    if ($RestoreVersion -gt $Server.VersionMajor)
    {
        Write-Warning "$FunctionName - Backups are from a newer version of SQL Server than $($Server.Name)"
        return $false
        break   
    }

    if (($Server.VersionMajor -gt 10 -and $RestoreVersion -lt 9)  )
    {
        Write-Warning "$FunctionName - This version - $RestoreVersion - too old to restore on to $($Server.Name)"
        return $false
        break 
    }
    $server.ConnectionContext.Disconnect()
    return $True
}