How to delete old files in an azure container

I plan to backup my azure vhd files by disabling my vm and then copying the vhd files from the production container to the backup container. How can I automate the deletion of VHD files that are weeks in a backup container?

+6
source share
3 answers

If you can accept the use of PowerShell, then this will do it for you. It will register the scheduled task for daily work and delete PageBlob in the specified container.

$taskTrigger = New-ScheduledTaskTrigger -Daily -At 12:01AM Register-ScheduledJob -Name DeleteMyOldFiles -Trigger $taskTrigger -ScriptBlock { $isOldDate = [DateTime]::UtcNow.AddDays(-7) Get-AzureStorageBlob -Container "[YOUR CONTAINER NAME]" | Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate -and $_.BlobType -eq "PageBlob" } | Remove-AzureStorageBlob } 
+8
source

This is something inaccessible right out of the box. You will need to write the code yourself. Essentially the steps are:

  • List of all blocks in the backup container. The Blob list returns drops along with its properties. One of the properties will be LastModifiedDate (this will be in UTC).
  • You can then put your logic in to find the drops that were changed "x" days ago. Then you continue and remove these drops.

A few other things:

  • You mentioned that your backup container contains some VHD files, which are essentially block blocks. When you specify blobs, you will also get a blob type so that you can further filter the list by blob type (= PageBlob )
  • Regarding process automation, you can either write this to a PowerShell script and then schedule it using Windows Scheduler. If you are more comfortable writing node.js, you can write the same logic using node.js and use the Windows Azure Mobile Service Scheduler.
+1
source

I found this answer while trying to delete the entire container. Using Rick's answer as a template, I came up with this trial option: if you determine which containers will be deleted:

 $ctx = New-AzureStorageContext -StorageAccountName $AzureAccount ` -StorageAccountKey $AzureAccountKey $isOldDate = [DateTime]::UtcNow.AddDays(-7) Get-AzureStorageContainer -Context $ctx ` | Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate } ` | Remove-AzureStorageContainer -WhatIf 

Then, when I was satisfied that the list should be deleted, I used -Force to not check every removal:

 Get-AzureStorageContainer -Context $ctx ` | Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate } ` | Remove-AzureStorageContainer -Force 
+1
source

Source: https://habr.com/ru/post/950632/


All Articles