1

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.

Arthur
  • 103
  • 11
  • 1
    Could you include more details in your question? What have you tried? – Joy Wang Dec 17 '18 at 07:13
  • Thanks, Joy Wang. I add some comments, if you know something about that, please write some comments at here. Thank u again. – Arthur Dec 17 '18 at 08:55

1 Answers1

1

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()
    }
}
Joy Wang
  • 39,905
  • 3
  • 30
  • 54
  • Thanks so much, Just now I had a try to delete the snapshots use the command you put upside. But all of the snapshots had been deleted, may I use one powershell to create a snapshot and then just delete that one after one month. – Arthur Dec 18 '18 at 00:14
  • And last time someone shared a command to create snapshot, are there have some difference? $context = New-AzureStorageContext -StorageAccountName your_storage_account -StorageAccountKey your_storage_account_key $share = Get-AzureStorageShare -Context $context -Name your_file_share_name $snapshot = $share.Snapshot() – Arthur Dec 18 '18 at 00:15
  • @Arthur 1.It should work, I test with `if($snapshot.SnapshotTime -lt (get-date).AddHours(-1))` to delete the snapshots created before an hour, it works fine, modify the `AddHours` to `AddMonths`, it should delete the snapshots created before one month. – Joy Wang Dec 18 '18 at 01:08
  • @ Joy Wang, Thanks so much, I test it with one hour too, I combine the create and delete command in one powershell, it can work, but it deleted another snapshot (The previous one), so I think maybe it's because of the create command (I used your command, but there have some error about "$SAResourceGroupName" and "Value[1], so I used the easy one that I posted upside). – Arthur Dec 18 '18 at 01:46
  • @Arthur Not sure why you got an error, all work fine on my side, and I test create and delete in two different runbooks, you could have a try. – Joy Wang Dec 18 '18 at 02:17
  • Do you know how to delete a appoint snapshot, like just delete the snapshot after 1 month which I create today, but have no effects of other snapshots, because I will keep the different snapshots with the different retained periods. – Arthur Dec 18 '18 at 02:22
  • I tired made them in one runbook, it can work well. Now I want to change the command "$allsnapshots" to the one I created before. But there has an error, I am not good at the cmdlets. Thanks so much, Joy. – Arthur Dec 18 '18 at 02:26
  • @Arthur This seems difficult to achieve, Snapshots can only be distinguished by `SnapshotTime`, my workaround is creating a snapshot, and get its `SnapshotTime`, new a runbook with a schedule to delete it after one month. – Joy Wang Dec 18 '18 at 02:32
  • @Arthur May be you could ask the specific question of deleting snapshots with different ways in another post, if my reply is helpful, could you accept it as the answer? – Joy Wang Dec 18 '18 at 02:54
  • Hi, I have some question want to check with you. yesterday you said you test with ** if($snapshot.SnapshotTime -lt (get-date).AddHours(-1)) to delete the snapshots created before an hour, it works fine. ** So it's mean this command is used to delete the snapshots create before an hour, not delete a new one then delete it after an hour, right? I test and found it will delete all of the existing snapshots rather than delete some snapshot after hours or months. Can you explain it? thanks – Arthur Dec 19 '18 at 04:15
  • @Arthur Yes, is used to delete the snapshots create before an hour. I test the create and delete in two different runbooks, it will just delete the snapshots created before an hour, not all the screenshots. Not sure why it occurred to you. – Joy Wang Dec 19 '18 at 05:14
  • sorry, I made a mistake. can you change your answer? Maybe it is better to say "Delete the snapshots created before one month:", thanks. – Arthur Dec 19 '18 at 05:19
  • @Arthur Done, seems a little difference between them. – Joy Wang Dec 19 '18 at 05:22