Back up a virtual machine in Azure with a Bicep template
आलेख
Azure Backup allows you to back up your Azure VM using multiple options - such as Azure portal, PowerShell, CLI, Azure Resource Manager, Bicep, and so on. This article describes how to back up an Azure VM with an Azure Bicep template and Azure PowerShell. This quickstart focuses on the process of deploying a Bicep template to create a Recovery Services vault. For more information on developing Bicep templates, see the Bicep documentation and the template reference.
Bicep is a language for declaratively deploying Azure resources. You can use Bicep instead of JSON to develop your Azure Resource Manager templates (ARM templates). Bicep syntax reduces the complexity and improves the development experience. Bicep is a transparent abstraction over ARM template JSON that provides all JSON template capabilities. During deployment, the Bicep CLI converts a Bicep file into an ARM template JSON. A Bicep file states the Azure resources and resource properties, without writing a sequence of programming commands to create resources.
Resource types, API versions, and properties that are valid in an ARM template, are also valid in a Bicep file.
The template used below is from Azure quickstart templates. This template allows you to deploy simple Windows VM and Recovery Services vault configured with DefaultPolicy for Protection.
Bicep
@description('Specifies a name for generating resource names.')
@maxLength(8)paramprojectNamestring
@description('Specifies the location for all resources.')paramlocationstring = resourceGroup().location
@description('Specifies the administrator username for the Virtual Machine.')paramadminUsernamestring
@description('Specifies the administrator password for the Virtual Machine.')
@secure()paramadminPasswordstring
@description('Specifies the unique DNS Name for the Public IP used to access the Virtual Machine.')paramdnsLabelPrefixstring
@description('Virtual machine size.')paramvmSizestring = 'Standard_A2'
@description('Specifies the Windows version for the VM. This will pick a fully patched image of this given Windows version.')
@allowed([
'2008-R2-SP1''2012-Datacenter''2012-R2-Datacenter''2016-Nano-Server''2016-Datacenter-with-Containers''2016-Datacenter''2019-Datacenter''2019-Datacenter-Core''2019-Datacenter-Core-smalldisk''2019-Datacenter-Core-with-Containers''2019-Datacenter-Core-with-Containers-smalldisk''2019-Datacenter-smalldisk''2019-Datacenter-with-Containers''2019-Datacenter-with-Containers-smalldisk'
])paramwindowsOSVersionstring = '2016-Datacenter'varstorageAccountName = '${projectName}store'varnetworkInterfaceName = '${projectName}-nic'varvNetAddressPrefix = '10.0.0.0/16'varvNetSubnetName = 'default'varvNetSubnetAddressPrefix = '10.0.0.0/24'varpublicIPAddressName = '${projectName}-ip'varvmName = '${projectName}-vm'varvNetName = '${projectName}-vnet'varvaultName = '${projectName}-vault'varbackupFabric = 'Azure'varbackupPolicyName = 'DefaultPolicy'varprotectionContainer = 'iaasvmcontainer;iaasvmcontainerv2;${resourceGroup().name};${vmName}'varprotectedItem = 'vm;iaasvmcontainerv2;${resourceGroup().name};${vmName}'varnetworkSecurityGroupName = 'default-NSG'resourcestorageAccount'Microsoft.Storage/storageAccounts@2021-06-01' = {
name: storageAccountNamelocation: locationsku: {
name: 'Standard_LRS'
}
kind: 'Storage'properties: {}
}
resourcepublicIPAddress'Microsoft.Network/publicIPAddresses@2020-06-01' = {
name: publicIPAddressNamelocation: locationproperties: {
publicIPAllocationMethod: 'Dynamic'dnsSettings: {
domainNameLabel: dnsLabelPrefix
}
}
}
resourcenetworkSecurityGroup'Microsoft.Network/networkSecurityGroups@2020-06-01' = {
name: networkSecurityGroupNamelocation: locationproperties: {
securityRules: [
{
name: 'default-allow-3389'properties: {
priority: 1000access: 'Allow'direction: 'Inbound'destinationPortRange: '3389'protocol: 'Tcp'sourceAddressPrefix: '*'sourcePortRange: '*'destinationAddressPrefix: '*'
}
}
]
}
}
resourcevNet'Microsoft.Network/virtualNetworks@2020-06-01' = {
name: vNetNamelocation: locationproperties: {
addressSpace: {
addressPrefixes: [
vNetAddressPrefix
]
}
subnets: [
{
name: vNetSubnetNameproperties: {
addressPrefix: vNetSubnetAddressPrefixnetworkSecurityGroup: {
id: networkSecurityGroup.id
}
}
}
]
}
}
resourcenetworkInterface'Microsoft.Network/networkInterfaces@2020-06-01' = {
name: networkInterfaceNamelocation: locationproperties: {
ipConfigurations: [
{
name: 'ipconfig1'properties: {
privateIPAllocationMethod: 'Dynamic'publicIPAddress: {
id: publicIPAddress.id
}
subnet: {
id: '${vNet.id}/subnets/${vNetSubnetName}'
}
}
}
]
}
}
resourcevirtualMachine'Microsoft.Compute/virtualMachines@2020-06-01' = {
name: vmNamelocation: locationproperties: {
hardwareProfile: {
vmSize: vmSize
}
osProfile: {
computerName: vmNameadminUsername: adminUsernameadminPassword: adminPassword
}
storageProfile: {
imageReference: {
publisher: 'MicrosoftWindowsServer'offer: 'WindowsServer'sku: windowsOSVersionversion: 'latest'
}
osDisk: {
createOption: 'FromImage'
}
dataDisks: [
{
diskSizeGB: 1023lun: 0createOption: 'Empty'
}
]
}
networkProfile: {
networkInterfaces: [
{
id: networkInterface.id
}
]
}
diagnosticsProfile: {
bootDiagnostics: {
enabled: truestorageUri: storageAccount.properties.primaryEndpoints.blob
}
}
}
}
resourcerecoveryServicesVault'Microsoft.RecoveryServices/vaults@2020-02-02' = {
name: vaultNamelocation: locationsku: {
name: 'RS0'tier: 'Standard'
}
properties: {}
}
resourcevaultName_backupFabric_protectionContainer_protectedItem'Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems@2020-02-02' = {
name: '${vaultName}/${backupFabric}/${protectionContainer}/${protectedItem}'properties: {
protectedItemType: 'Microsoft.Compute/virtualMachines'policyId: '${recoveryServicesVault.id}/backupPolicies/${backupPolicyName}'sourceResourceId: virtualMachine.id
}
}
To deploy the template, select Try it to open the Azure Cloud Shell, and then paste the following PowerShell script in the shell window. To paste the code, right-click the shell window and then select Paste.
Azure PowerShell
$projectName = Read-Host -Prompt"Enter a project name (limited to eight characters) that is used to generate Azure resource names"$location = Read-Host -Prompt"Enter the location (for example, centralus)"$adminUsername = Read-Host -Prompt"Enter the administrator username for the virtual machine"$adminPassword = Read-Host -Prompt"Enter the administrator password for the virtual machine" -AsSecureString$dnsPrefix = Read-Host -Prompt"Enter the unique DNS Name for the Public IP used to access the virtual machine"$resourceGroupName = "${projectName}rg"$templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.recoveryservices/recovery-services-create-vm-and-configure-backup/main.bicep"New-AzResourceGroup -Name$resourceGroupName -Location$locationNew-AzResourceGroupDeployment -ResourceGroupName$resourceGroupName -TemplateUri$templateUri -projectName$projectName -adminUsername$adminUsername -adminPassword$adminPassword -dnsLabelPrefix$dnsPrefix
Validate the deployment
Start a backup job
The template creates a VM and enables backup on the VM. After you deploy the template, you need to start a backup job. For more information, see Start a backup job.
पुनर्प्राप्ति वॉल्ट और Azure बैकअप नीतियों को कार्यान्वित करना सीखने से पहले Azure बैकअप के बारे में जानें. Windows IaaS VM पुनर्प्राप्ति कार्यान्वित करना, ऑन-प्रिमाइसेस कार्यभार का बैकअप लेना और पुनर्स्थापित करना और Azure VM बैकअप प्रबंधित करना सीखें.