Обновление 12/21:
В runbook_2, который используется для удаления ежедневных / еженедельных / ежемесячных снимков в указанное время.
Код, как показано ниже:
$username = 'xxx'
$password ='xxx'
$filesharename ='xxx'
$context = New-AzureStorageContext -StorageAccountName $username -StorageAccountKey $password
#get all the snapshots for the fileshare
$snap = Get-AzureStorageShare -Context $context | where { ($_.Name -eq $filesharename) -and ($_.IsSnapshot -eq $true)}
# keep the daily snapshot 10 days, the weekly snapshot 35days, and the Monthly snapshot 13months
# snapshot creation -> A: daily(6 am) B: Week(Every Sat, 7 am) C: Month(The first day of the months, 8 am)
$date_today=[System.DateTime]::UtcNow #get the current UTC time, in this format: Friday, December 21, 2018 2:16:41 AM
write-host "today is: $date_today"
for($i=0;$i -lt $snap.length;$i++) # loop all the snapshots for the specified azure fileshare
{
#delete the daily created snapshot after 10 days
#the condition in the if: first one means if (today's date - snapshottime) is greater or more than 10 days(or 35 days, or 13 months);
#the condition in the if: second one means if the snapshottime's hour is equal to 6(or 7, or 8)
#if both of the 2 conditions are true, then it will delete daily / weekly / monthly snapshots respectively
if( ($snap[$i].SnapshotTime -le $date_today.adddays(-10)) -and ($snap[$i].SnapshotTime.hour -eq 6))
{
#delete the daily snapshots greater than or equal to 10 days
#your code here: $snap[$i].Delete()
write-host "$snap[$i] is deleted."
}
#delete the weekly created snapshot after 35 days
elseif( ($snap[$i].SnapshotTime -le $date_today.adddays(-35)) -and ($snap[$i].SnapshotTime.hour -eq 7))
{
#your code here: $snap[$i].Delete()
write-host "$snap[$i] is deleted."
}
#delete the monthly created snapshot after 13 months
elseif( ($snap[$i].SnapshotTime -le $date_today.AddMonths(-13)) -and ($snap[$i].SnapshotTime.hour -eq 8))
{
#your code here: $snap[$i].Delete()
write-host "$snap[$i] is deleted."
}
else
{
write-host "no snapshot is deleted."
}
}
В runbook_2 удалите снимки за промежуток времени:
$username = 'xxx'
$password ='xxx'
$filesharename ='xxx'
$context = New-AzureStorageContext -StorageAccountName $username -StorageAccountKey $password
$snap = Get-AzureStorageShare -Context $context | where { ($_.Name -eq $filesharename) -and ($_.IsSnapshot -eq $true)}
#delete the snapshots int a time range, like in 12/17 and 12/18
# assume today is 12/20, I want to delete the snapshots created like this: snapshot_create_time >= 3_day_ago and snapshot_create_time < 1_day_ago .
$date_today=([datetime]::utcnow).date
write-host "today is:"
write-host $date_today
for($i=0;$i -lt $snap.Length;$i++)
{
if(($snap[$i].SnapshotTime.Date -ge $date_today.adddays(-3)) -and ($snap[$i].SnapshotTime.Date -lt $date_today.adddays(-1)))
{
write-host "the filtered snapshot"
write-host $snap[$i].SnapshotQualifiedStorageUri
$snap[$i].Delete()
write-host "delete the snapshot"
}
}