Friday, October 7, 2011

Python script to backup/restore Azure Storage blobs

Recently I wrote python script to backup/restore all blobs from Windows Azure Storage account.
You can find it on github
https://github.com/vsevolodp/azure-storage-backup
or download as a zip archive.

To see how to use it run:
  $ python as-backup.py

See README file for more info about supported fatures

Monday, September 19, 2011

How to migrate Azure Storage data after upgrade to Azure SDK 1.5

After Azure SDK 1.5 installation complete you will see that all your data disappeared from Storage. This is because installation will create new database in your SQL Express instance. But all your data still there.

Microsoft SQL Server Management Studio

As you can see I had previous database created 2009-09-19.

So you need to move your data into new database. To do it open new query editing window and copy/paste/run the script below (double-click to Select all).

And don't forget to change names of the databases!
BEGIN TRANSACTION
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[BlobContainer]
 ([AccountName]
 ,[ContainerName]
 ,[LastModificationTime]
 ,[ServiceMetadata]
 ,[Metadata])
SELECT [AccountName]
      ,[ContainerName]
      ,[LastModificationTime]
      ,[ServiceMetadata]
      ,[Metadata]
  FROM [DevelopmentStorageDb20090919].[dbo].[BlobContainer]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[BlockData]
 ([AccountName]
 ,[ContainerName]
 ,[BlobName]
 ,[VersionTimestamp]
 ,[IsCommitted]
 ,[BlockId]
 ,[Length]
 ,[Data])
SELECT [AccountName]
      ,[ContainerName]
      ,[BlobName]
      ,[VersionTimestamp]
      ,[IsCommitted]
      ,[BlockId]
      ,[Length]
      ,[Data]
  FROM [DevelopmentStorageDb20090919].[dbo].[BlockData]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[Blob]
 ([AccountName]
 ,[ContainerName]
 ,[BlobName]
 ,[VersionTimestamp]
 ,[BlobType]
 ,[CreationTime]
 ,[LastModificationTime]
 ,[ContentLength]
 ,[ContentType]
 ,[ContentMD5]
 ,[ServiceMetadata]
 ,[Metadata]
 ,[LeaseId]
 ,[LeaseTypeInt]
 ,[LeaseDuration]
 ,[LeaseEndTime]
 ,[SequenceNumber]
 ,[IsCommitted]
 ,[HasBlock]
 ,[UncommittedBlockIdLength]
 ,[MaxSize]
 ,[FileName])
SELECT [AccountName]
      ,[ContainerName]
      ,[BlobName]
      ,[VersionTimestamp]
      ,[BlobType]
      ,[CreationTime]
      ,[LastModificationTime]
      ,[ContentLength]
      ,[ContentType]
      ,[ContentMD5]
      ,[ServiceMetadata]
      ,[Metadata]
      ,[LeaseId]
      ,[LeaseTypeInt]
      ,[LeaseDuration]
      ,[LeaseEndTime]
      ,[SequenceNumber]
      ,[IsCommitted]
      ,[HasBlock]
      ,[UncommittedBlockIdLength]
      ,[MaxSize]
      ,[FileName]
  FROM [DevelopmentStorageDb20090919].[dbo].[Blob]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[CommittedBlock] 
 ([AccountName]
 ,[ContainerName]
 ,[BlobName]
 ,[VersionTimestamp]
 ,[Offset]
 ,[BlockId]
 ,[Length]
 ,[BlockVersion])
SELECT [AccountName]
      ,[ContainerName]
      ,[BlobName]
      ,[VersionTimestamp]
      ,[Offset]
      ,[BlockId]
      ,[Length]
      ,[BlockVersion]
  FROM [DevelopmentStorageDb20090919].[dbo].[CommittedBlock]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[Page]  
 ([AccountName]
      ,[ContainerName]
      ,[BlobName]
      ,[VersionTimestamp]
      ,[StartOffset]
      ,[EndOffset]
      ,[FileOffset])
SELECT [AccountName]
      ,[ContainerName]
      ,[BlobName]
      ,[VersionTimestamp]
      ,[StartOffset]
      ,[EndOffset]
      ,[FileOffset]
  FROM [DevelopmentStorageDb20090919].[dbo].[Page]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[CurrentPage]  
 ([AccountName]
 ,[ContainerName]
 ,[BlobName]
 ,[VersionTimestamp]
 ,[StartOffset]
 ,[EndOffset])
