Restoring from a snapshot - azure

I have created a snapshot of my VM in Azure a few weeks ago using the script below.
$mydiskId = $(az vm show --resource-group "myResourceGroup" --name "myVM" --query "storageProfile.osDisk.managedDisk.id")
az snapshot create --name "myTestSnapShot" --resource-group --resource-group "MW-ENGINEERING-USEAST" --source $mydiskId
Now I am looking to restore that snapshot. Googling provides many links such as this. However, these talk about creating a recovery point/storage account as well. This left me confused as to when creating the snapshot, did I miss any of those steps. None of the steps described in the document for restoring the snapshot uses any id of the snapshot I created through the commands above. Can someone please help me understand what did I miss? TIA

Your linked document is to restore the whole VM or individual files. Before this, you need to enable Azure Backup to create recovery points that are stored in geo-redundant recovery vaults.
If you don't enable Azure Backup, consider there is a scenario where you want to get certain data from the snapshot without restoring the complete VM. In that case, one of the excellent ways is to create a VM from the snapshot and get the specific data that you need. In this way, you can create a different VM name and get the original data from the source VM. You can read this blog for more details.
To use Azure CLI to create an Azure VM from snapshots, read this for more details.
#Create snapshot
osdiskid=$(az vm show \
-g myResourceGroupDisk \
-n myVM \
--query "storageProfile.osDisk.managedDisk.id" \
-o tsv)
az snapshot create \
--resource-group myResourceGroupDisk \
--source "$osdiskid" \
--name osDisk-backup
#Create disk from snapshot
az disk create \
--resource-group myResourceGroupDisk \
--name mySnapshotDisk \
--source osDisk-backup
#Create a new virtual machine from the snapshot disk.
az vm create \
--resource-group myResourceGroupDisk \
--name myVM \
--attach-os-disk mySnapshotDisk \
--os-type linux

Related

how to delete relevant resources when deleting a virtual machine

I am using Azure CLI version 2.34.1. I ran following commands to create a resource group and then a virtual machine. Note that I used options to delete relevant resources when the VM is deleted.
az group create --name myTestRG --location eastus
az vm create --resource-group myTestRG --name myTestWindows11VM --image MicrosoftWindowsDesktop:windows-11:win11-21h2-pro:22000.493.220201 --admin-username someusername --os-disk-delete-option delete --nic-delete-option delete
Later I deleted the VM using following command.
az vm delete --name MyTestWin11VM --resource-group myTestRG -y
However, when I browse to the portal, the resource group still showing following resources that are relevant to the VM.
What I may be doing wrong? Is there anyway to delete all resources associated to VM when I delete the virtual machine itself?
UPDATE ITS A BUG:
The way Azure works is to group resources in Resource Groups - its a mandatory field in all creation of services. Azure does this because many resources have dependencies, such as a VM with a NIC, VNet & NSG.
You can use this to your advantage and simply delete the Resource Group:
az group delete --name myTestRG
Azure will work out the dependency order, eg NSG, VNet, NIC, VM. You can read up on how it does the ordering: https://learn.microsoft.com/en-us/azure/azure-resource-manager/management/delete-resource-group?tabs=azure-cli
What happens if I have multiple VMs in a Resource Group and I only want to delete one?
There's 3 new options --os-disk-delete-option, --data-disk-delete-option, --nic-delete-option to support deleting VMs and dependencies:
az vm create \
--resource-group myResourceGroup \
--name myVM \
--image UbuntuLTS \
--public-ip-sku Standard \
--nic-delete-option delete \
--os-disk-delete-option delete \
--admin-username azureuser \
--generate-ssh-keys
Otherwise script the whole thing using Azure Resource Manager Templates (ARM Templates), or the new tool to generate ARM Templates called Bicep. It's worth continuing with raw CLI commands and delete dependencies in order. IF you get good with the CLI you end up with a library of commands that you can use with ARM templates.

Cannot get Azure container network profile Id

We are actually deploying container to Azure using Azure CLI and the create command as specify the sample documentation below :
https://learn.microsoft.com/en-us/azure/container-instances/container-instances-vnet
In this dosucmentation it is clearly specify from the sample command below that when the container and the Vnet/Subnet gets created, azure create for you a Network Profile Id ( that is need for yaml deplyoement)
az container create --name appcontainer --resource-group myResourceGroup --image mcr.microsoft.com/azuredocs/aci-helloworld --vnet aci-vnet --vnet-address-prefix 10.0.0.0/16 --subnet aci-subnet --subnet-address-prefix 10.0.0.0/24
After the container gets created successfully you are supposed to get Network profile name or ID, which you can obtain using "az network profile list"
Which in fact does not return anything
UPDATE :
I update m Azure CLI to 2.30 in powershell but the result is the same the output of the command return nohing even if container and vnet gets succesfully created
Output result
Thanks for your help
regards
I have tested in my environment.
I deployed a container to a new virtual network using the below command:
az container create --name appcontainer --resource-group myResourceGroup --image mcr.microsoft.com/azuredocs/aci-helloworld --vnet aci-vnet --vnet-address-prefix 10.0.0.0/16 --subnet aci-subnet --subnet-address-prefix 10.0.0.0/24
The container got successfully created.
To get the Network Profile ID, I used the below command:
az network profile list --resource-group myResourceGroup --query [0].id --output tsv
In this way, we can fetch the Network Profile ID
If network profile is not getting created using CLI, try using ARM template
The same happened to me. I solve it using Azure CLI version 2.27.2. Any newer version leaves me with the same problem.
There seems to be a problem with the latest versions of the Azure CLI

