مشاركة عبر


التشغيل السريع: تكوين النسخ الاحتياطي المخزن لملفات Azure باستخدام Azure Terraform

يصف هذا التشغيل السريع كيفية تكوين النسخ الاحتياطي المخزن لملفات Azure باستخدام قالب Azure Terraform.

يدعم Azure Backup تكوين النسخ الاحتياطية للقطة والنسخ الاحتياطية المخزنة لملفات Azure في حسابات التخزين الخاصة بك. يوفر النسخ الاحتياطي المخزن حلا خارج الموقع، وتخزين البيانات في حساب تخزين v2 عام للحماية من برامج الفدية الضارة وإجراءات المسؤول الضارة.

يتيح Terraform تعريف البنية الأساسية السحابية ومعاينتها ونشرها.

المتطلبات الأساسية

قبل تكوين النسخ الاحتياطي المخزن لملفات Azure، تأكد من استيفاء المتطلبات الأساسية التالية:

تسجيل الدخول إلى حساب Azure

سجل الدخول إلى حساب Azure الخاص بك وقم بالمصادقة باستخدام أحد هؤلاء العملاء - Azure CLI أو Azure PowerShell.

يدعم Terraform مصادقة Azure فقط مع Azure CLI، وليس Azure PowerShell. يجب أولا المصادقة على Azure قبل استخدام الوحدة النمطية Azure PowerShell لمهام Terraform.

تنفيذ كود Terraform

قبل تنفيذ التعليمات البرمجية Terraform، تعرف على كيفية استخدام نماذج التعليمات البرمجية ل Terraform لإدارة موارد Azure.

لتنفيذ التعليمات البرمجية Terraform لتدفق النسخ الاحتياطي لمشاركة الملفات، قم بتشغيل البرامج النصية التالية:

  1. إنشاء دليل يمكنك استخدامه لاختبار نموذج التعليمات البرمجية Terraform وجعله دليلك الحالي.
  2. أنشئ ملفا باسم providers.tf وأضف التعليمات البرمجية التالية:
terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = "3.99.0"
    }
  }
}

