0

I want to get some snapshots by every day, week and month, can I decide the retention time of the snapshots. (Like keep the daily snapshot 10 days, the weekly snapshot 35days, and the Monthly snapshot 13months).

I had got some advice from someone else but it can't achieve my goal, I want to get more advice or some solutions to achieve the goal.

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

I want the output of keep three different types of snapshots(daily, monthly, weekly). but the codes upside's output can't meet my goal. And there has an Exception below: The job was evicted and subsequently reached a Stopped state. The job cannot continue running.

Arthur
  • 103
  • 11
  • and one thing need to confirm, the daily / weekly / monthly snapshots are all operated in the same one fileshare? – Ivan Glasenberg Dec 20 '18 at 07:50
  • I just figure out something may help achieve the goal, in your schedule, please let me know the date/time of the daily / week / month. Then I can take a try. – Ivan Glasenberg Dec 20 '18 at 08:14
  • @Ivan Yang 1, yes, the same fileshare. 2, I test for the schedules (A: create once an hour and delete after 2 hours. B: Create every two hours and delete after 4 hours. C: Create every 3 hours and delete after 6 hours) for the test. – Arthur Dec 20 '18 at 08:23
  • I mean in the real environment, date/time of the daily / week / month. – Ivan Glasenberg Dec 20 '18 at 08:26
  • Sorry, for A: daily(6 am) B: Week(Every Sat, 6 am) C: Month(The first day of the months, 6 am), it this okay? Thanks – Arthur Dec 20 '18 at 08:30
  • I want to use the time to distinguish these snapshots, if they are are created in 6 am, it's not simple to know which one is created daily, weekly,monthly because some of them will be created in one day. can you consider change the time for some of them? like week(7 am), month(8 am)? – Ivan Glasenberg Dec 20 '18 at 08:34
  • Sorry, I made a mistake. Week(7 am), month(8 am), please use this time. – Arthur Dec 20 '18 at 08:37
  • I'll take a try and update you tomorrow :) – Ivan Glasenberg Dec 20 '18 at 08:38
  • hi Arthur, I update my code below. Theoretically it should work when combined with runbook_1. You can make some changes for your test. – Ivan Glasenberg Dec 21 '18 at 02:44
  • Hello, thanks so much. I will test it and connect you later, thanks.@ Ivan Yang – Arthur Dec 21 '18 at 04:10
  • remember that, you need adjust something, like the retention days(10days,35days,13months are very long in my code), as well as the snapshot creation time(I hard coded it here 6:00,7:00,8:00 and not specify if it's am or pm, you can adjust it for your test. you can schedule the runbook_1 to create them all in pm, or am). Please let me if any issue or if you don't know how to adjust it for test. – Ivan Glasenberg Dec 21 '18 at 04:16
  • @IvanYang, just now I tested the Runbook1, and there has an error about 'Get-AzureStorageShare : The remote server returned an error: (403) Forbidden. HTTP Status Code: 403 - HTTP Error Message: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature. + $snap = Get-AzureStorageShare -Context $context | where { ($_.Name -e ... + FullyQualifiedErrorId : StorageException,Microsoft.WindowsAzure.Commands.Storage.File.Cmdlet.GetAzureStorageShare' . Do you know the reason, thanks? – Arthur Dec 21 '18 at 04:52
  • For 2, you can delete the comment and put your own code there. For 1, do you use your own account name and key and the fileshare name? you test the connection code in your local powershell directly first, to make sure it can connect to your azure storage: the 4 lines of code from $username="" until to $snap = Get-AzureStorageShare -Context $context | where { ($_.Name -eq $filesharename) -and ($_.IsSnapshot -eq $true)} – Ivan Glasenberg Dec 21 '18 at 05:21
  • @IvanYang For 2, I am sorry to say that but I can't understand it very clearly, should I put some code at there? I used your "$snap[$i].Delete()" and find it can work well. Also, I have a question about your comment "#delete the daily created snapshot after 10 days", as my test, I think this may be "#delete the daily created snapshot before 10 days from now ([System.DateTime]::UtcNow)". Is it right or I make a misunderstanding. Thanks – Arthur Dec 21 '18 at 09:36
  • Arthur, sorry for the making you confused. For 2, you should put the code "$snap[$i].Delete()" there (just ignore my comment). For your another question, yes, you're right: it's 10 days before now. And if any issue, please let me know. If it's can work well, please help mark it :) . thanks. – Ivan Glasenberg Dec 21 '18 at 13:15
  • Hi Arthur, does it work? – Ivan Glasenberg Dec 25 '18 at 05:06
  • @IvanYang Now I am testing yet, the delete sounds can't work very well (sometimes it can't delete all of the snapshots which I appointed,) now I try to change the schedules to test it, if it can work well, I will tell you and mark your answer. By the way, I didn't see the write-host output, do you know the reason? thanks – Arthur Dec 26 '18 at 03:00
  • Until the runbook completed, you can see the output message. I think it should delete the appointed snapshots as I test at my side. If you still have the delete issue, please feel free to post your code in your question and I will take a look :) – Ivan Glasenberg Dec 26 '18 at 03:17
  • Thanks so much, Ivan. What do you mean about the runbook completed, the job? I linked the jobs with schedules, after the jobs finished there have no outputs. I will finish the test and connect with you tomorrow, please wait for a moment. – Arthur Dec 26 '18 at 04:03
  • yes, the job finished. If it's no output, then it's a little weird. But now we can focus the delete thing, any issue please let me know. – Ivan Glasenberg Dec 26 '18 at 04:20
  • @IvanYang yes, we can achieve the goal, thanks so much. Is it possible that the creation time take more than one hour because of the sharefile is too big or the net is slowly? Thanks. – Arthur Dec 26 '18 at 06:27
  • Yes, it's possible if the total time of the runbook taken is less than 3 hours. – Ivan Glasenberg Dec 26 '18 at 06:38
  • The official docs about powershell runbook limitation is [here](https://learn.microsoft.com/en-us/azure/automation/automation-runbook-execution#fair-share): In order to share resources among all runbooks in the cloud, **Azure Automation will temporarily unload or stop any job that has been running for more than three hours** – Ivan Glasenberg Dec 26 '18 at 06:41
  • Really? So it's another problem, if it takes more than one hour, we can't appoint the snapshots by an hour? By the way, do you know how to get all of the sharefiles at one time? Thanks – Arthur Dec 26 '18 at 06:47
  • If it takes more than one hour, there is an workaround(not very smart, but can use): You can set the schedule for the snapshots creation, like 1 am for the daily, 4 am for the weekly, 7 am for the monthly. Then for the daily snapshots, if it takes more than 1 hour, you can set the condition in the snapshots delete function as when creation_time.hour -eq 1 or -eq 2 or -eq 3. – Ivan Glasenberg Dec 26 '18 at 06:55
  • get all sharefiles: Get-AzureStorageShare -Context $context | where {$_.IsSnapshot -eq $false} – Ivan Glasenberg Dec 26 '18 at 07:01
  • @IvanYang Thanks so much, it is a good idea. I will try to create/delete all sharefiles with one PowerShell. – Arthur Dec 26 '18 at 07:10
  • ok, and hopes you good luck. Please post new question if any new issue occurs. So can you please mark this one since it solve your current problem? Thank you. – Ivan Glasenberg Dec 26 '18 at 07:12
  • @IvanYang, it worked very well. Thanks so much. Last question: '$snap = Get-AzureStorageShare -Context $context | where {($_.IsSnapshot -eq $true)}' this code mean get all the sharefiles snapshots, right? I had marked your answer, thanks again, Ivan, you are very helpful. – Arthur Dec 26 '18 at 07:32
  • yes, you're right :). The `$_.IsSnapshot -eq $true` will get all snapshots, and `$_.IsSnapshot -eq $false` will get all the fileshares. – Ivan Glasenberg Dec 26 '18 at 07:38
  • You're welcome. If any issue, just feel free to start a new post :). And if you can vote, please help vote my answer as well:) – Ivan Glasenberg Dec 26 '18 at 07:46

1 Answers1

0

Update 12/21:

In runbook_2, which is used to delete the daily/weekly/monthly snapshots at specified time.

Code as below:

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

}

In runbook_2, delete the snapshots in a time range:

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

}
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60