Azure: Script or Command to delete instance and all its associated resources (disks, network interface)

I am trying to find a way to delete an Instance in azure and all its associated resources. But I don't see any straightforward approach to accomplish it. I am using az vm delete -g resourcegroup -n myinstancename--yes command which currently only deletes instances. In my scenario, I can't use powershell.
For testing ,I created a VM with 1 NIC, 1 Public IP and 2 Data disks.
Then, I used the below az CLI script :
$osDisk = (az vm show --resource-group ansumantest --name ansumantest --query "storageProfile.osDisk.name" --output tsv)
$datadisks = (az vm show --resource-group ansumantest --name ansumantest --query "storageProfile.dataDisks[].name" --output tsv)
$nics= (az vm show --resource-group ansumantest --name ansumantest --query "networkProfile.networkInterfaces[].id" --output tsv)
foreach ($nic in $nics){
$publicIps=az network nic show --id $nic --query "ipConfigurations[].publicIpAddress.id" --output tsv
}
az vm delete --resource-group ansumantest --name ansumantest --yes
if ($osDisk) {
az disk show --resource-group ansumantest --name $osDisk --yes
}
foreach ($datadisk in $Datadisks){
az disk delete --resource-group ansumantest --name $datadisk --yes
}
foreach ($nic in $nics){
az network nic delete --id $nic
}
foreach ($publicIp in $publicIps){
az network public-ip delete --id $publicIp
}
Outputs:
OR
You can directly delete all the resources while the running the VM delete Command as well but there are some Prerequisites for this method i.e. While creating the VM using CLI you have to configure couple of features like below :
As per Microsoft Document in az vm create section:
[--os-disk-delete-option {Delete, Detach}]
Specify the behavior of
the managed disk when the VM gets deleted i.e whether the managed
disk is deleted or detached.
accepted values: Delete, Detach
[--data-disk-delete-option]
Specify whether data disk should be deleted or detached upon VM deletion.
[--nic-delete-option]
Specify what happens to the network interface when the VM is
deleted. Use a singular value to apply on all resources, or use = to
configure the delete behavior for individual resources. Possible
options are Delete and Detach.
If the above 3 are configured to delete while creating the VM then , when you run the az vm delete it defaults to delete these resources when the VM is to be deleted.
Reference:
Github Support

How to specify Azure Linux OS Disk Type with Azure CLI Create VM

I am successfully creating a Linux VMs with the Azure CLI Command...
vm create --resource-group myResourceGroup --name myVMName --image credativ:Debian:9:latest --size Standard_B1s
I note that the resulting OS disk is a 30GiB Premium SSD.
Is it possible to stand up the same VM image to non-premium disk or a different size at the point the VM is created, or is this determined by the image I have requested the VM is based upon?
If you want to create the Azure VM with the non-premium disk, for example, HDD disk. You can use the CLI command like this:
az vm create -g charlesTestVM -n myVM1 --size Standard_B1s --image UbuntuLTS --storage-sku Standard_LRS --os-disk-size-gb 63
And the result below:
So it means you can use the CLI command az vm create with the parameter ----storage-sku to control the disk type and the parameter --os-disk-size-gb to control the os disk size.
Yes, you can. Below is a list of the az VM create options.
https://learn.microsoft.com/en-us/cli/azure/vm
Hope this helps.
The size name suffix "s" means that the image has SSD disk. You can choose other size like Standard_D1_v2.

Azure Container Group Instance

I am working with Azure Container Instance group.. and one of my containers is constantly restarting.. it goes to a terminated state and restarts. Everything looks good in logs.. The container is running a spring framework + React application. When I run the containers locally.. it works perfectly.
Am not sure what is happening behind the scenes?
You could use Azure CLI to set a restart policy of OnFailure or Never.
az container create \
--resource-group myResourceGroup \
--name mycontainer \
--image mycontainerimage \
--restart-policy OnFailure
If you specify the restart policy and the issue still exists,there might be some problems with the application or script executed in your container. You could use az container show command to check the restartCount property.
az container show --name
--resource-group
For more details, refer to this article.

Resources