provider "azurerm" {
  features {}
  subscription_id   = "<azure_subscription_id>"
  tenant_id = "<azure_subscription_tenant_id>"
}

  1. أنشئ ملفا باسم main.tf وأضف التعليمات البرمجية التالية:

    Get Subscription and Tenant Id from Config
    
    data "azurerm_client_config" "current" {
    }
    
    ## Create a Resource Group for Storage and vault
    resource "azurerm_resource_group" "rg" {
      location = var.resource_group_location
      name     = var.resource_group_name
    }
    
    ## Azure Recovery Services vault
    resource "azurerm_recovery_services_vault" "vault" {
      name                = var.vault_name
      location            = azurerm_resource_group.rg.location
      resource_group_name      = azurerm_resource_group.rg.name
      sku                 = "Standard"
      depends_on = [azurerm_resource_group.rg]
    }
    
    # generate a random string (consisting of four characters)
    # https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string
    resource "random_string" "random" {
      length  = 4
      upper   = false
      special = false
    }
    
    ## Create a File Storage Account 
    resource "azurerm_storage_account" "storage" {
      name                     = "stor${random_string.random.id}"
      resource_group_name      = azurerm_resource_group.rg.name
      location                 = azurerm_resource_group.rg.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
      depends_on = [azurerm_resource_group.rg]
    }
    
    ## Create a File Share 
    resource "azurerm_storage_share" "fsshare" {
      name                 = var.FS_name
      storage_account_name = azurerm_storage_account.storage.name
    quota                = 1
      depends_on           = [azurerm_storage_account.storage]
    }
    
    resource "azurerm_backup_container_storage_account" "protection-container" {
      resource_group_name = azurerm_resource_group.rg.name
      recovery_vault_name = azurerm_recovery_services_vault.vault.name
      storage_account_id  = azurerm_storage_account.storage.id
    }
    
    resource "azurerm_backup_policy_file_share" "example" {
      name                = var.FSPol_name
      resource_group_name = azurerm_resource_group.rg.name
      recovery_vault_name = azurerm_recovery_services_vault.vault.name
    
      backup {
        frequency = "Daily"
        time      = "23:00"
      }
    
      retention_daily {
        count = 10
      }
    }
    
    resource "azurerm_backup_protected_file_share" "share1" {
      resource_group_name       = azurerm_resource_group.rg.name
      recovery_vault_name       = azurerm_recovery_services_vault.vault.name
      source_storage_account_id = azurerm_backup_container_storage_account.protection-container.storage_account_id
      source_file_share_name    = azurerm_storage_share.fsshare.name
      backup_policy_id          = azurerm_backup_policy_file_share.example.id
    }
    
    
  2. أنشئ ملفا باسم variables.tf وأضف التعليمات البرمجية التالية:

    variable "resource_group_name" {
      type        = string
      default     = "Contoso_TF_RG"
      description = "Name of the resource group."
    }
    
    variable "resource_group_location" {
      type        = string
      default     = "eastus"
      description = "Location of the resource group."
    }
    
    variable "vault_name" {
      type        = string
      default     = "Contoso-RSV"
      description = "Name of the Recovery services vault."
    }
    
    variable "FS_name" {
      type        = string
      default     = "fs01"
      description = "Name of the Storage Account."
    }
    
    variable "FSPol_name" {
      type        = string
      default     = "AFS-Policy"
      description = "Name of the Storage Account."
    
    
  3. أنشئ ملفا باسم outputs.tf وأضف التعليمات البرمجية التالية:

    output "resource_group" {
      value = azurerm_resource_group.rg.name
    }
    
    output "location" {
      description = "The Azure region"
      value       = azurerm_resource_group.rg.location
    }
    
    output "storage_account" {
      description = "Storage account for Profiles"
      value       = azurerm_storage_account.storage.name
    }
    
    output "storage_account_share" {
      description = "Name of the Azure File Share created"
      value       = azurerm_storage_share.fsshare.name
    }
    
    output "backup_instance_id" {
      description = "backup instance"
      value       = azurerm_backup_protected_file_share.share1.id
    }
    
    

تهيئة Terraform

لتهيئة نشر Terraform، قم بتشغيل terraform init الأمر .

يقوم هذا الأمر بتنزيل موفر Azure المطلوب لإدارة موارد Azure.

terraform init -upgrade

تقوم -upgrade المعلمة بترقية مكونات الموفر الإضافية الضرورية إلى أحدث إصدار يتوافق مع قيود إصدار التكوين.

إنشاء خطة تنفيذ Terraform

لإنشاء خطة تنفيذ، قم بتشغيل الأمر terraform plan .

terraform plan -out main.tfplan

النقاط الرئيسية:

  • terraform plan ينشئ الأمر خطة تنفيذ دون تنفيذها، مما يسمح لك بالتحقق من الإجراءات اللازمة لمطابقة التكوين قبل إجراء التغييرات.

  • -out تحدد المعلمة ملف إخراج للخطة، مع ضمان تطبيق الخطة التي تمت مراجعتها.

تطبيق خطة تنفيذ Terraform

لتطبيق خطة التنفيذ على البنية الأساسية السحابية terraform apply ، قم بتشغيل الأمر .

terraform apply main.tfplan

النقاط الرئيسية:

  • يفترض الأمر المثال terraform apply أنك قمت مسبقا بتشغيل خطة -out main.tfplanterraform .
  • إذا استخدمت اسم ملف مختلف للمعلمة -out ، فاستخدم اسم الملف هذا في terraform apply الأمر .
  • إذا لم تستخدم المعلمة -out ، فقم بتشغيل terraform apply.

استكشاف أخطاء Terraform على Azure وإصلاحها

إذا واجهت مشكلات أثناء استخدام Terraform على Azure، فراجع دليل استكشاف الأخطاء وإصلاحها.

الخطوات التالية