-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVolume Database Refresh.ps1
More file actions
83 lines (50 loc) · 3.02 KB
/
Volume Database Refresh.ps1
File metadata and controls
83 lines (50 loc) · 3.02 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
##############################################################################################################################
# Volume Database Refresh
#
# Scenario:
# Script will refresh a database on the target server from a source database on a separate server
#
# Prerequisities:
# sqlserver and PureStoragePowerShellSDK modules installed on client machine
# Two SQL instances with a database that has its data and log files on one volume on both servers
# Example here assumes vVols but RDMs will work as well
#
# Usage Notes:
# Each section of the script is meant to be run one after the other. The script is not meant to be executed all at once.
#
# Disclaimer:
# This example script is provided AS-IS and meant to be a building block to be adapted to fit an individual
# organization's infrastructure.
#
# THIS IS A SAMPLE SCRIPT WE USE FOR DEMOS! _PLEASE_ do not save your passwords in cleartext here.
# Use NTFS secured, encrypted files or whatever else -- never cleartext!
#
##############################################################################################################################
# Import powershell modules
Import-Module SqlServer
Import-Module PureStoragePowerShellSDK2
# Declare variables
$Target = 'SqlServer1' # Name of target VM
$ArrayName = 'flasharray1.example.com' # FlashArray FQDN
$DatabaseName = 'AdventureWorks' # Database to be refreshed
$TargetDiskSerialNumber = '6000c02022cb876dcd321example01b' # Target Disk Serial Number
$SourceVolumeName = 'vvol-SERVERNAME-a-1-3d9acfdd-vg/Data-example' # Source volume name on FlashArray
$TargetVolumeName = 'vvol-SERVERNAME-a-1-3d9acfdd-vg/Data-example' # Target volume name on FlashArray
# Create a Powershell session against the target VM
$TargetSession = New-PSSession -ComputerName $Target
# Set credential to connect to FlashArray
$Credential = Get-Credential
# Offline the database
Invoke-Sqlcmd -ServerInstance $Target -Database master -Query "ALTER DATABASE [$DatabaseName] SET OFFLINE WITH ROLLBACK IMMEDIATE"
# Offline the volume
Invoke-Command -Session $TargetSession -ScriptBlock { Get-Disk | Where-Object { $_.SerialNumber -eq $using:TargetDiskSerialNumber } | Set-Disk -IsOffline $True }
# Connect to the FlashArray's REST API
$FlashArray = Connect-Pfa2Array –EndPoint $ArrayName -Credential $Credential -IgnoreCertificateError
# Perform the volume overwrite (no intermediate snapshot needed!)
New-Pfa2Volume -Array $FlashArray -Name $TargetVolumeName -SourceName $SourceVolumeName -Overwrite $true
# Online the volume
Invoke-Command -Session $TargetSession -ScriptBlock { Get-Disk | ? { $_.SerialNumber -eq $using:TargetDiskSerialNumber } | Set-Disk -IsOffline $False }
# Online the database
Invoke-Sqlcmd -ServerInstance $Target -Database master -Query "ALTER DATABASE [$DatabaseName] SET ONLINE WITH ROLLBACK IMMEDIATE"
# Clean up
Remove-PSSession $TargetSession