Skip to content

Instantly share code, notes, and snippets.

@RobBlackwell
Created April 12, 2012 08:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RobBlackwell/2365665 to your computer and use it in GitHub Desktop.
Save RobBlackwell/2365665 to your computer and use it in GitHub Desktop.
Sample showing how to export a SQL Azure database to BACPAC format.
using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
namespace SqlAzureBackup
{
class Program
{
/// <summary>
/// Requests that SQL Azure exports a database to blob storage in BACPAC format.
/// </summary>
/// <returns>A GUID representing the job.</returns>
static Guid Export(string serverName, string databaseName, string userName, string password, string blob, string key)
{
// Call the REST API, with an XML document containing the job details and credentials.
// NB This API does not seem to be documented on MSDN and therefore could be subject to change.
// NB It's a good idea to do this on a copy (see CREATE DATABASE AS COPY) for transactional integrity
// North Central US https://ch1prod-dacsvc.azure.com/DACWebService.svc
// South Central US https://sn1prod-dacsvc.azure.com/DACWebService.svc
// North Europe https://db3prod-dacsvc.azure.com/DACWebService.svc
// West Europe https://am1prod-dacsvc.azure.com/DACWebService.svc
// East Asia https://hkgprod-dacsvc.azure.com/DACWebService.svc
// Southeast Asia https://sg1prod-dacsvc.azure.com/DACWebService.svc
var request = WebRequest.Create("https://am1prod-dacsvc.azure.com/DACWebService.svc/Export");
request.Method = "POST";
Stream dataStream = request.GetRequestStream();
string body = String.Format("<ExportInput xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><BlobCredentials i:type=\"BlobStorageAccessKeyCredentials\"><Uri>{0}</Uri><StorageAccessKey>{1}</StorageAccessKey></BlobCredentials><ConnectionInfo><DatabaseName>{2}</DatabaseName><Password>{3}</Password><ServerName>{4}</ServerName><UserName>{5}</UserName></ConnectionInfo></ExportInput>", blob, key, databaseName, password, serverName, userName);
System.Text.UTF8Encoding utf8 = new System.Text.UTF8Encoding();
byte[] buffer = utf8.GetBytes(body);
dataStream.Write(buffer, 0, buffer.Length);
dataStream.Close();
request.ContentType = "application/xml";
// The HTTP response contains the job number, a Guid serialized as XML
using (WebResponse response = request.GetResponse())
{
Encoding encoding = Encoding.GetEncoding(1252);
using (var responseStream = new StreamReader(response.GetResponseStream(), encoding))
{
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(responseStream.BaseStream, new XmlDictionaryReaderQuotas()))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(Guid));
return (Guid)serializer.ReadObject(reader, true);
}
}
}
}
static void Main(string[] args)
{
Guid guid = Export("MYSEREVER.database.windows.net", "MYDATABASE", "MYUSERNAME", "MYPASSWORD", "https://MYACCOUNT.blob.core.windows.net/backups/newfile.bacpac", "MYKEY");
Console.WriteLine(guid);
}
}
}
@pkpjpm
Copy link

pkpjpm commented Jul 27, 2013

Thank you so much, this is exactly what we need! Only thing I would add is that you've left off 2 data center URLs, I believe you can see the complete list here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment