/// <summary> /// remove the vmbackups with the metadata key "vmbackuptag" and value snapshotTag, snapshotTag is the parameter passed in. /// </summary> /// <param name="resourceGroupName"></param> /// <param name="vmName"></param> /// <param name="virtualMachineExtensionType"></param> /// <param name="location"></param> /// <param name="virtualMachineResponse"></param> /// <param name="profile"></param> /// <param name="VirtualMachineExtensionClient"></param> /// <param name="snapshotTag"></param> public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet) { var virtualMachineResponse = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(vmConfig.ResourceGroupName, vmConfig.VMName); StorageManagementClient storageClient = AzureSession.ClientFactory.CreateArmClient <StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription); List <string> blobUris = this.GetDiskBlobUris(virtualMachineResponse.Body); Dictionary <string, string> snapshotQuery = new Dictionary <string, string>(); snapshotQuery.Add(backupExtensionMetadataName, snapshotTag); List <CloudPageBlob> snapshots = this.FindSnapshot(blobUris, snapshotQuery, storageCredentialsFactory); if (snapshots == null || snapshots.Count == 0) { throw new AzureVMBackupException(AzureVMBackupErrorCodes.NoSnapshotFound, "snapshot with the tag not found."); } else { foreach (CloudPageBlob snapshot in snapshots) { snapshot.Delete(); } } }
/// <summary> /// remove the vmbackups with the metadata key "vmbackuptag" and value snapshotTag, snapshotTag is the parameter passed in. /// </summary> /// <param name="resourceGroupName"></param> /// <param name="vmName"></param> /// <param name="virtualMachineExtensionType"></param> /// <param name="location"></param> /// <param name="virtualMachineResponse"></param> /// <param name="profile"></param> /// <param name="VirtualMachineExtensionClient"></param> /// <param name="snapshotTag"></param> public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet) { var virtualMachineResponse = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(vmConfig.ResourceGroupName, vmConfig.VMName); StorageManagementClient storageClient = AzureSession.ClientFactory.CreateClient <StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription); List <string> blobUris = this.GetDiskBlobUris(virtualMachineResponse.Body); Dictionary <string, string> snapshotQuery = new Dictionary <string, string>(); List <CloudPageBlob> snapshots = this.FindSnapshot(blobUris, snapshotQuery, storageCredentialsFactory); foreach (CloudPageBlob snapshot in snapshots) { snapshot.Delete(); } }
/// <summary> /// we only support the Linux box now, if it's a windows, one AzureVMBackupException would be thrown. /// </summary> /// <param name="resourceGroupName"></param> /// <param name="vmName"></param> /// <param name="virtualMachineExtensionType"></param> /// <param name="location"></param> /// <param name="virtualMachineResponse"></param> /// <param name="snapshotTag"></param> /// <param name="virtualMachineExtensionBaseCmdlet"></param> public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet) { var virtualMachine = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView( vmConfig.ResourceGroupName, vmConfig.VMName); StorageManagementClient storageClient = AzureSession.ClientFactory.CreateArmClient <StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription); CloudPageBlobObjectFactory cloudPageBlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1)); List <string> vmPageBlobUris = this.GetDiskBlobUris(virtualMachine.Body); AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(vmPageBlobUris, cloudPageBlobObjectFactory); string taskId = Guid.NewGuid().ToString(); AzureVMBackupExtensionProtectedSettings privateConfig = new AzureVMBackupExtensionProtectedSettings(); privateConfig.logsBlobUri = string.Empty; privateConfig.objectStr = this.GetBase64Encoding(blobSASUris); AzureVMBackupExtensionPublicSettings publicConfig = new AzureVMBackupExtensionPublicSettings(); publicConfig.commandToExecute = backupSnapshotCommand; publicConfig.locale = backupDefaultLocale; publicConfig.commandStartTimeUTCTicks = DateTimeOffset.UtcNow.Ticks.ToString(); publicConfig.taskId = taskId; AzureVMBackupMetadata backupMetadata = new AzureVMBackupMetadata(); AzureVMBackupMetadataItem tagMetadataItem = new AzureVMBackupMetadataItem(); tagMetadataItem.Key = backupExtensionMetadataName; tagMetadataItem.Value = snapshotTag; AzureVMBackupMetadataItem taskIdMetadataItem = new AzureVMBackupMetadataItem(); taskIdMetadataItem.Key = backupExtensionIdentityMetadataName; taskIdMetadataItem.Value = taskId; backupMetadata.backupMetadata.Add(tagMetadataItem); backupMetadata.backupMetadata.Add(taskIdMetadataItem); publicConfig.objectStr = this.GetBase64Encoding(backupMetadata); VirtualMachineExtension vmExtensionParameters = new VirtualMachineExtension { Location = virtualMachine.Body.Location, Publisher = extensionPublisher, VirtualMachineExtensionType = extensionType, TypeHandlerVersion = extensionDefaultVersion, Settings = publicConfig, ProtectedSettings = privateConfig, }; var vmBackupOperation = virtualMachineExtensionBaseCmdlet.VirtualMachineExtensionClient.CreateOrUpdate( vmConfig.ResourceGroupName, vmConfig.VMName, vmConfig.ExtensionName ?? backupExtensionName, vmExtensionParameters); // check the snapshots with the task id are all created. int timePeriod = 5000; int loopingTimes = ((int)TimeSpan.FromMinutes(10).TotalMilliseconds / timePeriod); Dictionary <string, string> snapshotQuery = new Dictionary <string, string>(); snapshotQuery.Add(backupExtensionMetadataName, snapshotTag); snapshotQuery.Add(backupExtensionIdentityMetadataName, taskId); int i = 0; for (; i < loopingTimes; i++) { List <CloudPageBlob> snapshotsFound = this.FindSnapshot(vmPageBlobUris, snapshotQuery, storageCredentialsFactory); if (snapshotsFound.Count == vmPageBlobUris.Count) { break; } else { Thread.Sleep(timePeriod); } } if (i == loopingTimes) { throw new AzureVMBackupException(AzureVMBackupErrorCodes.TimeOut, "snapshot not created, or not found in time."); } }
/// <summary> /// we only support the Linux box now, if it's a windows, one AzureVMBackupException would be thrown. /// </summary> /// <param name="resourceGroupName"></param> /// <param name="vmName"></param> /// <param name="virtualMachineExtensionType"></param> /// <param name="location"></param> /// <param name="virtualMachineResponse"></param> /// <param name="snapshotTag"></param> /// <param name="virtualMachineExtensionBaseCmdlet"></param> public void CreateSnapshotForDisks(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet) { var virtualMachine = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView( vmConfig.ResourceGroupName, vmConfig.VMName); StorageManagementClient storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription); CloudPageBlobObjectFactory cloudPageBlobObjectFactory = new CloudPageBlobObjectFactory(storageCredentialsFactory, TimeSpan.FromMinutes(1)); List<string> vmPageBlobUris = this.GetDiskBlobUris(virtualMachine.Body); AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(vmPageBlobUris, cloudPageBlobObjectFactory); string taskId = Guid.NewGuid().ToString(); AzureVMBackupExtensionProtectedSettings privateConfig = new AzureVMBackupExtensionProtectedSettings(); privateConfig.logsBlobUri = string.Empty; privateConfig.objectStr = this.GetBase64Encoding(blobSASUris); AzureVMBackupExtensionPublicSettings publicConfig = new AzureVMBackupExtensionPublicSettings(); publicConfig.commandToExecute = backupSnapshotCommand; publicConfig.locale = backupDefaultLocale; publicConfig.commandStartTimeUTCTicks = DateTimeOffset.UtcNow.Ticks.ToString(); publicConfig.taskId = taskId; AzureVMBackupMetadata backupMetadata = new AzureVMBackupMetadata(); AzureVMBackupMetadataItem tagMetadataItem = new AzureVMBackupMetadataItem(); tagMetadataItem.Key = backupExtensionMetadataName; tagMetadataItem.Value = snapshotTag; AzureVMBackupMetadataItem taskIdMetadataItem = new AzureVMBackupMetadataItem(); taskIdMetadataItem.Key = backupExtensionIdentityMetadataName; taskIdMetadataItem.Value = taskId; backupMetadata.backupMetadata.Add(tagMetadataItem); backupMetadata.backupMetadata.Add(taskIdMetadataItem); publicConfig.objectStr = this.GetBase64Encoding(backupMetadata); VirtualMachineExtension vmExtensionParameters = new VirtualMachineExtension { Location = virtualMachine.Body.Location, Publisher = extensionPublisher, VirtualMachineExtensionType = extensionType, TypeHandlerVersion = extensionDefaultVersion, Settings = publicConfig, ProtectedSettings = privateConfig, }; var vmBackupOperation = virtualMachineExtensionBaseCmdlet.VirtualMachineExtensionClient.CreateOrUpdate( vmConfig.ResourceGroupName, vmConfig.VMName, vmConfig.ExtensionName ?? backupExtensionName, vmExtensionParameters); // check the snapshots with the task id are all created. int timePeriod = 5000; int loopingTimes = ((int)TimeSpan.FromMinutes(10).TotalMilliseconds / timePeriod); Dictionary<string, string> snapshotQuery = new Dictionary<string, string>(); snapshotQuery.Add(backupExtensionMetadataName, snapshotTag); snapshotQuery.Add(backupExtensionIdentityMetadataName, taskId); int i = 0; for (; i < loopingTimes; i++) { List<CloudPageBlob> snapshotsFound = this.FindSnapshot(vmPageBlobUris, snapshotQuery, storageCredentialsFactory); if (snapshotsFound.Count == vmPageBlobUris.Count) { break; } else { Thread.Sleep(timePeriod); } } if (i == loopingTimes) { throw new AzureVMBackupException(AzureVMBackupErrorCodes.TimeOut, "snapshot not created, or not found in time."); } }
/// <summary> /// remove the vmbackups with the metadata key "vmbackuptag" and value snapshotTag, snapshotTag is the parameter passed in. /// </summary> /// <param name="resourceGroupName"></param> /// <param name="vmName"></param> /// <param name="virtualMachineExtensionType"></param> /// <param name="location"></param> /// <param name="virtualMachineResponse"></param> /// <param name="profile"></param> /// <param name="VirtualMachineExtensionClient"></param> /// <param name="snapshotTag"></param> public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet) { var virtualMachineResponse = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(vmConfig.ResourceGroupName, vmConfig.VMName); StorageManagementClient storageClient = AzureSession.ClientFactory.CreateClient<StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription); List<string> blobUris = this.GetDiskBlobUris(virtualMachineResponse.Body); Dictionary<string, string> snapshotQuery = new Dictionary<string, string>(); List<CloudPageBlob> snapshots = this.FindSnapshot(blobUris, snapshotQuery, storageCredentialsFactory); foreach (CloudPageBlob snapshot in snapshots) { snapshot.Delete(); } }
/// <summary> /// remove the vmbackups with the metadata key "vmbackuptag" and value snapshotTag, snapshotTag is the parameter passed in. /// </summary> /// <param name="resourceGroupName"></param> /// <param name="vmName"></param> /// <param name="virtualMachineExtensionType"></param> /// <param name="location"></param> /// <param name="virtualMachineResponse"></param> /// <param name="profile"></param> /// <param name="VirtualMachineExtensionClient"></param> /// <param name="snapshotTag"></param> public void RemoveSnapshot(AzureVMBackupConfig vmConfig, string snapshotTag, VirtualMachineExtensionBaseCmdlet virtualMachineExtensionBaseCmdlet) { var virtualMachineResponse = virtualMachineExtensionBaseCmdlet.ComputeClient.ComputeManagementClient.VirtualMachines.GetWithInstanceView(vmConfig.ResourceGroupName, vmConfig.VMName); StorageManagementClient storageClient = AzureSession.ClientFactory.CreateArmClient<StorageManagementClient>(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, AzureEnvironment.Endpoint.ResourceManager); StorageCredentialsFactory storageCredentialsFactory = new StorageCredentialsFactory(vmConfig.ResourceGroupName, storageClient, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context.Subscription); List<string> blobUris = this.GetDiskBlobUris(virtualMachineResponse.Body); AzureVMBackupBlobSasUris blobSASUris = this.GenerateBlobSasUris(blobUris, virtualMachineExtensionBaseCmdlet.DefaultProfile.Context); Dictionary<string, string> snapshotQuery = new Dictionary<string, string>(); snapshotQuery.Add(backupExtensionMetadataName, snapshotTag); List<CloudPageBlob> snapshots = this.FindSnapshot(virtualMachineExtensionBaseCmdlet.DefaultProfile.Context, blobSASUris.pageBlobUri, blobSASUris.storageCredentialsFactory, snapshotQuery); if (snapshots == null || snapshots.Count == 0) { throw new AzureVMBackupException(AzureVMBackupErrorCodes.NoSnapshotFound, "snapshot with the tag not found."); } else { foreach (CloudPageBlob snapshot in snapshots) { snapshot.Delete(); } } }