Wednesday, 13 November 2013

Clustered ONTAP Snapshot Deletor Non-Interactive

Update: See http://www.cosonok.com/2013/11/update-cdot-snapshot-deletor-powershell.html for a slightly more advanced script (with logging, email reporting, ...)

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 non-interactive version that might be run as a scheduled task/add-on script to an existing process. The previous post contained the interactive version.

The first PowerShell script is required to make an encrypted password string.
The remaining two powershell scripts present connecting via a Cluster admin credential or a Cluster admin credential (there’s very little difference to the scripts.)

Set Credentials Script
Copy the following into Notepad (or - even better - Notepad++) and save as say set-cred.ps1 and then run using PowerShell:

## START COPYING HERE ##

## Set Credentials Script
#########################
## Creates a text file with encrypted password string
## Note: A blank file c:\scripts\encrypted_password.txt must be created first

$credential = Get-Credential
$credential.Password | ConvertFrom-SecureString | Set-Content c:\scripts\encrypted_password.txt

## STOP COPYING HERE ##

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

## START COPYING HERE ##

## CDOT Snapshot Deletor (Non-interactive using Cluster login)
##############################################################
## For the Cluster, Vserver and Volumes specified, it will delete snapshots older than the specified days
## Pre-requisites:
## The file c:\scripts\encrypted_password.txt must have be created first using set-cred.ps1
## Additionally the Data ONTAP PowerShell Toolkit must be installed,
##  and PowerShell execution policy set accordingly (RemoteSigned will work.)

## Section 1: Set Up Variables
## Enter the following variables/lists: $cluster,$username,$vserver,$volumes,$daysWorth

$cluster = "192.168.168.40"
$username = "admin"
$vserver = "vs1"
$volumes = "testshare", "testvol2"
$daysWorth = 7

$encrypted = Get-Content c:\scripts\encrypted_password.txt | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $encrypted)

## Section 2: Removing Snapshots
## Note: If you want to test, hash the Remove-NcSnapshot

Import-Module DataOnTap
Connect-NcController $cluster -Credential $credential -Vserver $vserver

echo "The following snapshots are older than $daysWorth days and are being deleted!" ""

foreach ($volume in $volumes){

echo "" "Volume=$volume" ""
$list = Get-NcVol -Name $volume | Get-NcSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-$daysWorth)}

foreach ($line in $list){

 $snapName = $line
 Remove-NcSnapshot -Volume $volume -Snapshot $snapName -confirm:$false
 echo "$snapName"

 }

}

## STOP COPYING HERE ##

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

## START COPYING HERE ##

## CDOT Snapshot Deletor (Non-interactive using Vserver login)
##############################################################
## For the Cluster, Vserver and Volumes specified, it will delete snapshots older than the specified days
## Pre-requisites:
## The file c:\scripts\encrypted_password.txt must have be created first using set-cred.ps1
## Additionally the Data ONTAP PowerShell Toolkit must be installed,
##  and PowerShell execution policy set accordingly (RemoteSigned will work.)

## Section 1: Set Up Variables
## Enter the following variables/lists: $cluster,$username,$vserver,$volumes,$daysWorth

$vserver = "192.168.168.20"
$username = "vsadmin"
$volumes = "testshare", "testvol2"
$daysWorth = 7

$encrypted = Get-Content c:\scripts\encrypted_password.txt | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($username, $encrypted)

## Section 2: Removing Snapshots
## Note: If you want to test, hash the Remove-NcSnapshot

Import-Module DataOnTap
Connect-NcController $vserver -Credential $credential

echo "The following snapshots are older than $daysWorth days and will be deleted!" ""

foreach ($volume in $volumes){

echo "" "Volume=$volume" ""
$list = Get-NcVol -Name $volume | Get-NcSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-$daysWorth)}

foreach ($line in $list){

 $snapName = $line
 Remove-NcSnapshot -Volume $volume -Snapshot $snapName -confirm:$false
 echo "$snapName"

 }

}

## STOP COPYING HERE ##

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

1 comment:

  1. I really appreciate this journal and that i can positive promote this journal to others in my circle.
    1337x UK proxy

    ReplyDelete