Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete old files in azure container

I plan to backup my azure vhd files by shutting down my vm and then copying the vhd files from the production container to a backup container. How can I automate deleting vhd files that are a week old in the backup container?

like image 541
user1532937 Avatar asked Jul 30 '13 14:07

user1532937


3 Answers

If you can accept using PowerShell, then this would do it for you. It will register a scheduled job to run daily and remove PageBlob's in the container specified.

$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
}
like image 116
Rick Rainey Avatar answered Sep 28 '22 18:09

Rick Rainey


This is something not available right out of the box. You would have to write some code yourself. Essentially the steps would be:

  1. List all blobs in your backup container. Blob list would return blobs along with its properties. One of the properties would be LastModifiedDate (this would be in UTC).
  2. You could then put your logic to find blobs which have been modified "x" days ago. You would then go ahead and delete those blobs.

A few other things:

  • You mentioned that your backup container contains some VHDs which are essentially page blobs. When you list blobs, you would also get blob type so you could further filter the list by blob type (= PageBlob)
  • As far as automating the process go, you could either write this in a PowerShell script and then schedule it using Windows Scheduler. If you're comfortable writing node.js, you could write the same logic using node.js and make use of Windows Azure Mobile Service Scheduler.
like image 39
Gaurav Mantri Avatar answered Sep 28 '22 17:09

Gaurav Mantri


I found this answer when trying to delete the whole container. Using Rick's answer as a template I came up with this trial what-if to determine which containers would 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 so as to not have to confirm every delete:

Get-AzureStorageContainer -Context $ctx                              `
     |  Where-Object { $_.LastModified.UtcDateTime -lt $isOldDate  } `
     |  Remove-AzureStorageContainer -Force 
like image 37
ΩmegaMan Avatar answered Sep 28 '22 19:09

ΩmegaMan