簡體   English   中英

使用 terraform 如何從備份創建 azure sql 數據庫

[英]Using terraform how do I create an azure sql database from a backup

使用 terraform 站點上的默認示例,我可以輕松地創建數據庫,但如何通過恢復備份來創建新數據庫?

provider "azurerm" {
    features {}
}

resource "azurerm_resource_group" "example" {
    name     = "example-resources"
    location = "West Europe"
}

resource "azurerm_storage_account" "example" {
    name                     = "examplesa"
    resource_group_name      = azurerm_resource_group.example.name
    location                 = azurerm_resource_group.example.location
    account_tier             = "Standard"
    account_replication_type = "LRS"
}

resource "azurerm_mssql_server" "example" {
    name                         = "example-sqlserver"
    resource_group_name          = azurerm_resource_group.example.name
    location                     = azurerm_resource_group.example.location
    version                      = "12.0"
    administrator_login          = "4dm1n157r470r"
    administrator_login_password = "4-v3ry-53cr37-p455w0rd"
}

resource "azurerm_mssql_database" "test" {
    name           = "acctest-db-d"
    server_id      = azurerm_mssql_server.example.id
    collation      = "SQL_Latin1_General_CP1_CI_AS"
    license_type   = "LicenseIncluded"
    max_size_gb    = 4
    read_scale     = true
    sku_name       = "BC_Gen5_2"
    zone_redundant = true

    create_mode = "RestoreExternalBackup" <-- WHAT ELSE DO I DO?

    extended_auditing_policy {
        storage_endpoint                        = azurerm_storage_account.example.primary_blob_endpoint
        storage_account_access_key              = azurerm_storage_account.example.primary_access_key
        storage_account_access_key_is_secondary = true
        retention_in_days                       = 6
    }


    tags = {
        foo = "bar"
    }

}

在文檔中,他們提到了create_mode “RestoreExternalBackup”選項,但沒有提供有關如何引用備份的示例 - 我的存儲在 azure 存儲容器中。

編輯:提到“RestoreExternalBackup”更多是因為我缺乏理解。 我想問的是如何從存儲帳戶中存儲的 bacpac 文件恢復/創建數據庫

遵循John Q. Martin的博客部署 Azure SQL 數據庫 Bacpac 和 Terraform

您可以將 bacpac 作為在 Azure 中創建的數據庫的來源。

首先,在 Azure SQL 服務器上設置防火牆,以防止部署過程中由於 blob 存儲訪問問題而導致的任何故障。 為了確保這一點,我們必須啟用“允許 Azure 服務和資源訪問此服務器”,這允許兩個 Azure 服務進行通信。

設置Azure SQL服務器防火牆

將 Start_ip 和 End_ip 都設置為 0.0.0.0。 這被 Azure 解釋為允許 Azure 服務的防火牆規則。

resource "azurerm_sql_firewall_rule" "allowAzureServices" {
  name                = "Allow_Azure_Services"
  resource_group_name = azurerm_resource_group.example.name
  server_name         = azurerm_sql_server.example.name
  start_ip_address    = "0.0.0.0"
  end_ip_address      = "0.0.0.0"
}

定義數據庫資源

我們需要使用azurerm_sql_database資源,因為僅支持通過此資源類型部署 bacpac。

此處的資源定義由兩個主要部分組成,第一部分是數據庫需要 go 的詳細信息,第二部分是定義 bacpac 源詳細信息的子塊。 在這里,我們需要輸入 bacpac 文件的 URI 和存儲密鑰,在本例中,我們使用 SAS 令牌作為密鑰以允許訪問 bacpac。

我們還需要為正在創建的服務器提供用戶名和密碼以允許導入工作,因為它需要獲得 Azure SQL 服務器的授權才能工作。

provider "azurerm" {
    features {}
}

resource "azurerm_resource_group" "example" {
    name     = "example-resources"
    location = "West Europe"
}

