azure SQL DB导入与副本

时间:2017-08-15 16:37:50

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

我正在开发一个ARM模板,它将要求以逗号分隔的db名称列表,然后使用copyIndex函数创建它们。这方面工作得很好,但我的解决方案的下一步不是。我接下来要做的是为每个数据库导入一个.bacpac文件,以便在完成时可以使用。

验证错误表明问题在于导入资源dependsOn中的concat函数。我已经用一些不同的方式对它进行了测试,无法看出它的错误。

我看到的确切错误信息是....

  

无法在第'858行处理资源'/subscriptions/xxxxxx-xxxxx-xxxxxx-xxxxx/resourceGroups/testGroup/providers/Microsoft.Sql/servers/testsql/databases/CustomersDB/extensions/import'的模板语言表达式和列'10'。 '语言功能'concat'提供的参数无效。全部或全部参数都不必是数组。

**添加了整个模板

c.append(text[-1]) 
text = text[:-1]

1 个答案:

答案 0 :(得分:0)

据我所知,我们无法在嵌套资源中使用copyindex函数。

如果您运行手臂模板,则会遇到此错误:

  

不支持复制嵌套资源。有关使用详情,请参阅https://aka.ms/arm-copy/#looping-on-a-nested-resource。'。

所以我建议你将嵌套资源作为arm模板中的根资源移动。然后你可以使用copyindex。

更多细节,您可以参考下面的arm模板:

注意:将参数orb替换为您的数据库名称。

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "brandosqlAdminLogin": {
      "type": "string",
      "minLength": 1
    },
    "brandosqlAdminLoginPassword": {
      "type": "string"
    },
    "org": {
      "type": "array",
      "defaultValue": [
        "contoso",
        "fabrikam",
        "coho"
      ]
    },
    "copydatabaseCollation": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "copydatabaseEdition": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ]
    },
    "copydatabaseRequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "S0",
        "S1",
        "S2",
        "P1",
        "P2",
        "P3"
      ],
      "metadata": {
        "description": "Describes the performance level for Edition"
      }
    },
    "copy2StorageKeyType": {
      "type": "string",
      "minLength": 1
    },
    "copy2StorageKey": {
      "type": "string"
    },
    "copy2StorageUri": {
      "type": "string",
      "minLength": 1
    },
    "copy2AdministratorLogin": {
      "type": "string",
      "minLength": 1
    },
    "copy2AdministratorLoginPassword": {
      "type": "string"
    },
    "serverDatabaseName": {
      "type": "array",
      "defaultValue": [
        "brandoimprottest/contoso",
        "brandoimprottest/fabrikam",
        "brandoimprottest/coho"
      ]
    },

    "copysqldatabase2Collation": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "SQL_Latin1_General_CP1_CI_AS"
    },
    "copysqldatabase2Edition": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ]
    },
    "copysqldatabase2RequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "S0",
        "S1",
        "S2",
        "P1",
        "P2",
        "P3"
      ],
      "metadata": {
        "description": "Describes the performance level for Edition"
      }
    }

  },
  "variables": {
    "brandosqlName": "brandoimprottest"
    },
  "resources": [
    {
      "name": "[variables('brandosqlName')]",
      "type": "Microsoft.Sql/servers",
      "location": "[resourceGroup().location]",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [],
      "tags": {
        "displayName": "brandosql"
      },
      "properties": {
        "administratorLogin": "[parameters('brandosqlAdminLogin')]",
        "administratorLoginPassword": "[parameters('brandosqlAdminLoginPassword')]"
      },
      "resources": [
        {
          "name": "AllowAllWindowsAzureIps",
          "type": "firewallrules",
          "location": "[resourceGroup().location]",
          "apiVersion": "2014-04-01-preview",
          "dependsOn": [
            "[resourceId('Microsoft.Sql/servers', variables('brandosqlName'))]"
          ],
          "properties": {
            "startIpAddress": "0.0.0.0",
            "endIpAddress": "0.0.0.0"
          }
        }
      ]
    },
    {
      "name": "[concat(variables('brandosqlName'), '/', parameters('org')[copyIndex()])]",
      "type": "Microsoft.Sql/servers/databases",
      "location": "[resourceGroup().location]",
      "apiVersion": "2014-04-01-preview",
      "copy": {
        "count": 3,
        "name": "sql-copy"
      },
      "dependsOn": [ "[resourceId('Microsoft.Sql/servers', variables('brandosqlName'))]" ],
      "tags": {
        "displayName": "copysqldatabase2"
      },
      "properties": {
        "collation": "[parameters('copysqldatabase2Collation')]",
        "edition": "[parameters('copysqldatabase2Edition')]",
        "maxSizeBytes": "1073741824",
        "requestedServiceObjectiveName": "[parameters('copysqldatabase2RequestedServiceObjectiveName')]"
      }
    },
    {
      "name": "[concat(variables('brandosqlName'), '/', parameters('org')[copyIndex()],'/','aaaa')]",
      "type": "Microsoft.Sql/servers/databases/extensions",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [ "sql-copy" ],
      "tags": {
        "displayName": "copy3"
      },
      "copy": {
        "name": "sql-copy2",
        "count": 3
      },
      "properties": {
        "storageKeyType": "[parameters('copy2StorageKeyType')]",
        "storageKey": "[parameters('copy2StorageKey')]",
        "storageUri": "[parameters('copy2StorageUri')]",
        "administratorLogin": "[parameters('copy2AdministratorLogin')]",
        "administratorLoginPassword": "[parameters('copy2AdministratorLoginPassword')]",
        "operationMode": "Import"
      }
    }
  ],
  "outputs": {}
}

