Skip to content

Instantly share code, notes, and snippets.

@sjnovick
Created July 3, 2019 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjnovick/68ca5f72aea5c1a80342f51c0bba6e32 to your computer and use it in GitHub Desktop.
Save sjnovick/68ca5f72aea5c1a80342f51c0bba6e32 to your computer and use it in GitHub Desktop.
# realm_snap.ps1
#
# *** Excerpts of code only:
# From a custom UI implemented with PowerShell + WPF
# to provide dev team with point-and-click snapshot
# and restore of development environments or "realms"
#
# Leverages the PureStoragePowerShellToolkit:
# https://www.powershellgallery.com/packages/PureStoragePowerShellToolkit
#
# sjnovick 2016
#
function get-snapinfo { #get all snapshots, select newest, return info
param($pureVol)
$snaps = Get-PfaVolumeSnapshots -Array $FlashArray -VolumeName $pureVol | sort -Descending created
if ($snaps){
$newestSnap = $snaps[0]
$source = $newestSnap.source
$serial = $newestSnap.serial
$created = $newestSnap.created
$name = $newestSnap.name
$size = $newestSnap.size
$snapInfo = "$created`n$name"}
else {$snapInfo = "no snaps found"}
return $snapInfo
}
function new-snap { #snapshot pure volumes
param($pureVol)
$user=($(whoami) -creplace '^[^\\]*\\', '')
$user=$user.Substring(0,3)
$date=$(date -format "MMddyyyy-HHmm")
$suffix="$date"+"-"+"$user"
New-PfaVolumeSnapshots -Array $FlashArray -Sources $pureVol -Suffix $suffix
}
function restore-snap {
param($pureVol, $DBsrv, $DBInst)
#determines the service name based off the instance name
if ($DBInst -ne "MSSQLSERVER"){
$serversvc = "MSSQL"+"$"+"$DBInst"}
else{$serversvc = "MSSQLSERVER"}
#get all snapshots, select newest
$snaps = Get-PfaVolumeSnapshots -Array $FlashArray -VolumeName $pureVol | sort -Descending created
$newestSnap = $snaps[0]
#stop sql server service
$sb = {param($serversvc)
$deps = get-service "$serversvc" -DependentServices
stop-service $deps
stop-service "$serversvc"}
Invoke-Command -ComputerName $DBsrv -ScriptBlock $sb -Args $serversvc
#restore (overwrite volume with newewst snapshot)
New-PfaVolume -Array $FlashArray -Source $newestSnap.name -VolumeName $pureVol -Overwrite
#start sql server service
$sb = {param($serversvc)
$deps = get-service "$serversvc" -DependentServices
start-service "$serversvc"
start-service $deps
}
Invoke-Command -ComputerName $DBsrv -ScriptBlock $sb -Args $serversvc
}
$WPFcboRealm.Add_SelectionChanged({
if ($WPFcboRealm.SelectedValue.ToString() -eq 'REALM1'){
$WPFtxtPureVol.text = "REALM1-data"
$WPFtxtDBsrv.text = "realm1.sjno.net"
$WPFtxtDBinst.text ="REALM1"}
elseif ($WPFcboRealm.SelectedValue.ToString() -eq 'REALM2'){
$WPFtxtPureVol.text = "REALM2-Data";
$WPFtxtDBsrv.text = "realm2.sjno.net"
$WPFtxtDBinst.text ="REALM2"}
elseif ($WPFcboRealm.SelectedValue.ToString() -eq 'REALM3'){
$WPFtxtPureVol.text = "REALM3-Data"
$WPFtxtDBsrv.text = "realm3.sjno.net"
$WPFtxtDBinst.text ="REALM3"}
else{}#cbo input error, needs handling
$WPFtxtOutput.text = get-snapInfo($WPFtxtPureVol.text)})
$WPFcmdClear.Add_Click({
$WPFtxtPureVol.text = ""
$WPFtxtDBsrv.text = ""
$WPFtxtDBinst.text = ""
$WPFtxtOutput.text = ""})
$WPFcmdRestore.Add_Click({
if ($WPFtxtOutput.text -ne "" -and $WPFtxtOutput.text -ne "no snaps found"){
$OKcancel = new-messagebox -Message "Are you sure? This will take $($WPFtxtDBsrv.text)\$($WPFtxtDBinst.text) offline for a short time and restore $($WPFtxtPureVol.text) to the latest snapshot." -Title Warning -Button OKCancel -Icon Warning -DefaultButton Cancel -OutputBool
if ($OKCancel -eq $true){
$pureVol = $WPFtxtPureVol.text
$DBsrv = $WPFtxtDBsrv.text
$DBinst = $WPFtxtDBinst.text
restore-snap $pureVol $DBsrv $DBinst}
else{}
}
else {new-messagebox -Message "No available snap" -Title Error -Button OK -Icon Error -DefaultButton OK}
})
$WPFcmdSnap.Add_Click({
$OKCancel = new-messagebox -Message "Are you sure? This will create a new snapshot of $($WPFtxtPureVol.text)." -Title Warning -Button OKCancel -Icon Warning -DefaultButton Cancel -OutputBool
if ($OKCancel -eq $true){
new-snap($WPFtxtPureVol.text)
$WPFtxtOutput.text = get-snapInfo($WPFtxtPureVol.text)}
else{}})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment