PowerShell 跨地域复制Azure虚拟机


Windows Azure 上的虚拟机如果想跨地区复制,比如从上海复制到北京,目前唯一的方案是Azure 门户上的Recovery Services 保管库可以异地复制虚拟机。但是很不幸,不是任何版本的虚拟机都支持。即使虚拟机类型,比如Ubuntu 16.04 也可能由于具体的Linux 内核版本号不同,而不被支持,非常尬尴。

所以还得自己动手丰衣足食。

原则上,一个虚拟机=机型(vmSize)+ 磁盘+虚拟网络+网络安全组+网卡+可用性集。除了磁盘数据以外,其他皆是逻辑配置。所以如果能把磁盘上海复制到北京,剩下的肯定是一条康庄大道。

幸好有一条PowerShell命令: Start-AzureStorageBlobCopy,为了保持数据一致性,我们可以先把虚拟机关机,然后将磁盘导出生成一个SAS Blob地址,通过Start-AzureStorageBlobCopy命令把该VHD复制到目的地区域的某个Storage,然后再通过目的地VHD 文件就地还原磁盘,再通过磁盘就地还原虚拟机。

暂时写了一个演示脚本,托管在:https://github.com/mosserlee/Copy-MyAzureVM

执行Copy-MyAzureVM命令:

.\Copy-MyAzureVM.ps1 
-SubscriptionId 'xxxxx-9faa-xxxx' 
-AzureEnvironmentName 'AzureChinaCloud'  
-DestLocation 'chinanorth'

弹出Azure账号对话框:

登陆后,选择要复制的机器所在的资源组,

再弹出对话框选择当前资源组下面的虚拟机,点击确认后,开始复制。

最耗时当属磁盘的复制。但是我发现一个有趣的现象,我的一个磁盘申请时,手抖了一下,申请大小是1TB,实际使用大小估计只有2GB,复制过程大约耗时11分钟,所以它应该只是复制了实际使用数据。

下面贴出完整的复制日志输出:

2018-03-29 06:11:34 ::: -Porcess resource group:[DR-DEMO-SH] started...
2018-03-29 06:11:41 ::: --Copy VM [wp-db] started...
2018-03-29 06:11:43 ::: ---Resource group [DR-DEMO-SH-New] does not exist, try to create new one.
2018-03-29 06:11:44 ::: ---Resource group [DR-DEMO-SH-New] create done.
2018-03-29 06:11:50 ::: ---create security group [wp-blog-nsg] started...
2018-03-29 06:12:02 ::: ---create security group [wp-blog-nsg] end
2018-03-29 06:12:05 ::: ---create VNet [DR-DEMO-vnet] started...
2018-03-29 06:12:19 ::: ---create VNet [DR-DEMO-vnet] end.
2018-03-29 06:12:21 ::: ---create network interface [wp-db977] started...
2018-03-29 06:12:23 ::: ---create network interface [wp-db977] done.
2018-03-29 06:12:24 ::: ---add network interface [wp-db977] to security group [wp-blog-nsg] started...
2018-03-29 06:12:25 ::: ---add network interface [wp-db977] to security group [wp-blog-nsg] done.
2018-03-29 06:12:27 ::: ---Storage account [drdemoshnewcopymyazurevm] does not exist, try to create new one.
2018-03-29 06:12:54 ::: ---Storage account [drdemoshnewcopymyazurevm] create done.
2018-03-29 06:12:54 ::: ---Storage container [wp-db] does not exist, try to create new one.
2018-03-29 06:12:54 ::: ---Storage container [wp-db] create done.
2018-03-29 06:12:56 ::: ---Get share access key for disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] started...
2018-03-29 06:13:27 ::: ---Get share access key for disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] ended.
2018-03-29 06:13:27 ::: ---Copy vhd for disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] started...
2018-03-29 06:13:27 ::: ----Copy disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] in progress...
2018-03-29 06:24:49 ::: ----Copy disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] done.
2018-03-29 06:24:49 ::: ----Revoke share access key for disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] started..
.
2018-03-29 06:25:21 ::: ----Revoke share access key for disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] done.
2018-03-29 06:25:21 ::: ---Copy vhd for disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] done.
2018-03-29 06:25:22 ::: ---create disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] started...
2018-03-29 06:25:54 ::: ---create disk [wp-db_OsDisk_1_7775eae21af54cccbef709f9a6a207f6] done.
2018-03-29 06:25:55 ::: ---Get share access key for disk [wp-db-data] started...
2018-03-29 06:26:26 ::: ---Get share access key for disk [wp-db-data] ended.
2018-03-29 06:26:26 ::: ---Copy vhd for disk [wp-db-data] started...
2018-03-29 06:26:27 ::: ----Copy disk [wp-db-data] in progress...
2018-03-29 06:26:58 ::: ----Copy disk [wp-db-data] done.
2018-03-29 06:26:58 ::: ----Revoke share access key for disk [wp-db-data] started...
2018-03-29 06:27:29 ::: ----Revoke share access key for disk [wp-db-data] done.
2018-03-29 06:27:29 ::: ---Copy vhd for disk [wp-db-data] done.
2018-03-29 06:27:30 ::: ---create disk [wp-db-data] started...
2018-03-29 06:28:02 ::: ---create disk [wp-db-data] done.
2018-03-29 06:28:06 ::: ---Create VM [wp-db] started...

