Skip to content

Commit

Permalink
Create Delete Snapshots after X Days
Browse files Browse the repository at this point in the history
  • Loading branch information
Athreyasubash authored Jan 23, 2024
1 parent c5ca47a commit 7e112ae
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions NFS Snapshot Management/Delete Snapshots after X Days
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
##PowerShell Script to delete snapshots

Connect-AzAccount
Set-AzContext -Subscription "Subscription Name"

$resourceGroupName = "Resouce group Name"
$accountName = "Storage account name"
$shareName = "Share Name"

$snapshotLifetimeInDays = "15"
#$snapshotLifetimeInMinutes = ""
$whatIf = $false

$CurrentDateTime = (Get-Date).ToUniversalTime()
$DateTimeThreshold = $CurrentDateTime.AddDays(-$snapshotLifetimeInDays)
#$DateTimeThreshold = $CurrentDateTime.AddMinutes(-$snapshotLifetimeInMinutes)

Write-Host "Querying all snapshots for share '$shareName'"
$snapshotList = (Get-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -IncludeSnapshot) | Where-Object { $_.Name -eq $shareName -and $_.snapshotTime -ne $null }
$snapshotList

Write-Host "Current date/time is $CurrentDateTime. Removing share snapshots older than '$DateTimeThreshold'"
foreach ($snapshot in $snapshotList) {
if ($snapshot.SnapshotTime -lt $DateTimeThreshold) {
Write-Host "Removing snapshot '$($snapshot.snapshotTime)' of share '$($snapshot.Name)'"
if ($whatIf -ne $true) {
Remove-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -Name $snapshot.Name -snapshotTime $snapshot.snapshotTime -Force
}
} else {
Write-Host "Retaining recent snapshot '$($snapshot.snapshotTime)' of share '$($snapshot.Name)'"
}
}

Write-Host "Querying all snapshots for share '$shareName' after deletion of old snapshots"
$snapshotListNew = (Get-AzRmStorageShare -ResourceGroupName $resourcegroupname -StorageAccountName $accountName -IncludeSnapshot)
$snapshotListNew

0 comments on commit 7e112ae

Please sign in to comment.