ONTAP Discussions

ontap manual snapshost expiry time

kbhonagiri
157 Views

Hi Everyone,

 

I have created a snapshot from Ontap API with expiry time and it is not getting deleted automatically after the expire time. is this expected ? How can we make sure it is deleted after expiry time?

 

Kalyan

1 ACCEPTED SOLUTION

mbeattie
79 Views

Hi Kalyan,

 

Here is an example of automating the deletion of a manual snapshot based on the desired retention in seconds:

 

Param(
   [Parameter(Mandatory = $True, HelpMessage = "The Cluster Hostname, FQDN 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 Snapshot name")]
   [String]$Snapshot,
   [Parameter(Mandatory = $True, HelpMessage = "The Total number of seconds to retain the snapshot before deletion")]
   [Int]$Retention,
   [Parameter(Mandatory = $True, HelpMessage = "The Credential to authenticate to ONTAP")]
   [System.Management.Automation.PSCredential]$Credential
)
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $Cluster -Credential $Credential | Out-Null
   Write-Host "Connect to cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed Connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Exit -1
}
#'------------------------------------------------------------------------------
#'Enumerate the Snapshot by name.
#'------------------------------------------------------------------------------
Try{
   $s = Get-NcSnapshot -Volume $Volume -SnapName $Snapshot -Vserver $Vserver -ErrorAction Stop
   Write-Host "Enumerated snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed enumerating Snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"". Error " + $_.Exception.Message)
   Exit -1
}
If($Null -eq $s){
   Write-Warning -Message "The Snapshot ""$Snapshot"" was not found on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
   Exit 0
}
$timestamp   = Get-Date -Date $s.AccessTimeDT
$snapshotAge = New-TimeSpan -Start $timestamp -End (Get-Date)
#'------------------------------------------------------------------------------
#'Delete the expired snapshot based on it's age and naming prefix.
#'------------------------------------------------------------------------------
If(($Null -ne $s) -And ($snapshotAge.TotalSeconds -ge $retention)){
   $age = $snapshotAge.TotalSeconds
   Try{
      Remove-NcSnapshot -Volume $volume -Snapshot $Snapshot -VserverContext $Vserver -Confirm:$False -ErrorAction Stop
      Write-Host "Snapshot Age in Seconds: $age. Snapshot Retention in Seconds`: $Retention"
      Write-Host "Deleted expired snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
   }Catch{
      Write-Warning -Message $("Failed deleting expired snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"". Error " + $_.Exception.Message)
      Exit -1
   }
}Else{
   Write-Host $("Snapshot Expired: False. Vserver: $vserver. Volume: $volume. Snapshot: $Snapshot. Snapshot Age Total Seconds:" + $snapshotAge.TotalSeconds)
}
#'------------------------------------------------------------------------------

Hope that helps

 

/Matt

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

View solution in original post

5 REPLIES 5

mbeattie
92 Views

Hi Kalyan,

 

The -expiry-time snapshot parameter is for snaplock.

 

 

 

[-expiry-time {MM/DD/YYYY HH:MM:SS [{+|-}hh:mm] | infinite}] - SnapLock Expiry Time

    Specifies the new snaplock expiry that is applied to Snapshot copy locked by SnapLock.

 

 

 

When using SnapLock volumes, snapshots can't be deleted until their retention period has expired. If you are not using SnapLock then manually created snapshots must be manually deleted (although that process could be automated).

 

What process are you trying to achieve via the API? Assuming you are creating a manual snapshot for some purposes then having some automation that schedules the deletion of that snapshot after X hours, days etc, then i would recommend your code compares the date difference of the snapshot creation time to the current time, if greater than or equal to Y then delete the snapshot.

 

/Matt

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

kbhonagiri
87 Views

This is how im creating snapshot with url = "https://" + hostname + "/api/storage/volumes/" + src_vol_uuid + "/snapshots"

payload = {
"name": snapshot_name,
"expiry_time": expiry_time_iso
}

  . How do i enable snapshot deletion automatically other than me writing another automation schedule to cleanup ?

mbeattie
80 Views

Hi Kalyan

 

ONTAP handles automatic snapshot deletion based on a snapshot schedule applied to a volume which then automatically deletes older snapshots for you.

 

When you create a manual snapshot it must also be deleted manually (though you can automate that process to clean them up). I recommend doing a date diff\timespan between the snapshot creation time and the current time. If the snapshot age in total seconds is greater than or equal to the desired retention then delete the snapshot

 

/Matt

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

mbeattie
80 Views

Hi Kalyan,

 

Here is an example of automating the deletion of a manual snapshot based on the desired retention in seconds:

 

Param(
   [Parameter(Mandatory = $True, HelpMessage = "The Cluster Hostname, FQDN 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 Snapshot name")]
   [String]$Snapshot,
   [Parameter(Mandatory = $True, HelpMessage = "The Total number of seconds to retain the snapshot before deletion")]
   [Int]$Retention,
   [Parameter(Mandatory = $True, HelpMessage = "The Credential to authenticate to ONTAP")]
   [System.Management.Automation.PSCredential]$Credential
)
#'------------------------------------------------------------------------------
#'Connect to the cluster.
#'------------------------------------------------------------------------------
Try{
   Connect-NcController -Name $Cluster -Credential $Credential | Out-Null
   Write-Host "Connect to cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed Connecting to cluster ""$Cluster"". Error " + $_.Exception.Message)
   Exit -1
}
#'------------------------------------------------------------------------------
#'Enumerate the Snapshot by name.
#'------------------------------------------------------------------------------
Try{
   $s = Get-NcSnapshot -Volume $Volume -SnapName $Snapshot -Vserver $Vserver -ErrorAction Stop
   Write-Host "Enumerated snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
}Catch{
   Write-Warning -Message $("Failed enumerating Snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"". Error " + $_.Exception.Message)
   Exit -1
}
If($Null -eq $s){
   Write-Warning -Message "The Snapshot ""$Snapshot"" was not found on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
   Exit 0
}
$timestamp   = Get-Date -Date $s.AccessTimeDT
$snapshotAge = New-TimeSpan -Start $timestamp -End (Get-Date)
#'------------------------------------------------------------------------------
#'Delete the expired snapshot based on it's age and naming prefix.
#'------------------------------------------------------------------------------
If(($Null -ne $s) -And ($snapshotAge.TotalSeconds -ge $retention)){
   $age = $snapshotAge.TotalSeconds
   Try{
      Remove-NcSnapshot -Volume $volume -Snapshot $Snapshot -VserverContext $Vserver -Confirm:$False -ErrorAction Stop
      Write-Host "Snapshot Age in Seconds: $age. Snapshot Retention in Seconds`: $Retention"
      Write-Host "Deleted expired snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"""
   }Catch{
      Write-Warning -Message $("Failed deleting expired snapshot ""$Snapshot"" on volume ""$Volume"" on vserver ""$Vserver"" on cluster ""$Cluster"". Error " + $_.Exception.Message)
      Exit -1
   }
}Else{
   Write-Host $("Snapshot Expired: False. Vserver: $vserver. Volume: $volume. Snapshot: $Snapshot. Snapshot Age Total Seconds:" + $snapshotAge.TotalSeconds)
}
#'------------------------------------------------------------------------------

Hope that helps

 

/Matt

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

kbhonagiri
75 Views

Thank you for your quick assistance, this helps

Public