NetApp: Identify Snapshots Not Managed by Policy

The function below can be used to identify snapshots on a NetApp Cluster Mode system that do not have a corresponding snapshot schedule applied to the volume in question. It can be used to find odd snapshots that have been taken ad-hoc, and also to find snapshots that are left orphaned by changing the snapshot policy that has been applied to a volume.

<#
.Synopsis
   Returns details of snapshots on a volume which do not correspond with the currently assigned snapshot policy for the volume. 

.EXAMPLE
   Get-NcVol "volume1" | Get-NcSnapshotNotManagedByPolicy

.EXAMPLE
   Get-NcVol | Get-NcSnapshotNotManagedByPolicy

.EXAMPLE
   Get-NcVol "volume1" | Get-NcSnapshotNotManagedByPolicy | Remove-NcSnapshot

.INPUTS
   Array of DataONTAP.C.Types.Volume.VolumeAttributes

.OUTPUTS
   Array of DataONTAP.C.Types.Snapshot.SnapshotInfo
#>
function Get-NcSnapshotNotManagedByPolicy {
    Param (
        # Array of NetApp Volumes. Use output from Get-NcVol to populate this parameter.
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [ValidateNotNullOrEmpty()]
        [DataONTAP.C.Types.Volume.VolumeAttributes[]]$Volume
    )

    Begin {
        if ($global:CurrentNcController -eq $null) {
            Write-Error "Not currently connected to a NetApp Controller" -ErrorAction Stop
        }
        $InvalidSnaps = @()
    }

    Process {
        ForEach ($Vol in $Volume) {
            if ($Vol.State -ne "online") {
                Write-Verbose "Skipping volume $($Vol.Name) - not online"
                continue
            }

            if ($vol.VolumeStateAttributes.IsNodeRoot -eq $true) {
                Write-Verbose "Skipping volume $($Vol.Name) - node root"
                continue
            }

            if ($Vol.VolumeMirrorAttributes.IsDataProtectionMirror -eq $true -or $Vol.VolumeMirrorAttributes.IsLoadSharingMirror -eq $true -or $Vol.VolumeTransitionAttributes.IsCopiedForTransition -eq $true) {
                Write-Verbose "Skipping volume $($Vol.Name) - data protection mirror, load sharing mirror or transition mirror"
                continue
            }
    
            # Retrieve details about the volume, its snapshot policy and schedules
            $VolSnapshots= Get-NcSnapshot -Volume $Vol.Name -Vserver $Volume.Vserver
            $SnapPolicy = Get-NcSnapshotPolicy -Name $Vol.VolumeSnapshotAttributes.SnapshotPolicy
            $SnapSchedules =  Get-NcJobSchedule -Name $SnapPolicy.SnapshotPolicySchedules.Schedule
            
            # Check if a SnapMirror exists for the volume. If it does, then exclude the SnapMirror Snapshot
            $SnapMirrorDests = Get-NcSnapmirrorDestination -SourceVolume $Vol.Name
            if ($SnapMirrorDests -ne $null) { $VolSnapshots = $VolSnapshots| Where {$_.Dependency -ne "snapmirror"} }

            # Check for other forms of SnapMirror, such as Load Sharing Mirrors, and exclude
            $SMR = Get-NcSnapMirror -Template
            $SMR.RelationshipType = "*"
            $SMR.SourceVolume = $Vol.Name
            $SMRS = Get-NcSnapmirror -Query $SMR
            if ($SMRS -ne $null) { $VolSnapshots = $VolSnapshots | Where { $_.Dependency -ne "snapmirror" } }

            # Ignore anything with a dependency of 'busy'
            $VolSnapshots = $VolSnapshots | Where { $_.Dependency -ne "busy" }

            # Ignore Snapshots for SMVI, SnapManager SQL and SnapManager Exchange. We do not have SMVI backup job information, so must assume they are all being managed
            $VolSnapshots = $VolSnapshots | Where {$_.Name -notlike "smvi_*" -and $_.Name -notlike "sqlsnap_*" -and $_.Name -notlike "sqlinfo_*" -and $_.Name -notlike "exchsnap_*" -and $_.Name -notlike "eloginfo_*" }

            # Invalid snapshots are those which do not start with the schedulename set by the policy linked to the volume
            $UnmanagedSnaps = $VolSnapshots| Where { $SnapSchedules.JobScheduleName -notcontains $_.Name.Split(".")[0] }
            Write-Verbose "$($Vol.Name) has $(($UnmanagedSnaps | Measure).Count) unmanaged snapshots"
            
            $InvalidSnaps += $UnmanagedSnaps
        }
    }

    End {
        return $InvalidSnaps
    }
}

Once the function has been run inside a PowerShell session, it is simple to obtain details of unmanaged snapshots on either a single volume or all volumes in the cluster:

   Get-NcVol "volume1" | Get-NcSnapshotNotManagedByPolicy

   Get-NcVol | Get-NcSnapshotNotManagedByPolicy

If you are certain of the outcome and just want to clean all of the identified snapshots up then you can pipe the results out and remove all of the snapshots identified:

   Get-NcVol "volume1" | Get-NcSnapshotNotManagedByPolicy | Remove-NcSnapshot

The script ignores snapmirror destination volumes, and can detect and ignore snapshots used for snapmirrors, load sharing mirrors and snapvault (etc). SMVI, SME and SMS snapshots are ignored as they should be managed by the host system. If you spot a snapshot included that shouldn’t be in the list though, please let me know so that I can update the code.

Be the first to like.


Leave a Reply

Your email address will not be published. Required fields are marked *