functions/Restore-DbaFromDatabaseSnapshot.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ValidationTags#FlowControl#
function Restore-DbaFromDatabaseSnapshot {
    <#
        .SYNOPSIS
            Restores databases from snapshots
 
        .DESCRIPTION
            Restores the database from the snapshot, discarding every modification made to the database
            NB: Restoring to a snapshot will result in every other snapshot of the same database to be dropped
            It also fixes some long-standing bugs in SQL Server when restoring from snapshots
 
        .PARAMETER SqlInstance
            The SQL Server that you're connecting to
 
        .PARAMETER SqlCredential
            Credential object used to connect to the SQL Server as a different user
 
        .PARAMETER Database
            Restores from the last snapshot databases with this names only. You can pass either Databases or Snapshots
 
        .PARAMETER ExcludeDatabase
            The database(s) to exclude - this list is auto-populated from the server
 
        .PARAMETER Snapshot
            Restores databases from snapshots with this names only. You can pass either Databases or Snapshots
 
        .PARAMETER Force
            If restoring from a snapshot involves dropping any other shapshot, you need to explicitly
            use -Force to let this command delete the ones not involved in the restore process.
            Also, -Force will forcibly kill all running queries that prevent the restore process.
 
        .PARAMETER WhatIf
            Shows what would happen if the command were to run
 
        .PARAMETER Confirm
            Prompts for confirmation of every step.
 
        .PARAMETER EnableException
            By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message.
            This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting.
            Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch.
 
        .NOTES
            Tags: DisasterRecovery, Snapshot, Backup, Restore, Database
            Author: niphlod
 
            Website: https://dbatools.io
            Copyright: (C) Chrissy LeMaire, clemaire@gmail.com
            License: GNU GPL v3 https://opensource.org/licenses/GPL-3.0
 
        .LINK
             https://dbatools.io/Restore-DbaFromDatabaseSnapshot
 
        .EXAMPLE
            Restore-DbaFromDatabaseSnapshot -SqlInstance sqlserver2014a -Database HR, Accounting
 
            Restores HR and Accounting databases using the latest snapshot available
 
        .EXAMPLE
            Restore-DbaFromDatabaseSnapshot -SqlInstance sqlserver2014a -Database HR -Force
 
            Restores HR database from latest snapshot and kills any active connections in the database on sqlserver2014a.
 
        .EXAMPLE
            Restore-DbaFromDatabaseSnapshot -SqlInstance sqlserver2014a -Snapshot HR_snap_20161201, Accounting_snap_20161101
 
            Restores databases from snapshots named HR_snap_20161201 and Accounting_snap_20161101
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param (
        [parameter(Mandatory = $true, ValueFromPipeline = $true)]
        [Alias("ServerInstance", "SqlServer")]
        [DbaInstance[]]$SqlInstance,
        [Alias("Credential")]
        [PSCredential]$SqlCredential,
        [Alias("Databases")]
        [object[]]$Database,
        [object[]]$ExcludeDatabase,
        [object[]]$Snapshot,
        [switch]$Force,
        [switch][Alias('Silent')]$EnableException
    )

    process {
        if (!$Snapshot -and !$Database -and !$ExcludeDatabase) {
            Stop-Function -Message "You must specify either -Snapshot (to restore from) or -Database/-ExcludeDatabase (to restore to)"
        }

        foreach ($instance in $SqlInstance) {
            Write-Message -Level Verbose -Message "Connecting to $instance"
            try {
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
            }
            catch {
                Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
            }

            $allDbs = $server.Databases

            # vault to hold all programmed operations from --> to
            $operations = @()

            if (!$Snapshot -and !$Database -and !$ExcludeDatabase) {
                # Restore all databases from the latest snapshot
                Write-Message -Level Verbose -Message "Selected all databases"
                $dbs = $allDbs | Where-Object IsDatabaseSnapshot -EQ $true
            }
            elseif ($Database) {
                # Restore only these databases from their latest snapshot
                <#
                    Note, for some reason lookup of this property has to be done using $_.DatabaseSnapshotBaseName
                    Removing this will cause: Where-Object : A positional parameter cannot be found that accepts argument 'System.Object[]'
                #>

                Write-Message -Level Verbose -Message "Selected only databases"
                $dbs = $allDbs | Where-Object {$Database -contains $_.DatabaseSnapshotBaseName}
            }
            elseif ($ExcludeDatabase) {
                Write-Message -Level Verbose -Message "Excluded only databases"
                $dbs = $allDbs | Where-Object {$ExcludeDatabase -NotContains $_.DatabaseSnapshotBaseName}
            }
            elseif ($Snapshot) {
                # Restore databases from these snapshots
                Write-Message -Level Verbose -Message "Selected only snapshots"
                $dbs = $allDbs | Where-Object { $Snapshot -contains $_.Name }
                $baseDatabases = $dbs | Select-Object -ExpandProperty DatabaseSnapshotBaseName | Get-Unique
                if ($baseDatabases.Count -ne $Snapshot.Count -and $dbs.Count -ne 0) {
                    Write-Message -Level Warning -Message "Multiple snapshots selected for the same database, skipping"
                    continue
                }
            }

            $opsHash = @{ }

            foreach ($db in $dbs) {
                if ($db.DatabaseSnapshotBaseName -notin $opsHash.Keys) {
                    if ($snapshot.Count -gt 0) {
                        # just in the need to drop every other snapshot
                        $toDrop = $allDbs | Where-Object { $_.DatabaseSnapshotBaseName -eq $db.DatabaseSnapshotBaseName }
                        $toDrop = $todrop | Select-Object Name
                        $opsHash[$db.DatabaseSnapshotBaseName] = @{
                            'from' = $db | Select-Object Name, DatabaseSnapshotBaseName, CreateDate
                            'drop' = $toDrop
                        }
                    }
                    else {
                        $opsHash[$db.DatabaseSnapshotBaseName] = @{
                            'from' = $db
                            'drop' = @()
                        }
                    }
                }
                else {
                    # store each older snapshot in the drop list while enumerating
                    if ($db.CreateDate -gt $opsHash[$db.DatabaseSnapshotBaseName]['from'].CreateDate) {
                        $prev = $opsHash[$db.DatabaseSnapshotBaseName]['from']
                        $opsHash[$db.DatabaseSnapshotBaseName]['from'] = $db | Select-Object Name, DatabaseSnapshotBaseName, CreateDate
                        $opsHash[$db.DatabaseSnapshotBaseName]['drop'] += $prev
                    }
                }
            }
            foreach ($dbName in $opsHash.Keys) {
                $drop = @()
                foreach ($toDrop in $opsHash[$dbName]['drop']) {
                    $drop += $toDrop.Name
                }
                $operations += @{
                    'from' = $opsHash[$dbName]['from'].Name
                    'to'   = $dbName
                    'drop' = $drop
                }
            }

            foreach ($op in $operations) {
                # Check if there are FS, because then a restore is not possible
                $all_FS = $server.Databases[$op['to']].FileGroups | Where-Object FileGroupType -EQ 'FileStreamDataFileGroup'
                if ($all_FS.Count -gt 0) {
                    Write-Message -Level Warning -Message "Database $($op['to']) has FileStream group(s). You cannot restore from snapshots"
                    [PSCustomObject]@{
                        ComputerName = $server.NetName
                        InstanceName = $server.ServiceName
                        SqlInstance  = $server.DomainInstanceName
                        Database     = $op['to']
                        Snapshot     = $op['from']
                        Status       = 'Error'
                        Notes        = "Database $($op['to']) has FileStream group(s). You cannot restore from snapshots"
                    }
                    break
                }
                # Get log size and autogrowth
                $orig_logproperties = $server.Databases[$op['to']].LogFiles | Select-Object Id, Size
                # Drop what needs to be dropped
                $opError = $false

                if ($op['drop'].Count -gt 1 -and $Force -eq $false) {
                    $warnMsg = @()
                    $warnMsg += "The restore process for $($op['to']) from $($op['from']) needs to drop the following:"
                    foreach ($db in $op['drop']) {
                        $warnMsg += $db
                    }
                    $warnMsg += "Use -Force if you really want to drop these snapshots."
                    Write-Message -Level Warning -Message ($warnMsg -join "`n")
                    break
                }
                foreach ($drop in $op['drop']) {
                    if ($Pscmdlet.ShouldProcess($server.name, "Remove db snapshot $drop")) {
                        # skip it if it's the same name
                        if ($drop -ne $($op['from'])) {
                            try {
                                if ($Force) {
                                    # snapshot with open transactions cannot be dropped
                                    $server.KillAllProcesses($drop)
                                }
                                $null = $server.Query("USE master; DROP DATABASE [$drop]")
                                $status = "Dropped"
                            }
                            catch {
                                Write-Message -Level Warning -Message $_
                                $operror = $true
                                break
                            }
                        }
                    }
                }
                if ($opError) {
                    Write-Message -Level Warning -Message "Errors trying to restore $($op['to']) from $($op['from'])"
                    [PSCustomObject]@{
                        ComputerName = $server.NetName
                        InstanceName = $server.ServiceName
                        SqlInstance  = $server.DomainInstanceName
                        Database     = $op['to']
                        Snapshot     = $op['from']
                        Status       = 'Error'
                        Notes        = "Failed to drop some snapshots"
                    }
                    break
                }

                # Need a proper restore now
                if ($Pscmdlet.ShouldProcess($server.DomainInstanceName, "Restore db $($op['to']) from $($op['from'])")) {
                    $query = "USE master; RESTORE DATABASE [$($op['to'])] FROM DATABASE_SNAPSHOT='$($op['from'])'"
                    try {
                        if ($Force) {
                            # for whatever reason, a snapshot with open transactions, albeit read-only, block the restore process
                            $server.KillAllProcesses($op['from'])
                            # for a "good" reason, all open transactions on the destination block the restore process
                            $server.KillAllProcesses($op['to'])
                        }
                        $null = $server.Query($query)
                    }
                    catch {
                        $opError = $true
                        $inner = $_.Exception.Message
                        Stop-Function -Message "Original exception: $inner, Query issued: $query, Error: $_.Exception.InnerException.InnerException.Message" -ErrorRecord $_
                    }
                }
                if ($operror) {
                    Write-Message -Level Warning -Message "Errors trying to restore $($op['to']) from $($op['from'])"
                    [PSCustomObject]@{
                        ComputerName = $server.NetName
                        InstanceName = $server.ServiceName
                        SqlInstance  = $server.DomainInstanceName
                        Database     = $op['to']
                        Snapshot     = $op['from']
                        Status       = 'Error'
                        Notes        = ''
                    }
                    break
                }
                # Comparing sizes before and after, need to reconnect to see if size
                # changed
                $server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential
                foreach ($log in $server.Databases[$op['to']].LogFiles) {
                    $matching = $orig_logproperties | Where-Object ID -EQ $log.ID
                    if ($matching.Size -ne $log.Size) {
                        Write-Message -Level Verbose -Message "Resizing log to the original value"
                        $log.Size = $matching.Size
                        $log.Alter()
                    }
                }
                [PSCustomObject]@{
                    ComputerName = $server.NetName
                    InstanceName = $server.ServiceName
                    SqlInstance  = $server.DomainInstanceName
                    Database     = $op['to']
                    Snapshot     = $op['from']
                    Status       = 'Restored'
                    Notes        = 'Remember to take a backup now, and also to remove the snapshot if not needed'
                }
            }
        }
    }
}