目的
Azure SQL Databaseのエクスポート/インポート処理を、Azureで実現する。
課題
Azure SQL Databaseのエクスポート/インポートは、様々な方法で実現できる。
ローカル環境から実施する際には、PowerShellを使った処理が一番簡単だが、
ローカル環境準備できない場合、方法が限られてくる。
実現方法
エクスポート処理は、管理コンソールから設定できる「.Backpac」作成を利用する。
Azure SQL Database の BACPAC の作成およびエクスポート
インポート処理は、WebJobを使ってスケジューリングした実現を目指す。
データベースのインポート(RestAPI)。
実際のソース
RestAPIを設定する際は、上記エンドポイントを正しく設定する。(上記APIを参考)
main.cpp
static void Main()
{
ImportHelper IHelper = new ImporHelper();
//Config
string endpoint = @"https://os1-1prod-dacsvc.azure.com/dacwebservice.svc"; //西日本の場合
string server = [サーバ名];
string dbname = [DB名];
string strname = [Bacpac格納Storageアカウント];
string strkey = [StorageKey];
string blobcontainer =[Bacpac格納コンテナ];
string blobname = [Bacpac名];
string dbuser = [DBのユーザ名];
string dbpass = [DBのパスワード];
IHelper.EndPointUri = endpoint;
IHelper.ServerName = server + ".database.windows.net";
IHelper.StorageKey = strkey;
IHelper.DatabaseName = dbname;
IHelper.UserName = dbuser;
IHelper.Password = dbpass;
IHelper.BlobPath = String.Format(@"https://{0}.blob.core.windows.net/{1}/{2}.bacpac", strname,blobcontainer,blobname);
IHelper.DatabaseName = IHelper.DatabaseName;
IHelper.DoImport();
}
ImportHelper.cpp
//Config省略
public void DoImport()
{
//DBの設定を記載
string AzureEdition = "standard";
string AzureEditionSize = "S1";
string DatabaseSizeInGB = "250";
//XMLの値
string xml1 = @"http://schemas.datacontract.org/2004/07/Microsoft.SqlServer.Management.Dac.ServiceTypes";
string xml2 = @"http://www.w3.org/2001/XMLSchema-instance";
string tty = @"BlobStorageAccessKeyCredentials";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(this.EndPointUri + @"/Import");
request.Method = WebRequestMethods.Http.Post;
request.ContentType = @"application/xml";
request.Headers["x-ms-date"] = "2012-03-01";
//XML作成
XNamespace aw= xml1;
XNamespace i = xml2;
XElement root = new XElement( aw + "ImportInput",
new XAttribute("xmlns",xml1),
new XAttribute(XNamespace.Xmlns + "i",xml2),
new XElement(aw +"AzureEdition",AzureEdition),
new XElement(aw +"BlobCredentials",
new XAttribute(i+"type",tty),
new XElement(aw + "Uri",this.BlobPath),
new XElement(aw + "StorageAccessKey",this.StorageKey)
),
new XElement(aw + "ConnectionInfo",
new XElement(aw + "DatabaseName",this.DatabaseName),
new XElement(aw + "Password",this.Password),
new XElement(aw + "ServerName",this.ServerName),
new XElement(aw + "UserName",this.UserName)
),
new XElement(aw +"DatabaseSizeInGB",DatabaseSizeInGB),
new XElement(aw +"ServiceObjective",AzureEditionSize)
);
//接続処理(単純化してます)
try
{
using (Stream webRequestStream = request.GetRequestStream())
{
DataContractSerializer dataContractSerializer = new DataContractSerializer(root.GetType());
dataContractSerializer.WriteObject(webRequestStream, root);
webRequestStream.Close();
}
WebResponse webResponse = null;
XmlReader xmlStreamReader = null;
using (webResponse = request.GetResponse())
{
xmlStreamReader = XmlReader.Create(webResponse.GetResponseStream());
xmlStreamReader.ReadElementContentAsString();
}
}
catch (WebException responseException){}
}
これをWebJobとしてスケジュール登録すれば、AzureWebJobを利用したSQLImportバッチが完成。
ExportをWebJobで実効する際も、基本的には同じ。