https://pulumi.com logo
#azure
Title
# azure
b

bumpy-glass-30283

09/08/2023, 8:22 AM
Is there any way to create a new disk/VM from a backup/restorepoint? cannot find anything about this. I was looking around the https://www.pulumi.com/registry/packages/azure-native/api-docs/dataprotection/ module, but nothing stands out. Even in azure cli and REST API it seems convoluted, and it's a different API (Data Protection vs. Backup): https://learn.microsoft.com/en-us/azure/backup/backup-azure-arm-userestapi-restoreazurevms
In case any1 stumbles upon this searching for an answer: the solution is to get a Restore Point for the disk.
Copy code
import pulumi
from pulumi_azure_native import compute, network, resources
# Restoration Point parameters
RP_RG_name = "<RP RG name>"
RP_collection_name = "RP collection name"
RP_name = "RP name"
# Target VM parameters
target_RG_name = "<Target RG name>"
target_Network_name = "<Target network name>"
target_Subnet_name = "<Target Subnet name>"

# Get the resource group resource
resource_group = resources.get_resource_group(resource_group_name=target_RG_name)
# Get the virtual network resource
virtual_network = network.get_virtual_network(
    resource_group_name=resource_group.name,
    virtual_network_name=target_Network_name,
)
# Get the subnet resource
subnet = network.get_subnet(
    resource_group_name=resource_group.name,
    virtual_network_name=virtual_network.name,
    subnet_name=target_Subnet_name,
)
# Get the restore point you want to restore from
# Start with `az restore-point collection list-all`
restore_point = compute.get_restore_point(
    resource_group_name=RP_RG_name,
    restore_point_collection_name=RP_collection_name,
    restore_point_name=RP_name,
)
# Create a disk from the Restore Point
disk = compute.Disk(
    "restore_disk",
    resource_group_name=resource_group.name,
    creation_data=compute.CreationDataArgs(
        create_option=compute.DiskCreateOption.RESTORE,
        source_resource_id=restore_point.source_metadata.storage_profile.os_disk.disk_restore_point.id,
    ),
)
# Create a network interface for our VM
network_interface = network.NetworkInterface(
    "restorer_vm_nic",
    resource_group_name=resource_group.name,
    ip_configurations=[
        network.NetworkInterfaceIPConfigurationArgs(
            name="ipconfig",
            subnet=network.SubnetArgs(id=subnet.id),
            private_ip_allocation_method=network.IPAllocationMethod.DYNAMIC,
        )
    ],
)
# Create the VM and attach the disk to it
vm = compute.VirtualMachine(
    "restored_vm",
    network_profile=compute.NetworkProfileArgs(
        network_interfaces=[
            compute.NetworkInterfaceReferenceArgs(id=network_interface.id)
        ],
    ),
    hardware_profile=compute.HardwareProfileArgs(
        # Select the preferred VM size
        vm_size=compute.VirtualMachineSizeTypes.STANDARD_B1S,
    ),
    resource_group_name=resource_group.name,
    storage_profile=compute.StorageProfileArgs(
        os_disk=compute.OSDiskArgs(
            create_option=compute.DiskCreateOption.ATTACH,
            name=disk.name,
            os_type=compute.OperatingSystemTypes.LINUX,
            managed_disk=compute.ManagedDiskParametersArgs(
                id=disk.id,
            ),
        ),
    ),
)
# Export some data of the restored VM
pulumi.export("virtualMachineId", vm.id)
pulumi.export("diskId", disk.id)
pulumi.export("restorePointId", restore_point.id)
pulumi.export("VM private IP address", network_interface.ip_configurations[0].private_ip_address)