Software Development Kit (SDK) and API Discussions

Script to modify volume automatically

leaqui
707 Views

Hi everyone, I need to automatically at 1am make the .snapshot directory available for a backup and hid the .snapshot directory at 3am after the backup. It just 2 simple commands that is required but it can't be made on the ontap so I'm a bit lost of how can I do it. Anyone kind enough to give me a light please?

Thank you in advance.

2 REPLIES 2

mbeattie
502 Views

Hi,

 

You could do something like this...so you have one script which you run with different argument values depending on if you want to turn snapdir on or off and set the command to run the script in the schedule task configuration.

 

 

Param(
   [Parameter(Mandatory = $True, HelpMessage = "The state of the snapshot directory for the volume")]
   [ValidateSet("on","off")]
   [String]$Snapdir
)
#'------------------------------------------------------------------------------
#'Set variables.
#'------------------------------------------------------------------------------
$cluster = "192.168.100.10"
$vserver = "svm1"
$volume  = "vol1"
#'------------------------------------------------------------------------------
#'Set the snapdir access value based on the input parameter.
#'------------------------------------------------------------------------------
If($Snapdir -eq "on"){
   $AccessEnabled = $True
}Else{
   $AccessEnabled = $False
}
#'------------------------------------------------------------------------------
#'Enumerate cached credentials and connect to the cluster.
#'------------------------------------------------------------------------------
$credential = (Get-NcCredential -Controller $cluster).Credential
Connect-NcController -Name $cluster -Credential $credential
#'------------------------------------------------------------------------------
#'Update the snapshot directory access for the volume.
#'------------------------------------------------------------------------------
$query = Get-NcVol -Template
Initialize-NcObjectProperty $query VolumeIdAttributes
$query.VolumeIdAttributes.Name = $volume
$query.VolumeIdAttributes.OwningVserverName = $vserver
$attributes = Get-NcVol -Template
$attributes.SnapshotDirectoryAccessEnabled = $True
Update-NcVol -Query $query -Attributes $attributes
#'------------------------------------------------------------------------------

 

 

Note: This assumes that you've cached the credentials to connect to cluster as within the security context of the account that is running the scheduled task using the "Add-NcCredential" cmdlet.

 

However i noticed an issue with the "update-ncvol" cmdlet trying to enable snapdir for older versions of ontap so you would have to use the private REST CLI to run the snapdir command. Which version of ONTAP are you using???

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

mbeattie
489 Views

Hi leaqui,

 

Here you go, this script will set the "snapdir-access" on or off based on the input variable values. EG:

 

 

 

PS C:\Scripts\SetSnapdirAccess> .\SetSnapdirAccess.ps1 -Cluster 192.168.100.100 -Vserver svm1 -Volume vol1 -Snapdir on
The ONTAP version running on cluster "192.168.100.100" is "9.12.1P8". Invoking System API
Executed Command: volume modify -vserver svm1 -volume vol1 -snapdir-access true
Updated snapshot directory access on volume "vol1" on vserver "svm1" on cluster "192.168.100.100"

 

 

 

You need to ensure the PowerShell toolkit is installed. Open an elevated PowerShell prompt as admin and run:

 

 

 

Install-Module NetApp.ONTAP

 

 

 

Then open a PowerShell prompt as the Active Directory service account user that you will be running the scheduled task as. EG:

 

 

 

runas /user:domain\username powershell.exe

 

 

 

Then cache the credentials as the user that will run the scheduled task:

 

 

 

$credential = Get-Credential
Add-NcCredential -Controller 192.168.100.100 -Credential $credential

 

 

 

When you are configuring your scheduled task you set the input parameters. EG:

 

 

 

powershell.exe C:\Scripts\SetSnapdirAccess\SetSnapdirAccess.ps1 -Cluster 192.168.100.100 -Vserver svm1 -Volume vol1 -Snapdir off

 

 

 

