Wednesday, 15 October 2014

Synchronize-VolumesX4.ps1

Continuing the X4 Series (x4 SVMs/x4 copies of the data) which aims (at the end), to use PowerShell to automate the construction of SVMs, volumes, keep configurations in sync, perform DR, and then - time permitting - show that what we’ve achieved with all this ugly coding could have been done much more elegantly in WFA. I feel this journey is important though - which is why we’re on it!

Introduction

In Vol-CreatorX4.ps1, we create 4 copies of a data volume, with protection relationships across the 4 SVMs. This script aims to solve the problem of “if someone manually creates a volume in the production SVM, how do we make sure the volume configuration is replicated and protected across all 3 clusters?” There will be subsequent posts for synchronizing CIFS shares, NFS Exports, Quotas.

Non-Interactive Mode

Because I’m lazy to keep typing, this script runs non-interactively from the PowerShell command line, as well as interactively. E.g.:

PS C:\Users\USER> .\Synchronize-VolumesX4.ps1 -User admin -Pass 123456 -SkipPrompt

The Script

Note: Steps 4, 5 and 6 are pretty much identical bits of PowerShell. The script could have been made a lot shorter using functions. The intention here is not to make a short script, so as to better illustrate all the steps and what they involve.

###############################
## Synchronize-VolumesX4.ps1 ##
###############################

Param($User,$Pass,[Switch]$SkipPrompt)
"";"<<<<< Synchronize-VolumesX4.ps1 >>>>>";""
"The script is designed to Synchronize the volumes in a 4 copy data-protection environment:
1 --SM--> 2
1 --SV--> 3 --SM--> 4.
It gets the details of the volumes in SVM 1, and creates any volumes that are missing on SVMs 2,3,4 including setting up SnapMirror (SM), and SnapVault (SV) relationships.";""

## USER INPUTS ##
If(!$User){$User = Read-Host ">>> Enter Admin User "}
If(!$Pass){$Pass = Read-Host ">>> Enter Password " -AsSecureString}
else {$Pass = $Pass | ConvertTo-SecureString  -AsPlainText -Force}
$Cred = New-Object System.Management.Automation.PsCredential($User,$Pass)

## CLUSTER / SVM / VOL / CIFS SHARE INFO ##
$1234 = 1,2,3,4; $234 = 2,3,4
$Clu = "","NCA1","NCB1","NCA2","NCB2"
$Svm = "","NCA1V1","NCB1V1","NCA2V1","NCB2V1"

## STEP 1 ##
"";">>> STEP 1: AQUIRE SVM 1's (" + $Svm[1] + ") ROOTVOL NAME";""
Import-Module DataONTAP
Connect-NcController -Name $Clu[1] -Credential $Cred
$Attrs = Get-NcVserver -Template
$Attrs.RootVolume = ""
$RootVol = (Get-NcVserver -Attributes $Attrs -VserverContext $Svm[1]).RootVolume
"SVM 1's ROOTVOL = $Rootvol (this will be ignored for synchronization)"

## STEP 2 ##
"";">>> STEP 2: GATHER LIST OF DATA VOLUMES ON SVM 1 (" + $Svm[1] + ")";""
$Attrs = Get-NcVol -Template
Initialize-NcObjectProperty -object $Attrs -name VolumeSpaceAttributes
Initialize-NcObjectProperty -object $Attrs -name VolumeIdAttributes
$Attrs.VolumeSpaceAttributes.Size = ""
$Attrs.VolumeIdAttributes.Type = ""
$Attrs.VolumeSpaceAttributes.SpaceGuarantee = ""
$Volumes = Get-NcVol -Attributes $Attrs -VserverContext $Svm[1]
$DataVols = @()
$Volumes | foreach {If ($_.Name -ne $RootVol){$DataVols += $_} }

## STEP 3 ##
"";">>> STEP 3a: CHECK DATA VOLUMES on SVM's 2,3,4"
">>> STEP 3b: IF THEY EXIST CHECK size/type/space-guarantee is correct!"
">>> STEP 3c: CREATE DP VOLS IF DON'T EXIST";""
$234 | foreach {
## STEP 3a ##
Connect-NcController -Name $Clu[$_] -Credential $Cred
$SvmName = $Svm[$_]
$Vols = Get-NcVol -Attributes $Attrs -VserverContext $Svm[$_]
Foreach ($DVol in $DataVols){
$SourceVol = $DVol.Name
$VolMatch = $false
$i = 0
Foreach ($Vol in $Vols){
$VolName = $Vol.Name
If ($VolName -eq $SourceVol){
## STEP 3b ##
"Volume $SourceVol exists on SVM $SvmName - comparing its size, type, and space guarantee!"
$VolMatch = $true
$Discrepancy = $False
If ($DVol.VolumeSpaceAttributes.Size -ne $Vol.VolumeSpaceAttributes.Size){"$VolName size discrepancy detected!";$Discrepancy = $True}
If ($Vol.VolumeIdAttributes.Type -ne "dp"){"$VolName should be type dp!";$Discrepancy = $True}
If ($DVol.VolumeSpaceAttributes.SpaceGuarantee -ne $Vol.VolumeSpaceAttributes.SpaceGuarantee){"$VolName space guarantee discrepancy detected!";$FoundDiscrepancy = $True}
If ($Discrepancy){
Write-Host "!!! Discepancies deteceted, manually resolve issues with $VolName on SVM $SvmName !!!" -ForegroundColor Red
If(!$SkipPrompt){Read-Host ">>> Press ENTER to CONTINUE <<<"}
}
}
}
If(!$VolMatch){
## STEP 3c ##
"Volume $SourceVol does NOT exist on SVM $SvmName - creating!"
New-NcVol -Name $SourceVol -Aggregate aggr1 -JunctionPath $null -SpaceReserve $DVol.VolumeSpaceAttributes.SpaceGuarantee -Type dp -VserverContext $Svm[$_] -Size $DVol.VolumeSpaceAttributes.Size
}
}            
}