结果:

enter image description here

我还测试了你的模板,我发现你的存储网址在导入扩展中有问题。我用主存储密钥和网址更改了它。效果很好。

模板:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "eastasia"
    },
    "sqlAdminUsername": {
      "type": "string"
    },
    "sqlAdminPassword": {
      "type": "string"
    },
    "sqlServerName": {
      "type": "string"
    },
    "sqlDatabaseNames": {
      "type": "array",
      "defaultValue": [
        "CustomersDB",
        "WideWorldImporters-Standard"
      ]
    },
    "sqlEdition": {
      "type": "string",
      "defaultValue": "Standard"
    },
    "sqlRequestedServiceObjectiveName": {
      "type": "string",
      "defaultValue": "S2"
    },
    "sqlMaxSizeBytes": {
      "type": "string",
      "defaultValue": "268435456000"
    },
    "publicIP": {
      "type": "string"
    },
    "copy2StorageKeyType": {
      "type": "string",
      "minLength": 1
    },
    "copy2StorageKey": {
      "type": "string"
    },
    "copy2StorageUri": {
      "type": "string",
      "minLength": 1
    }

  },
  "variables": {
    "storageKeyType": "SharedAccessKey",
    "collation": "SQL_Latin1_General_CP1_CI_AS"
  },
  "resources": [
    {
      "name": "[parameters('sqlServerName')]",
      "type": "Microsoft.Sql/servers",
      "apiVersion": "2014-04-01-preview",
      "location": "[parameters('location')]",
      "properties": {
        "administratorLogin": "[parameters('sqlAdminUsername')]",
        "administratorLoginPassword": "[parameters('sqlAdminPassword')]",
        "version": "12.0"
      },
      "resources": [
        {
          "name": "AllowAllWindowsAzureIps",
          "type": "firewallrules",
          "apiVersion": "2014-04-01-preview",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
          ],
          "properties": {
            "endIpAddress": "0.0.0.0",
            "startIpAddress": "0.0.0.0"
          }
        },
        {
          "name": "Allow_Remote_SSMS",
          "type": "firewallrules",
          "apiVersion": "2014-04-01-preview",
          "location": "[parameters('location')]",
          "dependsOn": [
            "[concat('Microsoft.Sql/servers/', parameters('sqlServerName'))]"
          ],
          "properties": {
            "startIpAddress": "[parameters('publicIP')]",
            "endIpAddress": "[parameters('publicIP')]"
          }
        }
      ]
    },
    {
      "name": "[concat(parameters('sqlServerName'), '/', parameters('sqlDatabaseNames')[copyIndex()])]",
      "type": "Microsoft.Sql/servers/databases",
      "location": "[parameters('location')]",
      "apiVersion": "2014-04-01-preview",
      "copy": {
        "count": "[length(parameters('sqlDatabaseNames'))]",
        "name": "sql-copy"
      },
      "dependsOn": [ "[resourceId('Microsoft.Sql/servers/', parameters('sqlServerName'))]" ],
      "properties": {
        "collation": "[variables('collation')]",
        "edition": "[parameters('sqlEdition')]",
        "maxSizeBytes": "[parameters('sqlMaxSizeBytes')]",
        "requestedServiceObjectiveName": "[parameters('sqlRequestedServiceObjectiveName')]"
      }
    },
    {
      "name": "[concat(parameters('sqlServerName'), '/', parameters('sqlDatabaseNames')[copyIndex()],'/','import')]",
      "type": "Microsoft.Sql/servers/databases/extensions",
      "apiVersion": "2014-04-01-preview",
      "dependsOn": [ "sql-copy" ],
      "copy": {
        "name": "sql-import",
        "count": "[length(parameters('sqlDatabaseNames'))]"
      },
      "properties": {
        "storageKeyType": "[parameters('copy2StorageKeyType')]",
        "storageKey": "[parameters('copy2StorageKey')]",
        "storageUri": "[parameters('copy2StorageUri')]",
        "administratorLogin": "[parameters('sqlAdminUsername')]",
        "administratorLoginPassword": "[parameters('sqlAdminPassword')]",
        "operationMode": "Import"
      }
    }
  ]
}

结果:

enter image description here