Windows Azure Backups with Bacpac – Automating Cleanup (Part 3)

Welcome to part 3 of my azure backup series.  We’ll be looking at automating cleanup of your blob storage.  If you will be using this method of doing backups to blob storage (taking snapshots of the database every XX hours/days), eventually you’ll have a large build up of old backups that will be outdated.  So rather than logging in to the azure console every few days, why not automate the cleanup of your outdated bacpac files?

The following is a list of settings that are needed to get the cleanup working.

  • _storageAccountName – The name of the storage service account
  • _storageKey – One of the access keys for the storage account (primary or secondary, doesn’t matter).  Just make sure you don’t check it in to a repository!
  • _storageContainer – Name of the storage container you’d like your bacpac files to be put into.
  • _cleanupThreshold – # of days before a backup is considered outdated and deleted.  This needs to be a negative number, I do the conversion in my constructor to ensure it’s negative.

To do the overall cleanup, I take advantage of the managed APIs for the Azure Storage to get a list of all blobs in storage and then to delete them out.

First we want to setup a client, it’s fairly straight forward, we just pass in a connection string and it does the rest.

var storageAccount = CloudStorageAccount.Parse(
     string.Format(CloudStorageconnectionString, _storageAccountName, _storageKey));
var client = storageAccount.CreateCloudBlobClient();

Next, we need to tell the client which container we want to look at and get a list of all it’s contents.  Then we want to filter down the list so that it only shows blobs that are past the threshold (the ones we want to delete).

var container = client.GetContainerReference(_storageContainer);
var blobs = container.ListBlobs(null, true);
var filtered = blobs.Where(a => a is CloudBlockBlob && 
     ((CloudBlockBlob)a).Properties.LastModified < DateTime.Now.AddDays(_cleanupThreshold))

Finally, we make the calls to do the deletions.  The below is the basic call used to delete the files, but in my full code I return a list of those blobs that were deleted.

foreach(var item in filtered)
     var blob = (CloudBlockBlob)item;

That’s fairly straight forward right?  Here is the code as a whole, this is an excerpt of the entire code but it’s what is necessary to do the cleanup.

Now sit back and pretend like you have to do your cleanups everyday to save the company money 🙂

Azure Backups Series:


One thought on “Windows Azure Backups with Bacpac – Automating Cleanup (Part 3)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s