Wednesday, 13 November 2013

Clustered ONTAP Snapshot Deletor Interactive

Here we present a PowerShell script which allows you to delete snapshots for volumes on your NetApp Clustered ONTAP clusters that are aged over a certain number of days. This is the interactive version and the next blog post presents a non-interactive version that could be run as a scheduled task/add-on script to an existing process.

CDOT Snapshot Deletion Interactive Script Using Cluster login
Copy the following into Notepad (or - even better - Notepad++) and save as say cdotSnapDeletor.ps1, then run using PowerShell:

## START COPYING HERE ##

## CDOT Snapshot Deletor (Interactive)
######################################
## The following script will:
##  prompt for a Cluster to connect to
##  prompt for Cluster user and password
##  prompt for Vserver
##  prompt for Volume
##  prompt for days worth of snapshots to keep
##  prompt for if you really want to delete snapshots
##  then cycle and repeat!
## Pre-requisites:
##  The Data ONTAP PowerShell Toolkit must be installed,
##  and PowerShell execution policy set accordingly (RemoteSigned will work.)

cls
Import-module DataOnTap

echo "CDOT SnapShot Deletor (Interactive)" ""

$confirm_do_loop3 = "y"

do {

$controller = Read-host "Please enter IP of cluster"
$user = Read-host "Please enter username"

echo "" "connecting ..." ""
Connect-NcController $controller -credential $user

echo "Vservers in this Cluster" ""
$list = Get-NcVserver
foreach ($line in $list){
 $vserver = $line.Vserver
 echo "$vserver"
}

$confirm_do_loop2 = "y"

do {

echo ""
$vserver = Read-host "Please enter Vserver"
$global:CurrentNcController.Vserver = $vserver

echo "" "Volumes in this Vserver:" ""
$list = Get-NcVol
foreach ($line in $list){
 $volName = $line.name
 echo "$volName"
}

$confirm_do_loop1 = "y"

do {

echo ""
$volume = Read-host "Please enter volume your want to check for snapshots"
$daysWorth = Read-host "How many days worth of snapshots do you want to keep"

$snapCount = (Get-NcSnapshot -Volume $volume | measure).count
echo "" "This volume has $snapCount snapshots"

echo "The following snapshots are older than $daysWorth days:" ""
$list = Get-NcVol -Name $volume | Get-NcSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-$daysWorth)}
foreach ($line in $list){
 $snapName = $line
 echo "$snapName"
}

echo ""
$confirm_delete = Read-host "Are you sure you want to delete these snapshots (y/n)"
echo ""

if ($confirm_delete -eq "y") {

foreach ($line in $list){
 $snapName = $line
 echo "Deleting $snapName"
 Remove-NcSnapshot -Volume $volume -Snapshot $snapName -confirm:$false
 }
 echo "" "Snapshots deleted!"
}

$confirm_do_loop1 = Read-host "Do you want to check more Volumes (y/n)"

} until ($confirm_do_loop1 -ne "y")

$confirm_do_loop2 = Read-host "Do you want to check more Vservers (y/n)"

} until ($confirm_do_loop2 -ne "y")

$confirm_do_loop3 = Read-host "Do you want to check more Clusters (y/n)"

} until ($confirm_do_loop3 -ne "y")

## STOP COPYING HERE ##

Image: An Example of the CDOT Snap Deletor Script in Action

No comments:

Post a Comment