resource "azurerm_storage_account" "example" {
    name                     = "examplesa"
    resource_group_name      = azurerm_resource_group.example.name
    location                 = azurerm_resource_group.example.location
    account_tier             = "Standard"
    account_replication_type = "LRS"
}

resource "azurerm_sql_server" "example" {
  name                         = "myexamplesqlserver"
  resource_group_name          = azurerm_resource_group.example.name
  location                     = azurerm_resource_group.example.location
  version                      = "12.0"
  administrator_login          = "4dm1n157r470r"
  administrator_login_password = "4-v3ry-53cr37-p455w0rd"

  tags = {
    environment = "production"
  }
}

resource "azurerm_sql_firewall_rule" "allowAzureServices" {
  name                = "Allow_Azure_Services"
  resource_group_name = azurerm_resource_group.example.name
  server_name         = azurerm_sql_server.example.name
  start_ip_address    = "0.0.0.0"
  end_ip_address      = "0.0.0.0"
}


resource "azurerm_sql_database" "appdb01" {
  depends_on                       = [azurerm_sql_firewall_rule.allowAzureServices]
  name                             = "AzSqlDbName"
  resource_group_name              = azurerm_sql_server.example.resource_group_name
  location                         = azurerm_sql_server.example.location
  server_name                      = azurerm_sql_server.example.name
  collation      = "SQL_Latin1_General_CP1_CI_AS"
  requested_service_objective_name = "BC_Gen5_2"
  max_size_gb    = 4
  read_scale     = true
  zone_redundant = true
  

  create_mode = "Default"
  import {
    storage_uri                  = "https://examplesa.blob.core.windows.net/source/Source.bacpac"
    storage_key                  = "gSKjBfoK4toNAWXUdhe6U7YHqBgCBPsvoDKTlh2xlqUQeDcuCVKcU+uwhq61AkQaPIbNnqZbPmYwIRkXp3OzLQ=="
    storage_key_type             = "StorageAccessKey"
    administrator_login          = "4dm1n157r470r"
    administrator_login_password = "4-v3ry-53cr37-p455w0rd"
    authentication_type          = "SQL"
    operation_mode               = "Import"
  }



  extended_auditing_policy {
        storage_endpoint                        = azurerm_storage_account.example.primary_blob_endpoint
        storage_account_access_key              = azurerm_storage_account.example.primary_access_key
        storage_account_access_key_is_secondary = true
        retention_in_days                       = 6
    }


    tags = {
        foo = "bar"
    }
}

筆記:

extended_auditing_policy塊已移至azurerm_mssql_server_extended_auditing_policyazurerm_mssql_database_extended_auditing_policy 此塊將在提供程序的 3.0 版中刪除。

requested_service_objective_name -(可選)數據庫的服務目標名稱。 有效值取決於版本和位置,可能包括S0S1S2S3P1P2P4P6P11ElasticPool 您可以使用 cli 列出可用名稱: shell az sql db list-editions -l westus -o table 有關詳細信息,請參閱Azure CLI-az sql db

並且import支持以下內容:

  • storage_uri -(必需)指定 .bacpac 文件的 blob URI。
  • storage_key -(必需)指定存儲帳戶的訪問密鑰。
  • storage_key_type -(必需)指定存儲帳戶的訪問密鑰類型。 有效值為StorageAccessKeySharedAccessKey
  • administrator_login -(必需)指定 SQL 管理員的名稱。
  • administrator_login_password -(必需)指定 SQL 管理員的密碼。
  • authentication_type -(必需)指定用於訪問服務器的身份驗證類型。 有效值為SQLADPassword
  • operation_mode -(可選)指定正在執行的導入操作的類型。 唯一允許的值是Import

或者,如果您想繼續使用azurerm_mssql_database ,那么我們需要部署和清空數據庫,然后通過 SqlPackage 部署bacpac (我還沒有嘗試過)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM