|
| 1 | +--- |
| 2 | +title: Quickstart - Bicep template VM Backup |
| 3 | +description: Learn how to back up your virtual machines with a Bicep template |
| 4 | +ms.devlang: azurecli |
| 5 | +ms.topic: quickstart |
| 6 | +ms.date: 11/17/2021 |
| 7 | +ms.reviewer: Daya-Patil |
| 8 | +ms.custom: mvc,subject-bicepqs, devx-track-azurepowershell |
| 9 | +author: v-amallick |
| 10 | +ms.service: backup |
| 11 | +ms.author: v-amallick |
| 12 | +--- |
| 13 | + |
| 14 | +# Back up a virtual machine in Azure with a Bicep template |
| 15 | + |
| 16 | +[Azure Backup](backup-overview.md) 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](../azure-resource-manager/bicep/deploy-cli.md) and the [template reference](/azure/templates/microsoft.recoveryservices/allversions). |
| 17 | + |
| 18 | +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. |
| 19 | + |
| 20 | +Resource types, API versions, and properties that are valid in an ARM template, are also valid in a Bicep file. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +To set up your environment for Bicep development, see [Install Bicep tools](../azure-resource-manager/bicep/install.md). |
| 25 | + |
| 26 | +>[!Note] |
| 27 | +>Install the latest [Azure PowerShell module](/powershell/azure/new-azureps-module-az?view=azps-6.6.0) and the Bicep CLI as detailed in article. |
| 28 | +
|
| 29 | +## Review the template |
| 30 | + |
| 31 | +The template used below is from [Azure quickstart templates](https://azure.microsoft.com/resources/templates/recovery-services-create-vm-and-configure-backup/). This template allows you to deploy simple Windows VM and Recovery Services vault configured with _DefaultPolicy_ for _Protection_. |
| 32 | + |
| 33 | +```json |
| 34 | +@description('Specifies a name for generating resource names.') |
| 35 | +@maxLength(8) |
| 36 | +param projectName string |
| 37 | + |
| 38 | +@description('Specifies the location for all resources.') |
| 39 | +param location string = resourceGroup().location |
| 40 | + |
| 41 | +@description('Specifies the administrator username for the Virtual Machine.') |
| 42 | +param adminUsername string |
| 43 | + |
| 44 | +@description('Specifies the administrator password for the Virtual Machine.') |
| 45 | +@secure() |
| 46 | +param adminPassword string |
| 47 | + |
| 48 | +@description('Specifies the unique DNS Name for the Public IP used to access the Virtual Machine.') |
| 49 | +param dnsLabelPrefix string |
| 50 | + |
| 51 | +@description('Virtual machine size.') |
| 52 | +param vmSize string = 'Standard_A2' |
| 53 | + |
| 54 | +@description('Specifies the Windows version for the VM. This will pick a fully patched image of this given Windows version.') |
| 55 | +@allowed([ |
| 56 | + '2008-R2-SP1' |
| 57 | + '2012-Datacenter' |
| 58 | + '2012-R2-Datacenter' |
| 59 | + '2016-Nano-Server' |
| 60 | + '2016-Datacenter-with-Containers' |
| 61 | + '2016-Datacenter' |
| 62 | + '2019-Datacenter' |
| 63 | + '2019-Datacenter-Core' |
| 64 | + '2019-Datacenter-Core-smalldisk' |
| 65 | + '2019-Datacenter-Core-with-Containers' |
| 66 | + '2019-Datacenter-Core-with-Containers-smalldisk' |
| 67 | + '2019-Datacenter-smalldisk' |
| 68 | + '2019-Datacenter-with-Containers' |
| 69 | + '2019-Datacenter-with-Containers-smalldisk' |
| 70 | +]) |
| 71 | +param windowsOSVersion string = '2016-Datacenter' |
| 72 | + |
| 73 | +var storageAccountName = '${projectName}store' |
| 74 | +var networkInterfaceName = '${projectName}-nic' |
| 75 | +var vNetAddressPrefix = '10.0.0.0/16' |
| 76 | +var vNetSubnetName = 'default' |
| 77 | +var vNetSubnetAddressPrefix = '10.0.0.0/24' |
| 78 | +var publicIPAddressName = '${projectName}-ip' |
| 79 | +var vmName = '${projectName}-vm' |
| 80 | +var vNetName = '${projectName}-vnet' |
| 81 | +var vaultName = '${projectName}-vault' |
| 82 | +var backupFabric = 'Azure' |
| 83 | +var backupPolicyName = 'DefaultPolicy' |
| 84 | +var protectionContainer = 'iaasvmcontainer;iaasvmcontainerv2;${resourceGroup().name};${vmName}' |
| 85 | +var protectedItem = 'vm;iaasvmcontainerv2;${resourceGroup().name};${vmName}' |
| 86 | +var networkSecurityGroupName = 'default-NSG' |
| 87 | + |
| 88 | +resource storageAccount 'Microsoft.Storage/storageAccounts@2021-06-01' = { |
| 89 | + name: storageAccountName |
| 90 | + location: location |
| 91 | + sku: { |
| 92 | + name: 'Standard_LRS' |
| 93 | + } |
| 94 | + kind: 'Storage' |
| 95 | + properties: {} |
| 96 | +} |
| 97 | + |
| 98 | +resource publicIPAddress 'Microsoft.Network/publicIPAddresses@2020-06-01' = { |
| 99 | + name: publicIPAddressName |
| 100 | + location: location |
| 101 | + properties: { |
| 102 | + publicIPAllocationMethod: 'Dynamic' |
| 103 | + dnsSettings: { |
| 104 | + domainNameLabel: dnsLabelPrefix |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +resource networkSecurityGroup 'Microsoft.Network/networkSecurityGroups@2020-06-01' = { |
| 110 | + name: networkSecurityGroupName |
| 111 | + location: location |
| 112 | + properties: { |
| 113 | + securityRules: [ |
| 114 | + { |
| 115 | + name: 'default-allow-3389' |
| 116 | + properties: { |
| 117 | + priority: 1000 |
| 118 | + access: 'Allow' |
| 119 | + direction: 'Inbound' |
| 120 | + destinationPortRange: '3389' |
| 121 | + protocol: 'Tcp' |
| 122 | + sourceAddressPrefix: '*' |
| 123 | + sourcePortRange: '*' |
| 124 | + destinationAddressPrefix: '*' |
| 125 | + } |
| 126 | + } |
| 127 | + ] |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +resource vNet 'Microsoft.Network/virtualNetworks@2020-06-01' = { |
| 132 | + name: vNetName |
| 133 | + location: location |
| 134 | + properties: { |
| 135 | + addressSpace: { |
| 136 | + addressPrefixes: [ |
| 137 | + vNetAddressPrefix |
| 138 | + ] |
| 139 | + } |
| 140 | + subnets: [ |
| 141 | + { |
| 142 | + name: vNetSubnetName |
| 143 | + properties: { |
| 144 | + addressPrefix: vNetSubnetAddressPrefix |
| 145 | + networkSecurityGroup: { |
| 146 | + id: networkSecurityGroup.id |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + ] |
| 151 | + } |
| 152 | +} |
| 153 | + |
| 154 | +resource networkInterface 'Microsoft.Network/networkInterfaces@2020-06-01' = { |
| 155 | + name: networkInterfaceName |
| 156 | + location: location |
| 157 | + properties: { |
| 158 | + ipConfigurations: [ |
| 159 | + { |
| 160 | + name: 'ipconfig1' |
| 161 | + properties: { |
| 162 | + privateIPAllocationMethod: 'Dynamic' |
| 163 | + publicIPAddress: { |
| 164 | + id: publicIPAddress.id |
| 165 | + } |
| 166 | + subnet: { |
| 167 | + id: '${vNet.id}/subnets/${vNetSubnetName}' |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + ] |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +resource virtualMachine 'Microsoft.Compute/virtualMachines@2020-06-01' = { |
| 176 | + name: vmName |
| 177 | + location: location |
| 178 | + properties: { |
| 179 | + hardwareProfile: { |
| 180 | + vmSize: vmSize |
| 181 | + } |
| 182 | + osProfile: { |
| 183 | + computerName: vmName |
| 184 | + adminUsername: adminUsername |
| 185 | + adminPassword: adminPassword |
| 186 | + } |
| 187 | + storageProfile: { |
| 188 | + imageReference: { |
| 189 | + publisher: 'MicrosoftWindowsServer' |
| 190 | + offer: 'WindowsServer' |
| 191 | + sku: windowsOSVersion |
| 192 | + version: 'latest' |
| 193 | + } |
| 194 | + osDisk: { |
| 195 | + createOption: 'FromImage' |
| 196 | + } |
| 197 | + dataDisks: [ |
| 198 | + { |
| 199 | + diskSizeGB: 1023 |
| 200 | + lun: 0 |
| 201 | + createOption: 'Empty' |
| 202 | + } |
| 203 | + ] |
| 204 | + } |
| 205 | + networkProfile: { |
| 206 | + networkInterfaces: [ |
| 207 | + { |
| 208 | + id: networkInterface.id |
| 209 | + } |
| 210 | + ] |
| 211 | + } |
| 212 | + diagnosticsProfile: { |
| 213 | + bootDiagnostics: { |
| 214 | + enabled: true |
| 215 | + storageUri: storageAccount.properties.primaryEndpoints.blob |
| 216 | + } |
| 217 | + } |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +resource recoveryServicesVault 'Microsoft.RecoveryServices/vaults@2020-02-02' = { |
| 222 | + name: vaultName |
| 223 | + location: location |
| 224 | + sku: { |
| 225 | + name: 'RS0' |
| 226 | + tier: 'Standard' |
| 227 | + } |
| 228 | + properties: {} |
| 229 | +} |
| 230 | + |
| 231 | +resource vaultName_backupFabric_protectionContainer_protectedItem 'Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems@2020-02-02' = { |
| 232 | + name: '${vaultName}/${backupFabric}/${protectionContainer}/${protectedItem}' |
| 233 | + properties: { |
| 234 | + protectedItemType: 'Microsoft.Compute/virtualMachines' |
| 235 | + policyId: '${recoveryServicesVault.id}/backupPolicies/${backupPolicyName}' |
| 236 | + sourceResourceId: virtualMachine.id |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +``` |
| 241 | + |
| 242 | +The resources defined in the above template are: |
| 243 | + |
| 244 | +- [Microsoft.Storage/storageAccounts](/azure/templates/microsoft.storage/storageaccounts) |
| 245 | +- [Microsoft.Network/publicIPAddresses](/azure/templates/microsoft.network/publicipaddresses) |
| 246 | +- [Microsoft.Network/networkSecurityGroups](/azure/templates/microsoft.network/networksecuritygroups) |
| 247 | +- [Microsoft.Network/virtualNetworks](/azure/templates/microsoft.network/virtualnetworks) |
| 248 | +- [Microsoft.Network/networkInterfaces](/azure/templates/microsoft.network/networkinterfaces) |
| 249 | +- [Microsoft.Compute/virtualMachines](/azure/templates/microsoft.compute/virtualmachines) |
| 250 | +- [Microsoft.RecoveryServices/vaults](/azure/templates/microsoft.recoveryservices/2016-06-01/vaults) |
| 251 | +- [Microsoft.RecoveryServices/vaults/backupFabrics/protectionContainers/protectedItems](/azure/templates/microsoft.recoveryservices/vaults/backupfabrics/protectioncontainers/protecteditems) |
| 252 | + |
| 253 | +## Deploy the template |
| 254 | + |
| 255 | +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**. |
| 256 | + |
| 257 | +```azurepowershell-interactive |
| 258 | +$projectName = Read-Host -Prompt "Enter a project name (limited to eight characters) that is used to generate Azure resource names" |
| 259 | +$location = Read-Host -Prompt "Enter the location (for example, centralus)" |
| 260 | +$adminUsername = Read-Host -Prompt "Enter the administrator username for the virtual machine" |
| 261 | +$adminPassword = Read-Host -Prompt "Enter the administrator password for the virtual machine" -AsSecureString |
| 262 | +$dnsPrefix = Read-Host -Prompt "Enter the unique DNS Name for the Public IP used to access the virtual machine" |
| 263 | +
|
| 264 | +$resourceGroupName = "${projectName}rg" |
| 265 | +$templateUri = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/quickstarts/microsoft.recoveryservices/recovery-services-create-vm-and-configure-backup/main.bicep" |
| 266 | +
|
| 267 | +New-AzResourceGroup -Name $resourceGroupName -Location $location |
| 268 | +New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri $templateUri -projectName $projectName -adminUsername $adminUsername -adminPassword $adminPassword -dnsLabelPrefix $dnsPrefix |
| 269 | +
|
| 270 | +``` |
| 271 | + |
| 272 | +## Validate the deployment |
| 273 | + |
| 274 | +### Start a backup job |
| 275 | + |
| 276 | +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](quick-backup-vm-powershell.md#start-a-backup-job). |
| 277 | + |
| 278 | +### Monitor the backup job |
| 279 | + |
| 280 | +To monitor the backup job, see [Monitor the backup job](quick-backup-vm-powershell.md#monitor-the-backup-job). |
| 281 | + |
| 282 | +## Clean up resources |
| 283 | + |
| 284 | +- If you no longer need to back up the VM, you can clean it up. |
| 285 | +- To try out restoring the VM, skip the clean-up process. |
| 286 | +- If you’ve used an existing VM, you can skip the final [Remove-AzResourceGroup](/powershell/module/az.resources/remove-azresourcegroup) cmdlet to keep the resource group and VM. |
| 287 | + |
| 288 | +Follow these steps: |
| 289 | + |
| 290 | +1. Disable protection, remove the restore points and vault. |
| 291 | + |
| 292 | +1. Delete the resource group and associated VM resources, as follows: |
| 293 | + |
| 294 | + ```azurepowershell |
| 295 | + Disable-AzRecoveryServicesBackupProtection -Item $item -RemoveRecoveryPoints |
| 296 | + $vault = Get-AzRecoveryServicesVault -Name "myRecoveryServicesVault" |
| 297 | + Remove-AzRecoveryServicesVault -Vault $vault |
| 298 | + Remove-AzResourceGroup -Name "myResourceGroup" |
| 299 | +
|
| 300 | + ``` |
| 301 | + |
| 302 | +## Next steps |
| 303 | + |
| 304 | +In this quickstart, you created a Recovery Services vault, enabled protection on a VM, and created the initial recovery point. |
| 305 | + |
| 306 | +- [Learn how](tutorial-backup-vm-at-scale.md) to back up VMs in the Azure portal. |
| 307 | +- [Learn how](tutorial-restore-disk.md) to quickly restore a VM |
| 308 | +- [Learn how](../azure-resource-manager/bicep/quickstart-create-bicep-use-visual-studio-code.md?tabs=CLI) to create Bicep templates. |
| 309 | +- [Learn how](../azure-resource-manager/bicep/decompile.md?tabs=azure-cli) to decompile Azure Resource Manager templates (ARM templates) to Bicep files. |
| 310 | + |
| 311 | + |
0 commit comments