Sunday, 19 October 2014

Synchronizing Quotas - Part 3/3: Quotas-Recover.ps1

Note: Did this in a bit of a rush...

########################
## Quotas-Recover.ps1 ##
########################

Param($User,$Pass,$Cluster,$SVM,[Switch]$Demo)
Import-Module DataONTAP

"";"<<<<< Quotas-Recover.ps1 >>>>>";""
"The script is designed to restore Quota Policy Rule information from special hidden CIFS shares and their comments field, in a DR situation. For simplification, it is assumed that the VOLUMES and CIFS shares are synched perfectly (i.e. no additional QUOTA.RULE.* shares on DR that should have been deleted!)";""

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)
If(!$Cluster){$Cluster = Read-Host ">>> Enter DR Cluster "}
If(!$SVM){$SVM = Read-Host ">>> Enter DR SVM "}

"";"STEP 1: CONNECTING TO DR CLUSTER $Cluster AND FOCUSING ON THE DR SVM $SVM <<<"
[Void](Connect-NcController -Name $Cluster -Credential $Cred)
$Global:CurrentNcController.Vserver = $SVM

"";"STEP 2: GET THE QUOTA.RULE.* SHARES"
$Shares = Get-NcCifsShare -ShareName "QUOTA.RULE.*"

"";"STEP 3: EXTRACT VOLUMES FOR QUOTA ON"
$QRules_V = @{}
$QRules_V_Q = @{}
Foreach ($Share in $Shares){
$ShareName = $Share.ShareName
$ShareNameSplit = $ShareName.Split(".")
$VolName = $ShareNameSplit[2]
$QRules_V.$VolName = "on"
$QRules_V_Q.$VolName = @{}
}

"";"STEP 4: EXTRACT QTREES WITH DISK LIMITS"
Foreach ($Share in $Shares){
$ShareComment = $Share.Comment
$ShareName = $Share.ShareName
$ShareNameSplit = $ShareName.Split(".")
$SplitCount = $ShareNameSplit.Count
$VolName = $ShareNameSplit[2]
$QtreeName = $ShareNameSplit[3]
If ($SplitCount -eq 5){
$QRules_V_Q.$VolName.$QtreeName = $ShareComment
}     
}

"";"STEP 5: GET THE APPLIED QUOTA POLICY"
$SVM_Attrs = Get-NcVserver -Template
$SVM_Attrs.QuotaPolicy = ""
$Quota_Pol = (Get-NcVserver -Attributes $SVM_Attrs).QuotaPolicy

"";"STEP 6: CYCLE THROUGH VOLUMES, SETTING DISK-LIMITS ON QTREES, THEN TURNING QUOTA ON"
$DiskLimitRules = @{}

Foreach ($VolName in $QRules_V.Keys){
$DiskLimitRules.$VolName = 0

Foreach ($QtreeName in $QRules_V_Q.$VolName.Keys){
$DiskLimit = $QRules_V_Q.$VolName.$QtreeName
Write-Host "Adding Quota Rule to /vol/$VolName/$QtreeName with Disk Limit of $DiskLimit" -ForegroundColor Red
If(!$DEMO){Add-NcQuota -Path "/vol/$VolName/$QtreeName" -DiskLimit $DiskLimit -Policy $Quota_Pol}
$DiskLimitRules.$VolName ++
}

If($DiskLimitRules.$VolName -ne 0){
Write-Host "Enabling Quota on $VolName" -ForegroundColor Red
If(!$DEMO){Enable-NcQuota -Volume $VolName}

} else {
Write-Host "Volume $VolName has only tracking rules, we'll turn the quota on in STEP 6!" -ForegroundColor Red
}
}

"";"STEP 7: CYCLE THROUGH VOLUMES, SETTING SOFT-DISK LIMITS ON QTREES (without DISK-LIMITS), THEN RUNNING QUOTA RESIZE"
$VOL_Attrs = Get-NcVol -Template
Initialize-NcObjectProperty -object $VOL_Attrs -name VolumeSpaceAttributes
$VOL_Attrs.VolumeSpaceAttributes.Size = ""
$QTREE_Attrs = Get-NcQtree -Template
$QTREE_Attrs.Qtree = ""

Foreach ($VolName in $QRules_V.Keys){
$VOL_Size = (Get-NcVol -Volume $VolName -Attributes $VOL_Attrs -Vserver $SVM).TotalSize
$SoftDiskLimitSetting = $VOL_Size*4
$QTREE_List = (Get-NcQtree -Attributes $QTREE_Attrs -Volume $VolName -Vserver $SVM).Qtree

Foreach ($QtreeName in $QTREE_List){

If($QtreeName){

If(!$QRules_V_Q.$VolName.$QtreeName){
Write-Host "Enabling tracking quota on /vol/$VolName/$QtreeName" -ForegroundColor Red
If(!$DEMO){Add-NcQuota -Path "/vol/$VolName/$QtreeName" -SoftDiskLimit $SoftDiskLimitSetting -Policy $Quota_Pol}         
}
}
}            

If($DiskLimitRules.$VolName -ne 0){ Write-Host "Resizing Quota on $VolName" -ForegroundColor Red }
else { Write-Host "Enabling Quota on $VolName" -ForegroundColor Red }

If(!$DEMO -and ($DiskLimitRules.$VolName -ne 0)){
$JobStarted = $False

While(!$JobStarted){
$JobStarted = Start-NcQuotaResize -Volume $VolName -ErrorAction SilentlyContinue

If(!$JobStarted){
Write-Host "Resizing Quota Job Failed to Start - waiting 10 seconds!" -ForegroundColor Red
Sleep 10
}
}
}

If(!$DEMO -and ($DiskLimitRules.$VolName -eq 0)){
Enable-NcQuota -Volume $VolName
}
}

## THE END ##

No comments:

Post a Comment