shirobert 发表于 2017-6-30 09:35:48

Azure SQL DB容灾到本地脚本

  本脚本将Azure SQL Database 数据库容灾到本地
   (Azure SQL --> Azure Blob --> 本地)
  1.先决条件


[*]Azure 账号
[*]Storage 账号
[*]最新版本Azure Powershell
  2.本地使用管理员身份打开Windwos PowerShell ISE
  1).首次登录国内版Azure



Login-AzureRmAccount -EnvironmentName AzureChinaCloud
  2).保存登录信息 (脚本通过文件自动登录)



Save-AzureRmProfile -Path “{local path}”
  3).sql db容灾到 Azure Blob 再到本地 脚本    (Azure SQL -> Azure Blob -> 本地)



$mydate=(Get-Date).tostring('yyyy-MM-dd-HH-mm-ss')
Select-AzureRmProfile –Path "f:\myprofile.json"    #步骤二中保存在本地到本地的路径
$mystoreuri="{Blob Uri}/{Container}/manager$mydate.bacpac"#保存到blob容器中的路径,manager$mydate 是保存数据库bacpac文件名称+时间
$sqlusername='{Your Sqlusername}'   #Azure sql 帐号
$sqlpassword=ConvertTo-SecureString -string '{Your Sqlpassword}' -asplaintext -force
$storagename='{Your StorageName}'
$storagekey='{Your StorageKey}'   
$storagekeytype='StorageAccessKey'
Set-AzureRmContext -SubscriptionId'{Your SubscriptionId}'
$exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName '{Your ResourceGroupName}' -ServerName '{Your ServerName}' -DatabaseName '{Your DBName}' -StorageKeytype StorageAccessKey -StorageKey $storagekey -StorageUri $mystoreuri -AdministratorLogin $sqlusername -AdministratorLoginPassword $sqlpassword


while((Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink).status){
    if((Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink).status"Succeeded"){

     $StorageContext = New-AzureStorageContext -StorageAccountName $storagename -StorageAccountKey $storagekey -Environment AzureChinaCloud
    Get-AzureStorageBlobContent -Context $StorageContext -Container "{Your Container}" -Blob manager$mydate.bacpac-Destination "{local Path}"#本地路径(这里的路径是需要保存从blob下载出来的DB备份文件)
    break;
  }

}
  4).执行脚本 结果如下:

   文件保存到了Blob中 再下载到本地

  我的文件是保存在F:test目录中
     
   *最后再根据自己的需要做本地计划任务
页: [1]
查看完整版本: Azure SQL DB容灾到本地脚本