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
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
- Login to Azure:
echo "Logging In...."
az login --service-principal -u $Clientid -p $Clientsecret --tenant $Tenantid
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.
- Set the Azure Account:
az account set -s ""
Sets the specific Azure subscription to use for the subsequent commands.
- Define Variables:
RecoveryServicesVault="vault-test"
resourceGroup="RG_DEV"
vmName="xdvmdev2"
These lines define variables for the recovery services vault name, resource group, and virtual machine name.
- 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
- 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.
- Set Backup Properties:
az backup vault backup-properties set \
--name $RecoveryServicesVault \
--resource-group $resourceGroup \
--backup-storage-redundancy "LocallyRedundant"
This sets the backup storage redundancy to "LocallyRedundant," meaning that the backups will be stored in a way that protects them from local failures.
- Check VM Backup Protection Status:
az backup protection check-vm --resource-group $resourceGroup --vm $vmName
This checks if the specified virtual machine is already protected by the backup vault.
- 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
If the VM is not protected, it enables backup protection for the VM with the default policy.
- 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
- 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.
- Check Backup Job Status:
echo "Checking the status of backup jobs..."
az backup job list \
--resource-group $resourceGroup \
--vault-name $RecoveryServicesVault \
--output table
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.