Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Windows Azure Web Sites: Sample C# Web Job code to perform a simple retention policy over website backup files
// ----------------------------------------------------------------------------------
// Windows Azure Web Sites
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
using System;
using System.Configuration;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
namespace SimpleBackupWebJob
{
class RetentionPolicy
{
// Name of the container in the blob storage used to place the backup files
// The name of this container is always the same in any storage account that has website backup set up
const string BACKUP_FILES_CONTAINER_NAME = "websitebackups";
static void Main(string[] args)
{
CloudStorageAccount account = null;
CloudBlobContainer blobContainer = null;
int maxDaysToKeep = int.MaxValue;
string websiteName = "";
// Uses the Application settings to connect the storage account
try
{
account = new CloudStorageAccount(new StorageCredentials(ConfigurationManager.AppSettings["BACKUP_STORAGE_ACCOUNT"], ConfigurationManager.AppSettings["BACKUP_STORAGE_ACCOUNT_KEY"]), false);
}
catch (Exception e)
{
throw new Exception("Couldn't connect to the specified storage account. Make sure your application settings has BACKUP_STORAGE_ACCOUNT and BACKUP_STORAGE_ACCOUNT_KEY properly set up.", e);
}
// Connects to the blob container that has the backup files
try
{
blobContainer = account.CreateCloudBlobClient().GetContainerReference(BACKUP_FILES_CONTAINER_NAME);
}
catch (Exception e)
{
throw new Exception("Couldn't find " + BACKUP_FILES_CONTAINER_NAME + " blob. Retention process will not continue.", e);
}
maxDaysToKeep = int.Parse(ConfigurationManager.AppSettings["BACKUP_RETENTION_MAX_DAYS_TO_KEEP"]);
Console.WriteLine("Config Reading: BACKUP_RETENTION_MAX_DAYS_TO_KEEP=" + maxDaysToKeep);
websiteName = ConfigurationManager.AppSettings["WEBSITE_SITE_NAME"];
Console.WriteLine("Config Reading: WEBSITE_SITE_NAME=" + websiteName);
int numBackupFiles = 0;
int numBackupFilesDeleted = 0;
DateTimeOffset expirationTimestamp = new DateTimeOffset(DateTime.Today.AddDays(-maxDaysToKeep));
// iterate over all blob items in the backup container
foreach (CloudBlockBlob blobItem in blobContainer.ListBlobs())
{
string fileName = blobItem.Name.ToLower();
// Select only blob items that start with the website name and end with .zip or .xml
if (fileName.StartsWith(websiteName,StringComparison.OrdinalIgnoreCase)
&& (fileName.EndsWith(".zip") || fileName.EndsWith(".xml")))
{
numBackupFiles++;
// Check if blobItem last modified date is older than the number of informed in the retention policy parameter
if (DateTimeOffset.Compare(blobItem.Properties.LastModified.Value, expirationTimestamp) < 0)
{
Console.WriteLine("Deleting " + blobItem.Name + " Last Modified=" + blobItem.Properties.LastModified.Value.LocalDateTime.ToShortDateString());
// actually delete the blob item
blobItem.Delete(DeleteSnapshotsOption.None, null, null, null);
numBackupFilesDeleted++;
}
}
}
//Summarize results of the job execution
Console.WriteLine("Total backup files found = " + numBackupFiles);
Console.WriteLine("Total backup files deleted = " + numBackupFilesDeleted);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.