-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRefresh-Dev-PsFunc.ps1
More file actions
317 lines (275 loc) · 13.4 KB
/
Refresh-Dev-PsFunc.ps1
File metadata and controls
317 lines (275 loc) · 13.4 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<#
.SYNOPSIS
A PowerShell function to refresh one or more SQL Server databases (the destination) from either a snapshot or
database.
.DESCRIPTION
This PowerShell function uses calls to the PowerShell SDK and dbatools module functions to refresh one or more SQL Server
server databases either from a snapshot (either supplied explicitly or from a list associated with a volume) or from a source
database directly.
.EXAMPLE
# Refresh a single database from a snapshot selected from a list of snapshots associated with the volume specified by the
# -RefreshSource parameter.
$Targets = @("Z-STN-WIN2016-A\DEVOPSTST")
Refresh-Dev-PsFunc -RefreshDatabase refresh-database `
-RefreshSource z-sql2016-devops-prd `
-DestSqlInstance $Targets `
-PfaEndpoint 10.225.112.10 `
-PfaUser pureuser `
-PfaPassword pureuser `
-PromptForSnapshot
.EXAMPLE
# Refresh multiple databases from a snapshot selected from a list of snapshots associated with the volume specified by the
# -RefreshSource parameter.$Targets = @("Z-STN-WIN2016-A\DEVOPSTST", "Z-STN-WIN2016-A\DEVOPSDEV")
Refresh-Dev-PsFunc -RefreshDatabase refresh-database `
-RefreshSource z-sql2016-devops-prd `
-DestSqlInstance $Targets `
-PfaEndpoint 10.225.112.10 `
-PfaUser pureuser `
-PfaPassword pureuser `
-PromptForSnapshot
.EXAMPLE
# Refresh a single database using the snapshot specified by the RefreshSource parameter:
$Targets = @("Z-STN-WIN2016-A\DEVOPSTST")
Refresh-Dev-PsFunc -RefreshDatabase refresh-database `
-RefreshSource source-snap `
-DestSqlInstance $Targets `
-PfaEndpoint 10.225.112.10 `
-PfaUser pureuser `
-PfaPassword pureuser `
-RefreshFromSnapshot
.EXAMPLE
# Refresh multiple databases using the snapshot specified by the RefreshSource parameter:
$Targets = @("Z-STN-WIN2016-A\DEVOPSTST", "Z-STN-WIN2016-A\DEVOPSDEV")
Refresh-Dev-PsFunc -RefreshDatabase refresh-database
-RefreshSource source-snap `
-DestSqlInstance $Targets `
-PfaEndpoint 10.225.112.10 `
-PfaUser pureuser `
-PfaPassword pureuser `
-RefreshFromSnapshot
#.EXAMPLE
# Refresh a single database from the database specified by the SourceDatabase parameter residing on the instance specified by RefreshSource
$Targets = @("Z-STN-WIN2016-A\DEVOPSTST")
Refresh-Dev-PsFunc -$RefreshDatabase refresh-database `
-RefreshSource z-sql-prd `
-DestSqlInstance $Targets `
-PfaEndpoint 10.225.112.10 `
-PfaUser pureuser `
-PfaPassword pureuser
.EXAMPLE
# Refresh multiple databases from the database specified by the SourceDatabase parameter residing on the instance specified by RefreshSource
$Targets = @("Z-STN-WIN2016-A\DEVOPSTST", "Z-STN-WIN2016-A\DEVOPSDEV")
Refresh-Dev-PsFunc -$RefreshDatabase refresh-database `
-RefreshSource z-sql-prd `
-DestSqlInstance $Targets `
-PfaEndpoint 10.225.112.10 `
-PfaUser pureuser `
-PfaPassword pureuser
.NOTES
This script requires that both the dbatools and PureStorage SDK modules available from the PowerShell gallery are
installed. It assumes that the source and destination databases reside on single logical volumes. The script needs
to be run as a user that has execution privilges to online / offline windows logical disks, online / offline the
target database.
It is recommended that this script be saved to a directory (C:\scripts\PowerShell\autoload in this example) on the
server it is executed from and autoloaded by the following commands being added to the PowerShell profile:
Import-Module -Name C:\scripts\PowerShell\dbatools
Import-Module -Name C:\scripts\PowerShell\PureStoragePowerShellSDK
$psdir="C:\scripts\PowerShell\autoload"
Get-ChildItem "${psdir}\*.ps1" | %{.$_}
Write-Host "Custom scripts loaded"
This function is available under the Apache 2.0 license, stipulated as follows:
Copyright 2017 Pure Storage, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
.LINK
TBD
#>
function Refresh-Dev-PsFunc
{
param(
[parameter(mandatory=$true)] [string] $RefreshDatabase
,[parameter(mandatory=$true)] [string] $RefreshSource
,[parameter(mandatory=$true)] [string[]] $DestSqlInstances
,[parameter(mandatory=$true)] [string] $PfaEndpoint
,[parameter(mandatory=$true)] [string] $PfaUser
,[parameter(mandatory=$true)] [string] $PfaPassword
,[parameter(mandatory=$false)] [switch] $PromptForSnapshot
,[parameter(mandatory=$false)] [switch] $RefreshFromSnapshot
)
$StartMs = Get-Date
if ( $PromptForSnapshot.IsPresent.Equals($false) -And $RefreshFromSnapshot.IsPresent.Equals($false) ) {
Write-Host "Connecting to source SQL Server instance" -ForegroundColor Yellow
try {
$SourceDb = Get-DbaDatabase -sqlinstance $RefreshSource -Database $RefreshDatabase
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to connect to source database $RefreshSource.$Database with: $ExceptionMessage"
Return
}
try {
$SourceServer = (Connect-DbaInstance -SqlInstance $RefreshSource).ComputerNamePhysicalNetBIOS
}
catch {
Write-Error "Failed to determine target server name with: $ExceptionMessage"
}
}
Write-Host "Connecting to array endpoint" -ForegroundColor Yellow
try {
$FlashArray = New-PfaArray –EndPoint $PfaEndpoint -UserName $PfaUser -Password (ConvertTo-SecureString -AsPlainText $PfaPassword -Force) -IgnoreCertificateError
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to connect to FlashArray endpoint $PfaEndpoint with: $ExceptionMessage"
Return
}
$GetDbDisk = { param ( $Db )
$DbDisk = Get-partition -DriveLetter $Db.PrimaryFilePath.Split(':')[0]| Get-Disk
return $DbDisk
}
$Snapshots = $(Get-PfaAllVolumeSnapshots $FlashArray)
$FilteredSnapshots = $Snapshots.where({ ([string]$_.Source) -eq $RefreshSource })
if ( $PromptForSnapshot.IsPresent ) {
Write-Host ' '
for ($i=0; $i -lt $FilteredSnapshots.Count; $i++) {
Write-Host 'Snapshot ' $i.ToString()
$FilteredSnapshots[$i]
}
$SnapshotId = Read-Host -Prompt 'Enter the number of the snapshot to be used for the database refresh'
}
elseif ( $RefreshFromSnapshot.IsPresent.Equals( $false ) ) {
try {
$SourceDisk = Invoke-Command -ComputerName $SourceServer -ScriptBlock $GetDbDisk -ArgumentList $SourceDb
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to determine source disk with: $ExceptionMessage"
Return
}
try {
$SourceVolume = Get-PfaVolumes -Array $FlashArray | Where-Object { $_.serial -eq $SourceDisk.SerialNumber } | Select name
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to determine source volume with: $ExceptionMessage"
Return
}
}
Foreach($DestSqlInstance in $DestSqlInstances) {
Write-Host "Connecting to destination SQL Server instance" -ForegroundColor Yellow
try {
$DestDb = Get-DbaDatabase -sqlinstance $DestSqlInstance -Database $RefreshDatabase
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to connect to destination database $DestSqlInstance.$Database with: $ExceptionMessage"
Return
}
try {
$TargetServer = (Connect-DbaInstance -SqlInstance $DestSqlInstance).ComputerNamePhysicalNetBIOS
}
catch {
Write-Error "Failed to determine target server name with: $ExceptionMessage"
}
$OfflineDestDisk = { param ( $DiskNumber, $Status )
Set-Disk -Number $DiskNumber -IsOffline $Status
}
try {
$DestDisk = Invoke-Command -ComputerName $TargetServer -ScriptBlock $GetDbDisk -ArgumentList $DestDb
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to determine destination database disk with: $ExceptionMessage"
Return
}
try {
$DestVolume = Get-PfaVolumes -Array $FlashArray | Where-Object { $_.serial -eq $DestDisk.SerialNumber } | Select name
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to determine destination FlashArray volume with: $ExceptionMessage"
Return
}
$OfflineDestDisk = { param ( $DiskNumber, $Status )
Set-Disk -Number $DiskNumber -IsOffline $Status
}
Write-Host "Offlining destination database" -ForegroundColor Yellow
try {
$DestDb.SetOffline()
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to offline database $Database with: $ExceptionMessage"
Return
}
Write-Host "Offlining destination Windows volume" -ForegroundColor Yellow
try {
Invoke-Command -ComputerName $TargetServer -ScriptBlock $OfflineDestDisk -ArgumentList $DestDisk.Number, $True
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to offline disk with : $ExceptionMessage"
Return
}
$StartCopyVolMs = Get-Date
Write-Host ' '
try {
if ( $PromptForSnapshot.IsPresent ) {
Write-Host "Snap -> DB refresh: Overwriting destination FlashArray volume <" $DestVolume.name "> with snapshot <" $FilteredSnapshots[$SnapshotId].name ">" -ForegroundColor Yellow
New-PfaVolume -Array $FlashArray -VolumeName $DestVolume.name -Source $FilteredSnapshots[$SnapshotId].name -Overwrite
}
elseif ( $RefreshFromSnapshot.IsPresent ) {
Write-Host "DB -> DB refresh : Overwriting destination FlashArray volume <" $DestVolume.name "> with snapshot <" $RefreshSource ">" -ForegroundColor Yellow
New-PfaVolume -Array $FlashArray -VolumeName $DestVolume.name -Source $RefreshSource -Overwrite
}
else {
Write-Host "DB -> DB refresh : Overwriting destination FlashArray volume <" $DestVolume.name "> with a copy of the source volume <" $SourceVolume.name ">" -ForegroundColor Yellow
New-PfaVolume -Array $FlashArray -VolumeName $DestVolume.name -Source $SourceVolume.name -Overwrite
}
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to refresh test database volume with : $ExceptionMessage"
Set-Disk -Number $DestDisk.Number -IsOffline $False
$DestDb.SetOnline()
Return
}
$EndCopyVolMs = Get-Date
Write-Host "Volume overwrite duration (ms) = " ($EndCopyVolMs - $StartCopyVolMs).TotalMilliseconds -ForegroundColor Yellow
Write-Host " "
Write-Host "Onlining destination Windows volume" -ForegroundColor Yellow
try {
Invoke-Command -ComputerName $TargetServer -ScriptBlock $OfflineDestDisk -ArgumentList $DestDisk.Number, $False
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to online disk with : $ExceptionMessage"
Return
}
Write-Host "Onlining destination database" -ForegroundColor Yellow
try {
$DestDb.SetOnline()
}
catch {
$ExceptionMessage = $_.Exception.Message
Write-Error "Failed to online database $Database with: $ExceptionMessage"
Return
}
}
$EndMs = Get-Date
Write-Host " "
Write-Host "-------------------------------------------------------" -ForegroundColor Green
Write-Host " "
Write-Host "D A T A B A S E R E F R E S H C O M P L E T E" -ForegroundColor Green
Write-Host " "
Write-Host " Duration (s) = " ($EndMs - $StartMs).TotalSeconds -ForegroundColor White
Write-Host " "
Write-Host "-------------------------------------------------------" -ForegroundColor Green
}