Quantcast
Channel: NetApp – The Practical Administrator
Viewing all articles
Browse latest Browse all 16

NetApp PowerShell Toolkit 101: Volume Snapshots

$
0
0

Snapshots are one of the core features of ONTAP, and something that many, many people rely on every day to protect their data from accidental (or malicious…) deletion and corruption. The NetApp PowerShell Toolkit can help us to manage the configuration of snapshot policies, the application of those policies to volumes, and creating/deleting/reverting snapshots too.

This post will cover:

  • Snapshots
    • Management
    • Reporting
    • Snap Reserve
  • Snapshot Policies
  • Snapshot Autodelete
  • Recovering Data

Snapshots

Managing Snapshots

  • Show snaps for a volume
    Get-NcVol $volName | Get-NcSnapshot
  • Create a snapshot
    Get-NcVol $volName | New-NcSnapshot $snapName
  • Delete a snapshot
    # delete a specific snapshot
    Get-NcVol $volName | Get-NcSnapshot $snapName | Remove-NcSnapshot
    
    # delete all snapshots for a volume
    Get-NcVol $volName | Get-NcSnapshot | Remove-NcSnapshot
    
    # delete all snapshots, for all volumes, which match a name pattern
    $pattern = "weekly"
    Get-NcSnapshot | ?{ $_.Name -match $pattern } | Remove-NcSnapshot

