I want to set the retention period of Files Share snapshot with scheduler by Azure Automation. 1, May I set the schedule of creating and deleting the snapshot at one runbook. (like create one snapshot at today and delete it after one month). 2, I want to get some snapshots by every day, week and month, can I decide the retain time. (Like keep the daily snapshot 15 days, the weekly snapshot 35days, and the Monthly snapshot 13months).
3, If it is so difficult, may I decide the snapshots that I want to delete, like the previous 10. If you know something about that, please write some comments below, thanks so much.
Some information for you to refer.
Try the command below to create snapshot, for its operating frequency, you could create a schedule for the runbook.
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$SAResourceGroupName="joywebapp"
$StorageAccountName="joystoragev2"
$StorageAccountKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $SAResourceGroupName -AccountName $StorageAccountName).Value[1]
$context=New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
$share = Get-AzureStorageShare -Context $context -Name "111"
$snapshot = $share.Snapshot()
Delete the snapshots created before one month:
$allsnapshots = Get-AzureStorageShare -Context $context | Where-Object { $_.Name -eq "111" -and $_.IsSnapshot -eq $true }
foreach($snapshot in $allsnapshots){
if($snapshot.SnapshotTime -lt (get-date).AddMonths(-1)){
$snapshot.Delete()
}
}