SELECT [AccountName]
      ,[ContainerName]
      ,[BlobName]
      ,[VersionTimestamp]
      ,[StartOffset]
      ,[EndOffset]
  FROM [DevelopmentStorageDb20090919].[dbo].[CurrentPage]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[QueueContainer]
 ([AccountName]
 ,[QueueName]
 ,[LastModificationTime]
 ,[ServiceMetadata]
 ,[Metadata])
SELECT [AccountName]
      ,[QueueName]
      ,[LastModificationTime]
      ,[ServiceMetadata]
      ,[Metadata]
  FROM [DevelopmentStorageDb20090919].[dbo].[QueueContainer]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[QueueMessage]  
 ([AccountName]
 ,[QueueName]
 ,[VisibilityStartTime]
 ,[MessageId]
 ,[ExpiryTime]
 ,[InsertionTime]
 ,[DequeueCount]
 ,[Data])
SELECT [AccountName]
      ,[QueueName]
      ,[VisibilityStartTime]
      ,[MessageId]
      ,[ExpiryTime]
      ,[InsertionTime]
      ,[DequeueCount]
      ,[Data]
  FROM [DevelopmentStorageDb20090919].[dbo].[QueueMessage]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[TableContainer]
 ([AccountName]
 ,[TableName]
 ,[LastModificationTime]
 ,[ServiceMetadata]
 ,[Metadata]
 ,[SchemaXml])
SELECT [AccountName]
      ,[TableName]
      ,[LastModificationTime]
      ,[ServiceMetadata]
      ,[Metadata]
      ,[SchemaXml]
  FROM [DevelopmentStorageDb20090919].[dbo].[TableContainer]
GO

INSERT INTO [DevelopmentStorageDb20110816].[dbo].[TableRow]  
 ([AccountName]
 ,[TableName]
 ,[PartitionKey]
 ,[RowKey]
 ,[Timestamp]
 ,[Data])
SELECT [AccountName]
      ,[TableName]
      ,[PartitionKey]
      ,[RowKey]
      ,[Timestamp]
      ,[Data]
  FROM [DevelopmentStorageDb20090919].[dbo].[TableRow]
GO

COMMIT TRANSACTION
GO

Tuesday, April 26, 2011

Just finished TOPCODER SRM 504 DIVISION 2 competition. Solved 250 and 500 point problems.

Here are my solutions in C#

using System;

public class ComparerInator
{
    public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
    
    public int makeProgram(int[] A, int[] B, int[] wanted)
    {
        if (test(A, B, wanted, f_a) || test(A, B, wanted, f_b))
            return 1;
        if (test(A, B, wanted, f_ab) || test(A, B, wanted, f_ba))
            return 7;
            
        return -1;
    }
    
    bool test(int[] A, int[] B, int[] wanted, Func<int, int, int> f)
    {
        for (int i=0; i < A.Length; i++)
        {
            if (f(A[i], B[i]) == wanted[i])
                continue;
                
            return false;
        }
        
        return true;
    }
    
    int f_a(int a, int b)
    {
        return a;
    }
    
    int f_b(int a, int b)
    {
        return b;
    }
    
    int f_ab(int a, int b)
    {
        return a<b?a:b;
    }
    
    int f_ba(int a, int b)
    {
        return a<b?b:a;
    }
}

using System;
using System.Text;

public class MathContest
{
    public int countBlack(String ballSequence, int repetitions)
    {
        StringBuilder sb = new StringBuilder(ballSequence.Length * repetitions);
        for (int i = 0; i < repetitions; i++)
            sb.Append(ballSequence);
            
        s = sb.ToString();
        
        pos = -1;
        invert = false;
        direction = 1;
        end = s.Length - 1;
        
        int count = 0;
        
        char ball = get_next();
        while (ball != (Char)0)
        {
            if (ball == 'B')
                count++;
            ball = get_next();
        }
        
        return count;
    }

    string s;
    int pos;
    bool invert;
    int direction;
    int end;

    char get_next()
    {
        if (pos == end)
            return (Char)0;    
            
        pos += direction;
        char ch = s[pos];
        
        if (invert)
        {
            if (ch == 'B')
                ch = 'W';
            else
                ch = 'B';
        }
        
        if (ch == 'B')
            invert = invert ? false : true;
        else
        {
            int t = end;
            end = pos + direction;
            pos = t + direction;
            direction = direction == 1 ? -1 : 1;
        }
        
        return ch;
    }
}
Unfortunately I've made a mistake in 500-point problem. Instead of
end = pos + direction;
            pos = t + direction;
I wrote
end = pos;
            pos = t;
so my solution was successfully challenged and gave me zero points.

More then it - it was my first competition on TopCoder and there was a problem running the engine. So results are not go to rating. Well... next time.