RequestId           :
IsSuccessStatusCode : True
StatusCode          : OK
ReasonPhrase        : OK

2018-03-29 06:28:32 ::: ---Create VM [wp-db] done.
2018-03-29 06:28:34 ::: ---Attach disk [wp-db-data] to  VM [wp-db] started...

RequestId           :
IsSuccessStatusCode : True
StatusCode          : OK
ReasonPhrase        : OK

2018-03-29 06:29:06 ::: ---Attach disk [wp-db-data] to  VM [wp-db] done.
2018-03-29 06:29:06 ::: --Copy VM [wp-db] done.
2018-03-29 06:29:06 ::: --Copy VM [wp-site] started...
2018-03-29 06:29:14 ::: ---create public ip [wp-site-ip] started...
2018-03-29 06:29:26 ::: ---create public ip [wp-site-ip] end.
2018-03-29 06:29:27 ::: ---create network interface [wp-site860] started...
2018-03-29 06:29:28 ::: ---create network interface [wp-site860] done.
2018-03-29 06:29:29 ::: ---add network interface [wp-site860] to security group [wp-blog-nsg] started...
2018-03-29 06:29:30 ::: ---add network interface [wp-site860] to security group [wp-blog-nsg] done.
2018-03-29 06:29:31 ::: ---Storage container [wp-site] does not exist, try to create new one.
2018-03-29 06:29:31 ::: ---Storage container [wp-site] create done.
2018-03-29 06:29:32 ::: ---Get share access key for disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] started...
2018-03-29 06:30:03 ::: ---Get share access key for disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] ended.
2018-03-29 06:30:03 ::: ---Copy vhd for disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] started...
2018-03-29 06:30:04 ::: ----Copy disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] in progress...
2018-03-29 06:41:41 ::: ----Copy disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] done.
2018-03-29 06:41:41 ::: ----Revoke share access key for disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] started...
2018-03-29 06:42:13 ::: ----Revoke share access key for disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] done.
2018-03-29 06:42:13 ::: ---Copy vhd for disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] done.
2018-03-29 06:42:14 ::: ---create disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] started...
2018-03-29 06:42:46 ::: ---create disk [wp-site_OsDisk_1_9de19e9aaf484fa88be008f95b6dc403] done.
2018-03-29 06:42:47 ::: ---Get share access key for disk [wp-site-data-1] started...
2018-03-29 06:43:18 ::: ---Get share access key for disk [wp-site-data-1] ended.
2018-03-29 06:43:18 ::: ---Copy vhd for disk [wp-site-data-1] started...
2018-03-29 06:43:19 ::: ----Copy disk [wp-site-data-1] in progress...
2018-03-29 06:52:56 ::: ----Copy disk [wp-site-data-1] done.
2018-03-29 06:52:56 ::: ----Revoke share access key for disk [wp-site-data-1] started...
2018-03-29 06:53:28 ::: ----Revoke share access key for disk [wp-site-data-1] done.
2018-03-29 06:53:28 ::: ---Copy vhd for disk [wp-site-data-1] done.
2018-03-29 06:53:29 ::: ---create disk [wp-site-data-1] started...
2018-03-29 06:54:00 ::: ---create disk [wp-site-data-1] done.
2018-03-29 06:54:04 ::: ---Create VM [wp-site] started...

RequestId           :
IsSuccessStatusCode : True
StatusCode          : OK
ReasonPhrase        : OK

2018-03-29 06:54:40 ::: ---Create VM [wp-site] done.
2018-03-29 06:54:42 ::: ---Attach disk [wp-site-data-1] to  VM [wp-site] started...

RequestId           :
IsSuccessStatusCode : True
StatusCode          : OK
ReasonPhrase        : OK

2018-03-29 06:55:13 ::: ---Attach disk [wp-site-data-1] to  VM [wp-site] done.
2018-03-29 06:55:13 ::: --Copy VM [wp-site] done.
2018-03-29 06:55:13 ::: -Porcess resource group [DR-DEMO-SH] done.
本文链接: https://www.pstips.net/copy-azure-vm-cross-region.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

电子邮件地址不会被公开。 必填项已用*标注