Snapshot Reporting

  • Show volumes with no snapshot protection
    I originally created this snippet as a response to this NetApp Communities question. It returns non-root data volumes (not data protection volumes) which have the volume option nosnap enabled or have a snapshot policy of none.

    Get-NcVol | ?{ 
        # get non-root volumes
        $_.VolumeStateAttributes.IsNodeRoot -eq $false `
        -and
            # which are rw (this will exclude SnapMirror, etc.)
            $_.VolumeIdAttributes.Type -eq "rw" `
        -and 
        (
            # with "nosnap" turned on
            (($_ | Get-NcVolOption -Hashtable).value.nosnap -eq "on") `
            -or 
            # or with snapshot policy set to none
            ($_.VolumeSnapshotAttributes.SnapshotPolicy -eq "none") 
        )
    }

  • Show the oldest snapshot for a volume

    Get-NcVol $volumeName| Get-NcSnapshot | `
      Sort-Object -Property Created | Select-Object -First 1

  • Show snapshots more than X days old

    $daysAgo = 14
    
    Get-NcSnapshot | Where-Object {
        # multiply the days by -1 to go backward.  if the value was
        # positive it would be in the future
        $_.Created -lt ((Get-Date).AddDays($daysAgo * -1))
    }

  • Show the cumulative snapshot usage for one (or more) volumes

    # single volume
    (Get-NcVol $volumeName).VolumeSpaceAttributes.SizeUsedBySnapshots | `
      ConvertTo-FormattedNumber
    
    # total snapshot space used for all volumes for a particular SVM
    ((Get-NcVserver $svmName | Get-NcVol).VolumeSpaceAttributes.SizeUsedBySnapshots | `
      Measure-Object -Sum).Sum | ConvertTo-FormattedNumber

  • Show volumes with a dependent/busy snapshot

    Get-NcSnapshot | ?{ $_.Dependency -ne $null }

Snap Reserve

Snap reserve is the amount of space in the volume which has been set aside for snapshot data…i.e. the data which is changed. The size of the snapshot is “contained” in this capacity, and not deducted from the available space in the volume.

  • Show snap reserve for a volume

    Get-NcVol $volName | Get-NcSnapshotReserve

  • Set the snap reserve for a volume

    Get-NcVol $volName | Set-NcSnapshotReserve -Percentage 10

  • Show volumes with no snap reserve

    # using a query against volumes
    Get-NcVol -Query @{ VolumeSpaceAttributes = @{ SnapshotReserveSize = 0 } }
    
    # or, using the snap reserve cmdlet
    Get-NcSnapshotReserve | ?{ $_.Percentage -eq 0 }

  • Show volumes with snap reserve > X percent
    # the percentage threshold
    $percent = 5
    
    # using a query
    Get-NcVol -Query @{ VolumeSpaceAttributes = @{ PercentageSnapshotReserve = ">$($percent)" } }
    
    # using the snap reserve cmdlet
    Get-NcSnapshotReserve | ?{ $_.Percentage -gt $percent }
  • Show volumes with snapshots exceeding the snap reserve

    Get-NcVol | Where-Object {
        $_.VolumeSpaceAttributes.SizeUsedBySnapshots -gt $_.VolumeSpaceAttributes.SnapshotReserveSize
    }

Managing Snapshot Policies

The snapshot policy is what determines when the ONTAP system will automatically create a snapshot and how long to retain it.

  • Show snapshot policy for all volumes

    Get-NcVol | Select-Object @{N="Name"; E={ $_.Name }},@{N="Snapshot Policy";E={ $_.VolumeSnapshotAttributes.SnapshotPolicy }}

  • Show volumes with a particular policy

    Get-NcVol -Query @{VolumeSnapshotAttributes=@{SnapshotPolicy=$policyName}}

  • Create a policy with a custom schedule

    #
    # create custom cron schedule(s) for the policy
    #
    
    # snapshot every two hours
    Add-NcJobCronSchedule -Name c2hour -Hour 0,2,4,6,8,10,12,14,16,18,20,22
    
    # snapshot every day at midnight
    Add-NcJobCronSchedule -Name cDaily -Day -1 -hour 0
    
    # snapshot every Sunday at midnight
    Add-NcJobCronSchedule -Name cWeekly -DayOfWeek 6
    
    # snapshot every month, on the first, at midnight
    Add-NcJobCronSchedule -Name cMonthly -Month -1 -Day 1
    
    # snapshot every year, January first at midnight
    Add-NcJobCronSchedule -Name cYearly -Month 0 -Day 1 -Hour 0 -Minute 0
    
    #
    # create the snapshot policy, add the first schedule, keeping twelve
    # bi-hourly snapshots (one day's worth)
    #
    New-NcSnapshotPolicy -Name Gold -Schedule c2hour -Count 12
    
    #
    # add the remaining schedules to complete the policy
    #
    
    # keep seven daily snapshots
    Add-NcSnapshotPolicySchedule -Name Gold -Schedule cDaily -Count 7
    
    # keep four weekly snapshots
    Add-NcSnapshotPolicySchedule -Name Gold -Schedule cWeekly -Count 4
    
    # keep one yearly snapshot
    Add-NcSnapshotPolicySchedule -Name Gold -Schedule cYearly -Count 1

  • Change the snapshot policy for a volume
    $query = @{
        Name = $volName
    }
    
    $attributes = @{
        VolumeSnapshotAttributes = @{
            SnapshotPolicy = $policyName
        }
    }
    
    Update-NcVol -Query $query -Attributes $attributes

Managing Snapshot AutoDelete

Snapshot AutoDelete is a protection mechanism, meant to prevent your volume from running out of space from oversized snapshots. There are a number of settings associated with AutoDelete. The names used for the ClusterShell and from the PowerShell toolkit are slightly different, I’ve noted the differences below.

  • CLI = Enabled, PSTK = statetrue/false (CLI) or on/off (PSTK), this indicates whether AutoDelete is enabled for the volume.
  • Commitment – How aggressive should AutoDelete be when removing snapshots? try = only delete snapshots which are not “in use” or locked by FlexClone, SnapMirror, etc. disrupt = will allow AutoDelete to remove data protection (SnapMirror, etc.) snapshots. destroy = will allow AutoDelete to remove snapshots used by FlexClone.
  • Trigger – What causes the AutoDelete action to kick off? volume = when volume capacity crosses the (configurable) threshold. snap_reserve = when the snap reserve is nearly full. space_reserve = when the reserved space in the volume is nearly full.
  • Target Free Space – AutoDelete will stop deleting snapshots when free space reaches this percentage.
  • Delete Ordernewest_first or oldest_first, the order which snapshots will be deleted. Generally speaking, oldest_first will result in the most space reclaimed.
  • Defer Deletescheduled = delete snapshots taken by the snapshot policy last. user_created = delete user created snapshots last. prefix = delete snapshots with the specified prefix last. none = don’t defer any, just delete in the specified delete order.
  • CLI = Defer Delete Prefix, PSTK = prefix – The prefix used when the defer delete value is prefix.
  • Destroy List – This list of services which can be destroyed if the backing snapshot is removed. This is the corollary for the commitment value of destroy The default is none, which is the safest option. Refer to the documentation for specifics.

With an understanding of the options, let’s look at how to query and modify AutoDelete settings.

  • Show the AutoDelete policy for a volume
    Get-NcVol $volName | Get-NcSnapshotAutodelete
  • Show all volumes with autodelete enabled/disabled
    # setting IsAutodeleteEnabled to $true will show volumes with autodelete enabled,
    # setting to $false will show volumes with autodelete disabled
    Get-NcVol -Query @{ VolumeSnapshotAutodeleteAttributes = @{ IsAutodeleteEnabled = $true } }
  • Enable/disable for a volume
    # enable
    Get-NcVol $volName | Set-NcSnapshotAutodelete -Key state -value on
    
    # disable
    Get-NcVol $volName | Set-NcSnapshotAutodelete -Key state -value off
  • Set multiple options for a volume
    $options = @{
        'commitment' = 'try';
        'defer_delete' = 'scheduled';
        'delete_order' = 'oldest_first';
        'state' = 'on'
        'target_free_space' = 20;
        'trigger' = 'volume';
    }
    
    Get-NcVserver $svmName | Get-NcVol | %{
        $volume = $_
    
        $options.GetEnumerator() | %{
            $volume | Set-NcSnapshotAutodelete -Key $_.Key -Value $_.Value
        }
    }

Recovering Data

Revert a snapshot

# note that if you revert a node's root volume
# it will cause the node to reboot
Get-NcVol $volumeName | Restore-NcSnapshotVolume -SnapName $snapName

Restore a file using FlexClone

I originally posted a version of this to the NetApp Communities site. This will create a FlexClone of the file from a snapshot into the current file system. Note that this is not the same thing as a single file snap restore, which uses the Restore-NcSnapshotFile cmdlet.

$svmName = "mySVM"

$volumeName = "myFavoriteDatastore"
$sourceSnap = "weekly.0"

$files = @("vc.vmx", "vc.vmdk", "vc.vmdk-flat")
$sourceFolder = "/vc/"
$destinationFolder = "/vc_restored/"

$files | %{ 
    $splat = @{
        # the name of the volume which holds the file(s)
        'Volume' = $volumeName;

        # the path to the file in the volume
        'SourcePath' = "$($sourceFolder)$($_)";

        # the path for the restored file
        'DestinationPath' = "$($destinationFolder)$($_)";

        # if false, the clone will be thin.  if omitted, the
        # policy will be inherited from the original
        'SpaceReserved' = $false;

        # the source snapshot
        'Snapshot' = $sourceSnap;

        # this command must be targeted at a SVM
        'VserverContext' = $svmName;
    }

    New-NcClone @splat
}

The post NetApp PowerShell Toolkit 101: Volume Snapshots appeared first on The Practical Administrator.


Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>