"URGENT PLEASE" How can I automate Taking snapShots daily, Remove SnapShots daily from Azure VMs?

r001 0 Reputation points
2024-03-06T13:19:50.9466667+00:00

"URGENT PLEASE"

I am struggling to setup a automation of Azure Virtual Machines in Production for taking snapshots daily with their tags, remove that snapshots daily.

Thank you
Rips

Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,875 questions
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,256 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Monalla-MSFT 12,946 Reputation points
    2024-03-06T16:34:24.21+00:00

    @r001 - Welcome to Microsoft Q&A and thanks for reaching out to us.

    In order to set up automation of Azure Virtual Machines in Production you can use Azure Automation Runbooks.

    You can follow below for a high-level steps to achieve the same:

    1. Create an Azure Automation account in your subscription and create a new runbook in the account.
    2. Write a PowerShell script to take a snapshot of the VMs and tag them with the current date.
    3. Schedule the Runbook to run daily at a specific time.
    4. Write another PowerShell script to remove the snapshots that were created by the Runbook.
    5. Schedule the second Runbook to run daily at a specific time.

    Here is an example PowerShell script that you can use to take a snapshot of a VM and tag it with the current date:

    # Get the VM object
    $vm = Get-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM" 
    # Get the current date
    $date = Get-Date -Format "yyyy-MM-dd" 
    # Create the snapshot 
    $snapshot = New-AzSnapshotConfig -SourceUri $vm.StorageProfile.OsDisk.ManagedDisk.Id -CreateOption Copy -Location $vm.Location -Tags @{ Created=$date } New-AzSnapshot -Snapshot $snapshot -SnapshotName "mySnapshot" -ResourceGroupName "myResourceGroup"
    
    

    And here is an example PowerShell script that you can use to remove the snapshots that were created by the Runbook:

    # Get the snapshots that were created by the Runbook
    $snapshots = Get-AzSnapshot -ResourceGroupName "myResourceGroup" | Where-Object { $_.Tags.Created -eq (Get-Date -Format "yyyy-MM-dd") } 
    # Remove the snapshots 
    $snapshots | Remove-AzSnapshot
    

    Please note that these scripts are just examples, and you may need to modify them to fit your specific requirements. Also, make sure to test the scripts in a non-production environment before running them in production.

    Hope this helps. and please feel free to reach out if you have any further questions.


    If the above response was helpful, please feel free to "Accept as Answer" and click "Yes" so it can be beneficial to the community.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.