使用AzureRM Powershell导入数据库

时间:2016-01-25 08:18:27

标签: azure azure-powershell azure-resource-manager azure-sql-database

有一个名为Import a BACPAC file to create a new Azure SQL database using PowerShell的文档,其中介绍了如何将bacpac文件导入ASM下的SQL Server。

有没有办法使用Azure资源管理cmdlet将bacpac文件导入Azure SQL Server。

继@juvchan回答后,我一直试图让以下工作。

$update = @{
            "operationMode" = "Import"
            "storageKey"= "Key"
            "storageKeyType" = "Primary"
            "administratorLogin"= "adminlogin"
            "administratorLoginPassword"= "adminpassword"
            "storageUri"= "https://example.blob.core.windows.net/sql/exampleIOSQL-2016-1-23-12-26.bacpac"
            }
New-AzureRmResource -ResourceGroupName "resourcegroupname" `
                -ResourceType "Microsoft.Sql/servers" `
                -Name "sqldbsvr" `
                -PropertyObject $update `
                -ApiVersion 2015-08-01 `
                -Force -Location "westeurope" 

不幸的是,除了这个非常有用的错误信息之外,我无法获得任何信息 -

  

New-AzureRmResource:{" code":""," message":"处理此请求时出错。&#34 ;,"目标":空,"细节":[]," innererror":[]}

1 个答案:

答案 0 :(得分:4)

目前,最新的 Microsoft Azure PowerShell - January 2016 (version 1.1) 的Azure RM模块 没有任何cmdlet 可以支持Azure SQL数据库导入,例如Azure服务管理的cmdlet,即Start-AzureSqlDatabaseImport

但是,有一种解决方法可以在Azure资源管理器(ARM)上下文中实现此目的。

解决方法是使用用户定义的ARM模板(包括数据库导入资源类型)执行 Azure资源组模板部署。

建议的解决方法包括示例PowerShell脚本,示例ARM模板json和示例ARM模板参数json,如下所示:

PowerShell示例代码如下:

Login-AzureRmAccount

$tenantId = "your_tenant_id"
$subscriptionId = "your_subscription_id"

$rgName = "your_rg_name"
$location = "your_location"
$templateFile = "YourARMTemplate.json"
$templateParamFile = "YourARMTemplate.Parameters.json"

Set-AzureRmContext -SubscriptionId $subscriptionId -TenantId $tenantId

New-AzureRmResourceGroup -Location $location -Name $rgName -Force

$deployment = New-AzureRmResourceGroupDeployment -ResourceGroupName $rgName -TemplateFile $templateFile -TemplateParameterFile $templateParamFile -Mode Incremental -Force

Azure SQL数据库导入的示例ARM模板json如下所示:

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "dbApiVersion": "2014-04-01-preview",
    "resourceGroupLocation": "[resourceGroup().location]",
    "dbServerNameTidy": "[toLower(trim(parameters('dbServerName')))]",
    "masterDbNameTidy": "[toLower(trim(parameters('masterDbName')))]"
  },
  "parameters": {
    "dbServerName": {
      "type": "string"
    },
    "dbLogin": {
      "type": "string"
    },
    "dbPassword": {
      "type": "string"
    },
    "dbServerVersion": {
      "type": "string",
      "defaultValue": "12.0"
    },
    "dbCollation": {
      "type": "string",
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "dbEdition": {
      "type": "string",
      "defaultValue": "Standard"
    },
    "dbMaxSize": {
      "type": "string",
      "defaultValue": "10737418240"
    },
    "dbServiceObjectiveLevel": {
      "type": "string",
      "defaultValue": "455330E1-00CD-488B-B5FA-177C226F28B7"
    },
    "bacpacStorageKey": {
      "type": "string"
    },
    "masterDbName": {
      "type": "string"
    },
    "masterBacpacUrl": {
      "type": "string"
    },
  },
  "resources": [
    {
      "type": "Microsoft.Sql/servers",
      "apiVersion": "[variables('dbApiVersion')]",
      "properties": {
        "administratorLogin": "[parameters('dbLogin')]",
        "administratorLoginPassword": "[parameters('dbPassword')]",
        "version": "[parameters('dbServerVersion')]"
      },
      "name": "[variables('dbServerNameTidy')]",
      "location": "[variables('resourceGroupLocation')]",
      "resources": [
        {
          "type": "firewallrules",
          "apiVersion": "[variables('dbApiVersion')]",
          "properties": {
            "endIpAddress": "0.0.0.0",
            "startIpAddress": "0.0.0.0"
          },
          "name": "AllowAllWindowsAzureIps",
          "dependsOn": [
            "[resourceId('Microsoft.Sql/servers', variables('dbServerNameTidy'))]"
          ]
        },
        {
          "type": "databases",
          "apiVersion": "[variables('dbApiVersion')]",
          "properties": {
            "edition": "[parameters('dbEdition')]",
            "collation": "[parameters('dbCollation')]",
            "maxSizeBytes": "[parameters('dbMaxSize')]",
            "requestedServiceObjectiveId": "[parameters('dbServiceObjectiveLevel')]"
          },
          "name": "[variables('webDbNameTidy')]",
          "location": "[variables('resourceGroupLocation')]",
          "dependsOn": [
            "[resourceId('Microsoft.Sql/servers', variables('dbServerNameTidy'))]"
          ],
          "resources": [
            {
              "type": "extensions",
              "apiVersion": "[variables('dbApiVersion')]",
              "properties": {
                "operationMode": "Import",
                "storageKey": "[parameters('bacpacStorageKey')]",
                "storageKeyType": "Primary",
                "administratorLogin": "[parameters('dbLogin')]",
                "administratorLoginPassword": "[parameters('dbPassword')]",
                "storageUri": "[parameters('masterBacpacUrl')]"
              },
              "name": "Import",
              "dependsOn": [
                "[resourceId('Microsoft.Sql/servers/databases', variables('dbServerNameTidy'), variables('masterDbNameTidy'))]"
              ]
            }
          ]
        }
      ]
    }
  ]
}

Azure SQL数据库导入的示例ARM模板参数json如下所示:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "dbServerName": {
            "value": "<your DB Server Name>"
        },
        "dbLogin": {
            "value": "<Your DB server login name>"
        },
        "dbPassword": {
            "value": "<Your DB server login password>"
        },
        "bacpacStorageKey": {
            "value": "<Your Azure Storage Account Primary key which stores the bacpac blob>"
        },
        "masterDbName": {
            "value": "<your Azure Sql Db name>"
        },
        "masterBacpacUrl": {
            "value": "<Your Azure storage account Bacpac blob file full Url>"
        }
    }
}

希望这有助于澄清。