是否有一些代码可以创建文件共享快照并在使用 Azure Automation 指定时间后删除其中的一些快照?

Is there some codes can create file share snapshots and delete some of them after appoint time with Azure Automation?

我想每天、每周、每月获取一些快照,我可以决定快照的保留时间吗? (比如保持每日快照 10 天,每周快照 35 天,每月快照 13 个月)。

我从别人那里得到了一些建议,但它不能实现我的目标,我想得到更多的建议或一些解决方案来实现目标。

$context = New-AzureStorageContext -StorageAccountName  -StorageAccountKey 
$share = Get-AzureStorageShare -Context $context -Name $filesharename
$s = $share.snapshot()
$s2= $s.SnapshotQualifiedStorageUri.PrimaryUri.ToString()
$snapshottime = $s2.Substring($s2.IndexOf('=')+1)

write-output "create a snapshot"

write-output $snapshottime

start-sleep -s 180

write-output "delete the snapshot"

$snap = Get-AzureStorageShare -Context $context -SnapshotTime
$snapshottime -Name $filesharename
$snap.Delete()

write-output "deleted successfully after 3 minutes"

我想要保留三种不同类型快照(每日、每月、每周)的输出。 但是上面的代码输出不能满足我的目标。 下面有一个例外: 该作业被逐出并随后进入停止状态。作业无法继续 运行。

12 月 21 日更新:

在runbook_2中,用于删除指定时间的daily/weekly/monthly个快照。

代码如下:

$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"
}

}