DEV Community

Sagar R Ravkhande
Sagar R Ravkhande

Posted on

Create Azure backup of VM using Automation script in AzCli

echo "Logging In...."
az login --service-principal -u $Clientid -p $Clientsecret --tenant $Tenantid

az account set -s "ba-ib-at23055-neu-dev"

RecoveryServicesVault="at23055vault-test"
resourceGroup="AT23055_DRMDASHBOARD_DEV"
vmName="xd934b23055dev2"

az backup vault show --name $RecoveryServicesVault --resource-group $resourceGroup

retVal=$?
if [ $retVal -eq 0 ]; then
        echo "Vault Exists Already"
else
        echo "Vault doesn't Exist!"
        echo "Creating a new Vault.."
        az backup vault create --resource-group $resourceGroup \
        --name $RecoveryServicesVault \
        --location northeurope
fi

az backup vault backup-properties set \
    --name $RecoveryServicesVault  \
    --resource-group $resourceGroup \
    --backup-storage-redundancy "LocallyRedundant"
#GeoRedundant


az backup protection check-vm --resource-group $resourceGroup --vm $vmName

if [ $retVal -eq 0 ]; then
        echo "Virtual machine is protected Already"
else
        echo "Virtual machine is not protected"
        echo "Creating Virtual machine Protection..."
        az backup protection enable-for-vm \
        --resource-group $resourceGroup \
        --vault-name $RecoveryServicesVault \
        --vm $vmName \
        --policy-name DefaultPolicy
fi

retention=`date +'%d-%m-%Y' -d "+1 year"`

count=$(az backup job list --resource-group $resourceGroup --vault-name $RecoveryServicesVault --output table | grep -i 'InProgress' | wc -l)

if [ $count -gt 0 ]; then
        echo "Backup is InProgress, Unable to initiate backup as another backup operation is currently in progress."
        echo "Checking the status of backup jobs..."
        az backup job list \
        --resource-group $resourceGroup \
        --vault-name $RecoveryServicesVault \
        --output table
        exit 0
else
        echo "Initiating backup job.."
        az backup protection backup-now \
        --resource-group $resourceGroup \
        --vault-name $RecoveryServicesVault \
        --container-name $vmName \
        --item-name $vmName \
        --backup-management-type AzureIaaSVM \
        --retain-until $retention
fi


echo "Checking the status of backup jobs..."
az backup job list \
    --resource-group $resourceGroup \
    --vault-name $RecoveryServicesVault \
    --output table

Enter fullscreen mode Exit fullscreen mode

Please find below the concise explanation of the provided code snippet that is intended to manage Azure backup vaults and virtual machines using the Azure CLI:

Explanation of the Code

  1. Login to Azure:
echo "Logging In...."  
az login --service-principal -u $Clientid -p $Clientsecret --tenant $Tenantid
Enter fullscreen mode Exit fullscreen mode

This logs into Azure using a service principal (a special Azure account typically used for automated scripts). It uses the client ID, client secret, and tenant ID provided in the variables.

  1. Set the Azure Account:
az account set -s "<Subscription_ID>"  
Enter fullscreen mode Exit fullscreen mode

Sets the specific Azure subscription to use for the subsequent commands.

  1. Define Variables:
RecoveryServicesVault="vault-test"  
resourceGroup="RG_DEV"  
vmName="xdvmdev2"  
Enter fullscreen mode Exit fullscreen mode

These lines define variables for the recovery services vault name, resource group, and virtual machine name.

  1. Check if the Backup Vault Exists:
az backup vault show --name $RecoveryServicesVault --resource-group $resourceGroup  
retVal=$?  
if [ $retVal -eq 0 ]; then  
    echo "Vault Exists Already"  
else  
    echo "Vault doesn't Exist!"  
    echo "Creating a new Vault.."  
    az backup vault create --resource-group $resourceGroup \
    --name $RecoveryServicesVault \
    --location northeurope  
fi  
Enter fullscreen mode Exit fullscreen mode
  • It checks if the specified backup vault exists.
  • If it does, it confirms its existence; if not, it creates a new backup vault in the specified location.
  1. Set Backup Properties:
az backup vault backup-properties set \
    --name $RecoveryServicesVault  \
    --resource-group $resourceGroup \
    --backup-storage-redundancy "LocallyRedundant"  
Enter fullscreen mode Exit fullscreen mode

This sets the backup storage redundancy to "LocallyRedundant," meaning that the backups will be stored in a way that protects them from local failures.

  1. Check VM Backup Protection Status:
az backup protection check-vm --resource-group $resourceGroup --vm $vmName  
Enter fullscreen mode Exit fullscreen mode

This checks if the specified virtual machine is already protected by the backup vault.

  1. Enable VM Protection if Not Already Protected:
if [ $retVal -eq 0 ]; then  
    echo "Virtual machine is protected Already"  
else  
    echo "Virtual machine is not protected"  
    echo "Creating Virtual machine Protection..."  
    az backup protection enable-for-vm \
    --resource-group $resourceGroup \
    --vault-name $RecoveryServicesVault \
    --vm $vmName \
    --policy-name DefaultPolicy  
fi  
Enter fullscreen mode Exit fullscreen mode

If the VM is not protected, it enables backup protection for the VM with the default policy.

  1. Initiate Backup Job:
retention=`date +'%d-%m-%Y' -d "+1 year"`  
count=$(az backup job list --resource-group $resourceGroup --vault-name $RecoveryServicesVault --output table | grep -i 'InProgress' | wc -l)  

if [ $count -gt 0 ]; then  
    echo "Backup is InProgress, Unable to initiate backup as another backup operation is currently in progress."  
    echo "Checking the status of backup jobs..."  
    az backup job list \
    --resource-group $resourceGroup \
    --vault-name $RecoveryServicesVault \
    --output table  
    exit 0  
else  
    echo "Initiating backup job.."  
    az backup protection backup-now \
    --resource-group $resourceGroup \
    --vault-name $RecoveryServicesVault \
    --container-name $vmName \
    --item-name $vmName \
    --backup-management-type AzureIaaSVM \
    --retain-until $retention  
fi  
Enter fullscreen mode Exit fullscreen mode
  • It defines a retention policy by calculating a date one year from now.
  • It checks if another backup job is in progress. If so, it displays the active jobs and exits.
  • If no jobs are in progress, it initiates a new backup job for the VM.
  1. Check Backup Job Status:
echo "Checking the status of backup jobs..."  
az backup job list \
    --resource-group $resourceGroup \
    --vault-name $RecoveryServicesVault \
    --output table  
Enter fullscreen mode Exit fullscreen mode

Finally, it retrieves and displays the status of all backup jobs associated with the vault.

Summary

This script automates the process of logging into Azure, checking or creating a backup vault, managing the backup protection of a specified VM, and initiating a backup job, while also checking for existing operations to avoid conflicts.

Top comments (0)