Существуют ли какие-либо коды, позволяющие создавать моментальные снимки общего файла и удалять некоторые из них после назначенного времени с помощью Azure Automation? - PullRequest
0 голосов
/ 20 декабря 2018

Я хочу получать снимки по дням, неделям и месяцам, могу ли я определить время хранения снимков.(Например, сохранить ежедневный снимок на 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"

Я хочу, чтобы выходные данные сохраняли три разных типа снимков (ежедневно, ежемесячно, еженедельно).но выходные данные кодов не могут соответствовать моей цели.Ниже приведено исключение: задание было выселено и впоследствии достигло состояния остановки.Задание не может быть продолжено.

1 Ответ

0 голосов
/ 20 декабря 2018

Обновление 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"
}

}
...