上海-北京间通过Azure Storage的RA-GRS类型的存储账户进行快速复制
Azure的Blob存储分成多种类型,目前主要有:

其中RA-GRS可以在上海-北京两个数据中心间同步数据。并且,在第二个数据中心可以只读的方式读取这个存储账户中的Blob内容。
虽然GRS采用的是准实时同步,不是实时同步,但对很多需要快速恢复虚拟机、文件的情况已经足够使用。
本文将介绍,如何采用RA-GRS在第二个数据中心可以读的特性,在第二个数据中心快速的复制磁盘文件的功能。
下面的Powershell脚本将用户输入的VM信息获取OSDisk和DataDisk的信息,把这些Disk复制到用户指定的类型是RA-GRS的存储账户中,并将这些Disk在第二个数据中心复制到用户指定的目标存储账户中。具体步骤如下:

第一步,把VM的Disk复制到相同存储账户的临时Container:temp中。这一步的作用是将正在读写的Disk变成一个普通的VHD文件。
第二步,把LRS的temp中的VHD文件复制到RA-GRS的存储账户的temp中。这一步比较耗时,主要看Disk的实际使用量。
第三步,等待数分钟,Azure平台会自动将VHD文件复制到另外一个数据中心的Secondary存储账户中。
第四步,把VHD文件复制到另外一个数据中心的目标存储账户中,并生成Disk。后续可以使用这些Disk创建VM。
具体的PowerShell脚本如下:
function Copy-GRSVM {
param(
#The GRS_RA type storage account
[Parameter(Mandatory=$true)]
[String]$GRS_StorageAccountName,
#The Dest Storage account, in another region
[Parameter(Mandatory=$true)]
[String]$Dst_StorageAccountName,
#VM cloud service name
[Parameter(Mandatory=$true)]
[String]$VM_Service,
#VM Name
[Parameter(Mandatory=$true)]
[String]$VM_Name
)
#Get the VM, using the service name and vm name
$vm = get-azurevm -ServiceName $VM_Service -Name $VM_Name
#Get the VM OS type
$ostype = $vm.VM.OSVirtualHardDisk.OS
#Copy OS Disk
$oslink = $vm.vm.OSVirtualHardDisk.MediaLink.OriginalString
Copy-diskfromlink -GRS_StorageAccountName $GRS_StorageAccountName -Dst_StorageAccountName $Dst_StorageAccountName -link $oslink -isOSDisk $true -OSType $ostype
#copy Data Disk
$datadisks = $vm.vm.DataVirtualHardDisks
foreach ($datadisk in $datadisks)
{
$datadisklink = $datadisk.MediaLink.OriginalString
Copy-diskfromlink -GRS_StorageAccountName $GRS_StorageAccountName -Dst_StorageAccountName $Dst_StorageAccountName -link $datadisklink -isOSDisk $false -OSType $ostype
}
}
function Copy-diskfromlink {
param(
#The GRS_RA type storage account
[Parameter(Mandatory=$true)]
[String]$GRS_StorageAccountName,
#The Dest Storage account, in another region
[Parameter(Mandatory=$true)]
[String]$Dst_StorageAccountName,
#VM disk link
[Parameter(Mandatory=$true)]
[String]$link,
#VM OS type
[Parameter(Mandatory=$true)]
[String]$OSType,
#The disk is OS
[Parameter(Mandatory=$true)]
[boolean]$isOSDisk
)
$diskname = $link.Split("/")[-1]
$vmdiskctn = $link.Split("/")[-2]
#Get the VM storage account's name, keys and context.
$VM_StorageAccountName = $link.Split("/")[-3].Split(".")[0]
$vmsa = Get-AzureStorageAccount -StorageAccountName $VM_StorageAccountName
$vmsakey = Get-AzureStorageKey -StorageAccountName $vmsa.Label
$vmsactx = New-AzureStorageContext -StorageAccountName $vmsa.Label -StorageAccountKey $vmsakey.Primary
#Get the GRS_RA storage account's name, keys and context
$grsa = Get-AzureStorageAccount -StorageAccountName $GRS_StorageAccountName
$grsakey = Get-AzureStorageKey -StorageAccountName $grsa.Label
$grsactx = New-AzureStorageContext -StorageAccountName $grsa.Label -StorageAccountKey $grsakey.Primary
$grsactn = Get-AzureStorageContainer -Context $grsactx | Where-Object {$_.name -match "temp"}
#Define the connection string, which is the GRS_RA storage account secondary region endpoint
$Connect_str="DefaultEndpointsProtocol=https;AccountName=" + $grsa.StorageAccountName + ";AccountKey=" + $grsakey.Primary + ";BlobEndpoint=http://" + $grsa.StorageAccountName + "-secondary.blob.core.chinacloudapi.cn;"
$grdstCtx = New-AzureStorageContext -ConnectionString $Connect_str
#Get the dest storage account's name, keys and context
$dstsa = Get-AzureStorageAccount -StorageAccountName $Dst_StorageAccountName
$dstsakey = Get-AzureStorageKey -StorageAccountName $dstsa.StorageAccountName
$dstsactx = New-AzureStorageContext -StorageAccountName $dstsa.StorageAccountName -StorageAccountKey $dstsakey.Primary
#In VM Storage account, if the temp container is not presented, create it.
$vmsactn = Get-AzureStorageContainer -Context $vmsactx | Where-Object {$_.name -match "temp"}
if ($vmsactn)
{write-host "temp container is already created!"}
else
{New-AzureStorageContainer -Name temp -Context $vmsactx}
#If the GRS_RA storage account type is not GRS_RA, change it to that.
if ($grsa.AccountType -eq "Standard_RAGRS")
{write-host "the storage account is GRS_RA"}
else
{Set-AzureStorageAccount -StorageAccountName $grsa.Label -Type Standard_RAGRS}
#In GRS_RA storage account, if the temp container is not presented, create it.
if ($grsactn)
{write-host "temp container is already created!"}
else
{New-AzureStorageContainer -Name temp -Context $grsactx}
#Copy VM OS disk to the same Storage account's temp conainter. This copy will be very fast. And is the only way to copy a starting VM's disk to a block blob file.
Start-AzureStorageBlobCopy -SrcBlob $diskname -SrcContainer $vmdiskctn -Context $vmsactx -DestContainer temp -DestBlob $diskname -DestContext $vmsactx -Force
#Monitor the copy status, wait until the copy success.
$i=0
while($true)
{
$i=$i+1
write-host "Copying vhd from VM Storage Account vhd container to temp container, elapsing"$i"0 seconds"
$cpstatus = Get-AzureStorageBlobCopyState -Container temp -Context $vmsactx -Blob $diskname
$percent=$cpstatus.BytesCopied/$cpstatus.TotalBytes * 100
write-host "Already copying" $percent "%"
if($cpstatus.Status -eq "Pending")
{Start-Sleep -s 10}
else
{
Write-Host "copy ok"
break}
}
#Copy the disk vhd file, from the VM storage temp container to the GRS_RA storage account temp container. This copy will spend lots of time.
Start-AzureStorageBlobCopy -SrcBlob $diskname -SrcContainer temp -Context $vmsactx -DestContainer temp -DestBlob $diskname -DestContext $grsactx -Force
#Monitor the copy status, wait until the copy success.
$i=0
while($true)
{
$i=$i+1
write-host "Copying vhd from VM Storage Account temp container to GRS Storage account temp container, elapsing"$i"0 seconds"
$cpstatus = Get-AzureStorageBlobCopyState -Container temp -Context $grsactx -Blob $diskname
$percent=$cpstatus.BytesCopied/$cpstatus.TotalBytes * 100
write-host "Already copying" $percent "%"
if($cpstatus.Status -eq "Pending")
{Start-Sleep -s 10}
else
{
Write-Host "copy ok"
break}
}
#Because the GRS_RA is best effort way to sync the data between two regions, wait 5 minutes to ensure the data is sync.
write-host "wait 5 minutes"
Start-Sleep -Seconds 300
#Check the disk is presented in the secondary region's GRS_RA storage account's temp folder.
while($true)
{
write-host "Check whether the disk is in the GRS storage account seconday site"
$grdstblob = Get-AzureStorageBlob -Container temp -Blob $diskname -Context $grdstCtx
if($grdstblob)
{ write-host "disk ok"
break}
else
{
Start-Sleep -s 10
}
}
#Copy the disk vhd file from the GRS_RA seconday region temp container to the dest storage account's vhds container
Start-AzureStorageBlobCopy -SrcContainer temp -SrcBlob $diskname -Context $grdstCtx -DestContainer vhds -DestContext $dstsactx -DestBlob $diskname
#Monitor the copy status, wait until the copy success.
$i=0
while($true)
{
$i=$i+1
write-host "Copying vhd from GRS Account secondary site temp container to destination Storage Account temp container, elapsing"$i"0 seconds"
$cpstatus = Get-AzureStorageBlobCopyState -Container vhds -Context $dstsactx -Blob $diskname
$percent=$cpstatus.BytesCopied/$cpstatus.TotalBytes * 100
write-host "Already copying" $percent "%"
if($cpstatus.Status -eq "Pending")
{Start-Sleep -s 10}
else
{
Write-Host "copy ok"
break}
}
#remove the temp vhd file in 2 temp containers
write-host "Removing the Temp vhd file"
Remove-AzureStorageBlob -Context $vmsactx -Container temp -Blob $diskname
Remove-AzureStorageBlob -Context $grsactx -Container temp -Blob $diskname
#Create the new disk from the vhd file
write-host "create disk from vhd file"
$dst_disk_link = "https://" + $Dst_StorageAccountName + ".blob.core.chinacloudapi.cn/vhds/" +$diskname
if($isOSDisk)
{
Add-AzureDisk -DiskName $diskname -MediaLocation $dst_disk_link -OS $OSType
}
else
{
Add-AzureDisk -DiskName $diskname -MediaLocation $dst_disk_link
}
}
Copy-GRSVM -GRS_StorageAccountName hweast -Dst_StorageAccountName hwnorth -VM_Service hwcentos65 -VM_Name hwcentos65-03
上海-北京间通过Azure Storage的RA-GRS类型的存储账户进行快速复制的更多相关文章
- Microsoft Azure Storage Exployer使用指南
概述 Microsoft Azure Storage Exployer 是微软官方推荐的一款管理Azure Storage 客户端工具,客户使用完全免费.支持Windows.Mac和Linux.用户使 ...
- Azure Storage架构介绍
Windows Azure Storage由三个重要部分或者说三种存储数据服务组成,它们是:Windows Azure Blob.Windows Azure Table和Windows Azure Q ...
- Azure Terraform(四)状态文件存储
一,引言 我们都知道在执行部署计划之后,当前目录中就产生了名叫 "" 的 Terraform 的状态文件,该文件中记录了已部署资源的状态.默认情况下,在执行部署计划后,Terraf ...
- 关于Azure存储账户中存储虚拟机VHD文件的注意事项
Joy Qiao from MSFT Thu, Mar 12 2015 3:16 PM 我们在使用Azure时经常都会在Azure存储账户中放一些文件,包括Azure虚机的VHD文件也都是放在存储 ...
- Windows Azure Storage (21) 使用AzCopy工具,加快Azure Storage传输速度
<Windows Azure Platform 系列文章目录> Update 2016-09-28 想要在Azure云端,使用AzCopy工具,从Azure China 上海数据中心存储账 ...
- Windows Azure Storage (18) 使用HTML5 Portal的Azure CDN服务
<Windows Azure Platform 系列文章目录> Update:2015-04-15 如果读者使用的是国内由世纪互联运维的Azure China服务,请参考笔者的文档:Azu ...
- Windows Azure Storage (24) 启用Azure Blob日志
<Windows Azure Platform 系列文章目录> 之前有一个业务需求,客户想知道Azure Storage是否有日志功能,可以检查某个Azure Blob文件在某个时间点被删 ...
- 【Azure】用“Azure Storage Exlporer”进行磁盘拷贝
zure Storage Explorer工具的下载 Azure 存储客户端工具 https://docs.azure.cn/zh-cn/storage/storage-explorers Azure ...
- Azure Storage 系列(一)入门简介
一,引言 今天作为新的Azure 资源介绍的开篇,我们来学习一个新的服务,Azure Storage.众所周知,我们实际在开发过程中,会需要存储一些比如说日志,图片,等等,各种类型的数据.比如说存储图 ...
随机推荐
- javascript中apply和call的区别
请补充 136页 pdf 高级javascript设计
- ShowModal 代码分析
下面为Delphi中,方法TCustomForm.ShowModal的代码,通过分析以下代码,可以了解ShowModal到底是怎么一回事! 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Js中的apply和call
1.call和apply都是为了改变某个函数运行时的上下文而存在的 2.也就是改变函数体内this的指向. 3.二者的作用完全一样,只是接受参数的方式不太一样. 4.call 需要把参数按顺序传递进去 ...
- JVM性能优化, Part 1 ―― JVM简介
JVM性能优化这些列文章共分为5章,是ImportNew上面翻译自Javaworld: 第1章:JVM技术概览 第2章:编译器 第3章:垃圾回收 第4章:并发垃圾回收 第5章:可伸缩性 众所周知,Ja ...
- 从硬盘设计思想到RAID改良之道
监控硬盘的前生今世关于桌面硬盘.企业级近线硬盘(NL-SAS/SATA)和监控硬盘的差别,我们在前文中已经讲得很详细,这里再换一个角度来看看. "监控硬盘是希捷和西数为视频监控定制的,典型的 ...
- 数据库抽象层PDO
通过数据库抽象层PDO可以访问多个数据库 //数据库抽象层PDO //造DSN:驱动名:dbname=数据库名:host=服务器地址 $dsn = "mysql:dbname=mydb;ho ...
- 关于用JAVA开发短信方面的知识
现在流行的网络业务莫过于短信了.网易新浪等都因此而盈利,股价上涨.我凭自己的经验和公司支持,也就乘着东风来研究一下了! 首先,你要选择一台移动或者联通的短信服务器做你们的发送短信接口.这是最关键的 ...
- JavaScript 从对象 new 说起,简单理解 this/call/apply
new 创建一个新对象: 将构造函数的作用域赋给新对象(因此this就指向了这个新对象): 执行构造函数中的代码(为这个新对象添加属性): 返回新对象 用代码描述的话(先别管proyotype, a ...
- python 3 面向过程编程
python 3 面向过程编程 核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就像设计好一条工业流水线,是一种机械式的思维方式. 1.优点:程序结构清晰,可以把复杂的问题简单化,流程 ...
- HDU 3954 Level up(多颗线段树+lazy操作)
又是一开始觉得的水题,结果GG了好久的东西... 题意是给你n个英雄,每个英雄开始为1级经验为0,最多可以升到k级并且经验一直叠加,每一级都有一个经验值上限,达到就升级.接着给你两种操作:W li r ...