Note: That you can run the same script to turn it back on again. Just create a second scheduled task and set the -Snapdir parameter value to on

 

Here is the source code.

 

Param(
   [Parameter(Mandatory = $True, HelpMessage = "The cluster name or IP Address")]
   [String]$Cluster,
   [Parameter(Mandatory = $True, HelpMessage = "The vserver name")]
   [String]$Vserver,
   [Parameter(Mandatory = $True, HelpMessage = "The volume name")]
   [String]$Volume,
   [Parameter(Mandatory = $True, HelpMessage = "The state of the snapshot directory for the volume")]
   [ValidateSet("on","off")]
   [String]$Snapdir
)
#'------------------------------------------------------------------------------
#'Set the snapdir access value based on the input parameter.
#'------------------------------------------------------------------------------
If($Snapdir -eq "on"){
   $AccessEnabled = $True
}Else{
   $AccessEnabled = $False
}
#'------------------------------------------------------------------------------
#'Enumerate cached credentials for the cluster.
#'------------------------------------------------------------------------------
Try{
   $credential = (Get-NcCredential -Controller $cluster -ErrorAction Stop).Credential
}Catch{
   Write-Warning -Message "Failed enumerating credentials for cluster ""$cluster"""
}
If($Null -eq $credential){
   Write-Warning -Message "The credentials for cluster ""$cluster"" were null. Exiting"
   Exit -1
}
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   $connection = Connect-NcController -Name $cluster -Credential $credential -ErrorAction Stop
   $version    = $connection.Version.Split(":")[0].Split(" ")[2].Split(".")
}Catch{
   Write-Warning -Message $("Failed connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Exit -1
}
#'------------------------------------------------------------------------------
#'Set a variable to use the system API if the ONTAP version is < 9.13.
#'------------------------------------------------------------------------------
If(($version[0] -ge 9) -And ($version[1] -ge 13)){
   $useSystemApi = $False
}Else{
   $useSystemApi = $True
}
#'------------------------------------------------------------------------------
#'Update the snapshot directory access for the volume.
#'------------------------------------------------------------------------------
If(-Not($useSystemApi)){
   $query = Get-NcVol -Template
   Initialize-NcObjectProperty $query VolumeIdAttributes
   $query.VolumeIdAttributes.Name = $volume
   $query.VolumeIdAttributes.OwningVserverName = $vserver
   $attributes = Get-NcVol -Template
   $attributes.SnapshotDirectoryAccessEnabled = $True
   Try{
      Update-NcVol -Query $query -Attributes $attributes -ErrorAction Stop
      Write-Host "Updated snapshot directory access on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
   }Catch{
      Write-Warning -Message $("Failed updating snapshot directory access on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"". Error " + $_.Exception.Message)
      Exit -1
   }
   Exit 0
}Else{
   Write-Host $("The ONTAP version running on cluster ""$Cluster"" is """ +  ($Version -join ".") + """. Invoking System API")
}
#'------------------------------------------------------------------------------
$command = $("volume modify -vserver $Vserver -volume $Volume -snapdir-access " + $AccessEnabled.ToString().ToLower())
$cmd     = $command.Split(" ")
$api     = $("<system-cli><args><arg>" + ($cmd -join "</arg><arg>") + "</arg></args></system-cli>")
#'------------------------------------------------------------------------------
#'Invoke the API.
#'------------------------------------------------------------------------------
Try{
   $output = Invoke-NcSystemApi -Request $api -ErrorAction Stop
   Write-Host $("Executed Command`: " + $([String]::Join(" ", $command)))
   Write-Host "Updated snapshot directory access on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed Executed Command`: " + $([String]::Join(" ", $command)) + ". Error " + $_.Exception.Message)
   Write-Warning -Message "Failed updating snapshot directory access on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
   Exit -1
}
#'------------------------------------------------------------------------------

 

Hope that helps.

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Public