Created
April 12, 2012 08:45
-
-
Save RobBlackwell/2365665 to your computer and use it in GitHub Desktop.
Sample showing how to export a SQL Azure database to BACPAC format.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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