Azure SQL DB容灾到本地脚本
本脚本将Azure SQL Database 数据库容灾到本地
(Azure SQL --> Azure Blob --> 本地)
1.先决条件
- Azure 账号
- Storage 账号
- 最新版本Azure Powershell
2.本地使用管理员身份打开Windwos PowerShell ISE
1).首次登录国内版Azure
1 Login-AzureRmAccount -EnvironmentName AzureChinaCloud
2).保存登录信息 (脚本通过文件自动登录)
Save-AzureRmProfile -Path “{local path}”
3).sql db容灾到 Azure Blob 再到本地 脚本 (Azure SQL -> Azure Blob -> 本地)
1 $mydate=(Get-Date).tostring('yyyy-MM-dd-HH-mm-ss') 2 Select-AzureRmProfile –Path "f:\myprofile.json" #步骤二中保存在本地到本地的路径 3 $mystoreuri="{Blob Uri}/{Container}/manager$mydate.bacpac" #保存到blob容器中的路径 ,manager$mydate 是保存数据库bacpac文件名称+时间 4 $sqlusername='{Your Sqlusername}' #Azure sql 帐号 5 $sqlpassword=ConvertTo-SecureString -string '{Your Sqlpassword}' -asplaintext -force 6 $storagename='{Your StorageName}' 7 $storagekey='{Your StorageKey}' 8 $storagekeytype='StorageAccessKey' 9 Set-AzureRmContext -SubscriptionId '{Your SubscriptionId}' 10 $exportRequest = New-AzureRmSqlDatabaseExport -ResourceGroupName '{Your ResourceGroupName}' -ServerName '{Your ServerName}' -DatabaseName '{Your DBName}' -StorageKeytype StorageAccessKey -StorageKey $storagekey -StorageUri $mystoreuri -AdministratorLogin $sqlusername -AdministratorLoginPassword $sqlpassword
1 while((Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink).status){ 2 if((Get-AzureRmSqlDatabaseImportExportStatus -OperationStatusLink $exportRequest.OperationStatusLink).status -eq "Succeeded"){ 3 4 $StorageContext = New-AzureStorageContext -StorageAccountName $storagename -StorageAccountKey $storagekey -Environment AzureChinaCloud 5 Get-AzureStorageBlobContent -Context $StorageContext -Container "{Your Container}" -Blob manager$mydate.bacpac -Destination "{local Path}" #本地路径 (这里的路径是需要保存从blob下载出来的DB备份文件) 6 break; 7 } 8 9 }
4).执行脚本 结果如下:

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

我的文件是保存在F:test目录中

*最后再根据自己的需要做本地计划任务

浙公网安备 33010602011771号