## STEP 4 ##
"";">>> STEP 4a: CHECK EXISTS 1 --SM--> 2"
">>> STEP 4b: IF IT EXISTS, check it is SnapMirrored, has the correct schedule and type!"
">>> STEP 4c: CREATE THE RELATIONSHIP IF IT DOESN'T EXIST and initialize";""
Connect-NcController $Clu[2] -Credential $Cred
Foreach ($DVol in $DataVols){
$SourceVol = $DVol.Name
## STEP 4a ##
$SMResult = Get-NcSnapMirror -SourceVolume $SourceVol -SourceVserver $Svm[1] -DestinationVserver $Svm[2]
If($SMResult){
## STEP 4b ##
"SnapMirror relationship 1 --SM--> 2 exists for $SourceVol"
$SMIssue = $False         
If($SMResult.MirrorState -ne "snapmirrored"){"SnapMirror is not SnapMirrored!";$SMIssue=$True}
If($SMResult.Schedule -ne "sm20min"){"SnapMirror should have schedule sm20min!";$SMIssue=$True}
If($SMResult.RelationshipType -ne "data_protection"){"SnapMirror should by dp type!";$SMIssue=$True}
If($SMIssue){
Write-Host "!!! Please manually investigate this relationship !!!" -ForegroundColor Red
If(!$SkipPrompt){Read-Host ">>> Press ENTER to CONTINUE <<<"}
}
} else {
## STEP 4c ##
New-NcSnapmirror -DestinationVserver $Svm[2] -DestinationVolume $SourceVol -SourceVserver $Svm[1] -SourceVolume $SourceVol -Schedule sm20min -Type dp
Invoke-NcSnapmirrorInitialize -DestinationVserver $Svm[2] -DestinationVolume $SourceVol
}
}

## STEP 5 ##
"";">>> STEP 5a: CHECK EXISTS 1 --SV--> 3"
">>> STEP 5b: IF IT EXISTS, check it is SnapVaulted, has the correct schedule, type and policy!"
">>> STEP 5c: CREATE THE RELATIONSHIP IF IT DOESN'T EXIST and initialize";""
Connect-NcController $Clu[3] -Credential $Cred
Foreach ($DVol in $DataVols){
$SourceVol = $DVol.Name
## STEP 5a ##
$SMResult = Get-NcSnapMirror -SourceVolume $SourceVol -SourceVserver $Svm[1] -DestinationVserver $Svm[3]
If($SMResult){
## STEP 5b ##
"SnapVault relationship 1 --SV--> 3 exists for $SourceVol"
$SMIssue = $False         
If($SMResult.MirrorState -ne "snapmirrored"){"SnapVault is not SnapMirrored!";$SMIssue=$True}
If($SMResult.Schedule -ne "sv60min"){"SnapVault should have schedule sv60min!";$SMIssue=$True}
If($SMResult.RelationshipType -ne "vault"){"Relationship should by xdp (Vault) type!";$SMIssue=$True}
If($SMResult.Policy -ne "92x60min_16x24hrs"){"Relationship should have SnapMirror Policy 92x60min_16x24hrs!";$SMIssue=$True}
If($SMIssue){
Write-Host "!!! Please manually investigate this relationship !!!" -ForegroundColor Red
If(!$SkipPrompt){Read-Host ">>> Press ENTER to CONTINUE <<<"}
}
} else {
## STEP 5c ##
New-NcSnapmirror -DestinationVserver $Svm[3] -DestinationVolume $SourceVol -SourceVserver $Svm[1] -SourceVolume $SourceVol -Schedule sv60min -Type vault -Policy 92x60min_16x24hrs
Invoke-NcSnapmirrorInitialize -DestinationVserver $Svm[3] -DestinationVolume $SourceVol
}
}

## STEP 6 ##
"";">>> STEP 6a: CHECK EXISTS 3 --SM--> 4"
">>> STEP 6b: IF IT EXISTS, check it is SnapMirrored, has the correct schedule and type!"
">>> STEP 6c: CREATE THE RELATIONSHIP IF IT DOESN'T EXIST and initialize";""
Connect-NcController $Clu[4] -Credential $Cred
Foreach ($DVol in $DataVols){
$SourceVol = $DVol.Name
## STEP 6a ##
$SMResult = Get-NcSnapMirror -SourceVolume $SourceVol -SourceVserver $Svm[3] -DestinationVserver $Svm[4]
If($SMResult){
## STEP 6b ##
"SnapMirror relationship 3 --SM--> 4 exists for $SourceVol"
$SMIssue = $False         
If($SMResult.MirrorState -ne "snapmirrored"){"SnapMirror is not SnapMirrored!";$SMIssue=$True}
If($SMResult.Schedule -ne "sm60min"){"SnapMirror should have schedule sm60min!";$SMIssue=$True}
If($SMResult.RelationshipType -ne "data_protection"){"SnapMirror should by dp type!";$SMIssue=$True}
If($SMIssue){
Write-Host "!!! Please manually investigate this relationship !!!" -ForegroundColor Red
If(!$SkipPrompt){Read-Host ">>> Press ENTER to CONTINUE <<<"}
}
} else {
## STEP 6c ##
New-NcSnapmirror -DestinationVserver $Svm[4] -DestinationVolume $SourceVol -SourceVserver $Svm[3] -SourceVolume $SourceVol -Schedule sm60min -Type dp
Invoke-NcSnapmirrorInitialize -DestinationVserver $Svm[4] -DestinationVolume $SourceVol
}
}

## THE END ##

No comments:

Post a Comment