Backing up to Azure Blob Storage is great, especially if like me you use Ola Hallengren’s amazing (and free) jobs to backup MS SQL Server (and if you don’t, I recommend you do, find out more here). What is not quite so awesome is cleaning up old files. Blobs do not behave like files, and certainly if you use Ola’s jobs setting the cleanup time when using a URL for the backup destination will cause the backup to fail. There is however hope, because we can do our cleanup using PowerShell!
The Gist below contains the code at time of writing. For the latest and greatest version keep an eye on the Github repo.
Save this with a .ps1 extension and schedule it as you see fit.
We capture 7 arguments to make this script tick:
- storageAccountName - The name (NOT the URL) of our Azure Storage Account
- storageAccountKey - The key for the Storage Account
- containerName - The name of the container we want to clean up
- age - The maximum age of the files we want to keep, anything older will be removed
- errorSMTPServer - The SMTP server to use to send an email if something goes wrong
- errorSourceAddress - The from email of any error messages
- errorDestAddress - The to email of any error messages
When working with Blob Storage we have to first build the storage context ($context in the above script) We do this by running New-AzureStorageContext and passing in the storage account name and storage account key we capture as parameters. Interestingly, this cmdlet does not verify this inforamtion, it just build a context object which we can then pass to other Azure storage cmdlets.
The try/catch block then executes the Get-AzureBlobStorage cmdlet using our storage context from above and also adding the container to search. The results are filtered for anything older than the specified age and finally the filtered results passed to Remove-AzureStorageBlob for immediate destruction.
Should anything go wrong the catch block grabs the error and sends it via email using the details provided.
Use this code at your own risk. It is provided without warranty, guarantee and if used incorrectly could hose all your backups and this would not be my fault!