// <summary> /// Helper function to convert ps recovery point model from service response. /// </summary> public static RecoveryPointBase GetPSAzureRecoveryPoints(ServiceClientModel.RecoveryPointResponse rpResponse, AzureVmItem item) { if (rpResponse == null || rpResponse.RecPoint == null) { throw new ArgumentNullException(Resources.GetRPResponseIsNull); } ServiceClientModel.RecoveryPoint recPoint = rpResponse.RecPoint.Properties as ServiceClientModel.RecoveryPoint; Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string protectedItemName = HelperUtils.GetProtectedItemUri(uriDict, item.Id); DateTime recPointTime = DateTime.ParseExact( recPoint.RecoveryPointTime, @"MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); AzureVmRecoveryPoint result = new AzureVmRecoveryPoint() { RecoveryPointId = rpResponse.RecPoint.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerUri, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rpResponse.RecPoint.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, SourceResourceId = item.SourceResourceId, }; return(result); }
/// <summary> /// Restores the disk based on the recovery point and other input parameters /// </summary> /// <param name="rp">Recovery point to restore the disk to</param> /// <param name="storageAccountId">ID of the storage account where to restore the disk</param> /// <param name="storageAccountLocation">Location of the storage account where to restore the disk</param> /// <param name="storageAccountType">Type of the storage account where to restore the disk</param> /// <returns>Job created by this operation</returns> public RestAzureNS.AzureOperationResponse RestoreDisk( AzureVmRecoveryPoint rp, string storageAccountId, string storageAccountLocation, string storageAccountType, bool osaOption) { var useOsa = ShouldUseOsa(rp, osaOption); string resourceGroupName = BmsAdapter.GetResourceGroupName(); string resourceName = BmsAdapter.GetResourceName(); string vaultLocation = BmsAdapter.GetResourceLocation(); Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(rp.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, rp.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, rp.Id); string recoveryPointId = rp.RecoveryPointId; //validtion block if (storageAccountLocation != vaultLocation) { throw new Exception(Resources.RestoreDiskIncorrectRegion); } string vmType = containerUri.Split(';')[1].Equals("iaasvmcontainer", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; string strType = storageAccountType.Equals("Microsoft.ClassicStorage/StorageAccounts", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; if (vmType != strType) { throw new Exception(string.Format(Resources.RestoreDiskStorageTypeError, vmType)); } IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest() { CreateNewCloudService = false, RecoveryPointId = recoveryPointId, RecoveryType = RecoveryType.RestoreDisks, Region = vaultLocation, StorageAccountId = storageAccountId, SourceResourceId = rp.SourceResourceId, OriginalStorageAccountOption = useOsa, }; RestoreRequestResource triggerRestoreRequest = new RestoreRequestResource(); triggerRestoreRequest.Properties = restoreRequest; var response = BmsAdapter.Client.Restores.TriggerWithHttpMessagesAsync( resourceName, resourceGroupName, AzureFabricName, containerUri, protectedItemUri, recoveryPointId, triggerRestoreRequest, cancellationToken: BmsAdapter.CmdletCancellationToken).Result; return(response); }
public BaseRecoveryServicesJobResponse RestoreDisk(AzureVmRecoveryPoint rp, string storageAccountId, string storageAccountLocation, string storageAccountType) { string resourceGroupName = BmsAdapter.GetResourceGroupName(); string resourceName = BmsAdapter.GetResourceName(); string vaultLocation = BmsAdapter.GetResourceLocation(); string containerName = rp.ContainerName; string protectedItemName = rp.ItemName; string recoveryPointId = rp.RecoveryPointId; //validtion block if (storageAccountLocation != vaultLocation) { throw new Exception(Resources.RestoreDiskIncorrectRegion); } string vmType = containerName.Split(';')[1].Equals("iaasvmcontainer", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; string strType = storageAccountType.Equals("Microsoft.ClassicStorage/StorageAccounts", StringComparison.OrdinalIgnoreCase) ? "Classic" : "Compute"; if (vmType != strType) { throw new Exception(String.Format(Resources.RestoreDiskStorageTypeError, vmType)); } IaasVMRestoreRequest restoreRequest = new IaasVMRestoreRequest() { AffinityGroup = String.Empty, CloudServiceOrResourceGroup = String.Empty, CreateNewCloudService = false, RecoveryPointId = recoveryPointId, RecoveryType = RecoveryType.RestoreDisks, Region = vaultLocation, StorageAccountId = storageAccountId, SubnetId = string.Empty, VirtualMachineName = string.Empty, VirtualNetworkId = string.Empty, }; TriggerRestoreRequest triggerRestoreRequest = new TriggerRestoreRequest(); triggerRestoreRequest.Item = new RestoreRequestResource(); triggerRestoreRequest.Item.Properties = new RestoreRequest(); triggerRestoreRequest.Item.Properties = restoreRequest; var response = BmsAdapter.Client.Restores.TriggerRestoreAsync( resourceGroupName, resourceName, BmsAdapter.GetCustomRequestHeaders(), AzureFabricName, containerName, protectedItemName, recoveryPointId, triggerRestoreRequest, BmsAdapter.CmdletCancellationToken).Result; return(response); }
/// <summary> /// Triggers the recovery operation for the given recovery point /// </summary> /// <returns>The job response returned from the service</returns> public BaseRecoveryServicesJobResponse TriggerRestore() { AzureVmRecoveryPoint rp = ProviderData[RestoreBackupItemParams.RecoveryPoint] as AzureVmRecoveryPoint; string storageAccountId = ProviderData[RestoreBackupItemParams.StorageAccountId].ToString(); string storageAccountLocation = ProviderData[RestoreBackupItemParams.StorageAccountLocation].ToString(); string storageAccountType = ProviderData[RestoreBackupItemParams.StorageAccountType].ToString(); var response = ServiceClientAdapter.RestoreDisk(rp, storageAccountId, storageAccountLocation, storageAccountType); return(response); }
private bool ShouldUseOsa(AzureVmRecoveryPoint rp, bool osaOption) { bool useOsa = false; if (osaOption) { if (rp.OriginalSAEnabled) { useOsa = true; } else { throw new Exception("This recovery point doesn’t have the capability to restore disks to their original storage account. Re-run the restore command without the UseOriginalStorageAccountForDisks parameter."); } } return(useOsa); }
/// <summary> /// Helper function to convert ps recovery points list model from service response. /// </summary> public static List <RecoveryPointBase> GetPSAzureRecoveryPoints(ServiceClientModel.RecoveryPointListResponse rpList, AzureVmItem item) { if (rpList == null || rpList.RecoveryPointList == null || rpList.RecoveryPointList.RecoveryPoints == null) { throw new ArgumentNullException("RPList"); } Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string protectedItemName = HelperUtils.GetProtectedItemUri(uriDict, item.Id); List <RecoveryPointBase> result = new List <RecoveryPointBase>(); foreach (ServiceClientModel.RecoveryPointResource rp in rpList.RecoveryPointList.RecoveryPoints) { ServiceClientModel.RecoveryPoint recPoint = rp.Properties as ServiceClientModel.RecoveryPoint; DateTime recPointTime = DateTime.ParseExact(recPoint.RecoveryPointTime, @"MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerUri, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, SourceVMStorageType = recPoint.SourceVMStorageType, SourceResourceId = item.SourceResourceId, }; result.Add(rpBase); } return(result); }
public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; Dictionary <Enum, object> providerParameters = new Dictionary <Enum, object>(); string secondaryRegion = ""; if (RestoreToSecondaryRegion.IsPresent) { ARSVault vault = ServiceClientAdapter.GetVault(resourceGroupName, vaultName); secondaryRegion = BackupUtils.regionMap[vault.Location]; providerParameters.Add(CRRParams.SecondaryRegion, secondaryRegion); } if (RehydratePriority != null) { Logger.Instance.WriteDebug("Rehydrate priority is " + RehydratePriority); int rehydrateExpiryInDays = Int32.Parse(RehydrateDuration); // Rehydrate duration is taken as days if (rehydrateExpiryInDays < 10 || rehydrateExpiryInDays > 30) { throw new ArgumentException(Resources.InvalidRehydrateDuration); } providerParameters.Add(RecoveryPointParams.RehydrateDuration, RehydrateDuration); providerParameters.Add(RecoveryPointParams.RehydratePriority, RehydratePriority); } if (UseSystemAssignedIdentity.IsPresent || UserAssignedIdentityId != null) { if (UseSystemAssignedIdentity.IsPresent && UserAssignedIdentityId != null) { throw new ArgumentException(Resources.MultipleMSIProvidedForRestore); } /*if (UseSystemAssignedIdentity.IsPresent) * { * // check to be added on vault.Identity * } * else * { * // check whether given User MSI is present on vault * }*/ } string RestoreType = null; if (string.Compare(ParameterSetName, AzureManagedVMReplaceExistingParameterSet) == 0) { RestoreType = "OriginalLocation"; } providerParameters.Add(VaultParams.VaultName, vaultName); providerParameters.Add(VaultParams.ResourceGroupName, resourceGroupName); providerParameters.Add(VaultParams.VaultLocation, VaultLocation); providerParameters.Add(RestoreBackupItemParams.RecoveryPoint, RecoveryPoint); providerParameters.Add(RestoreVMBackupItemParams.OsaOption, UseOriginalStorageAccount.IsPresent); providerParameters.Add(RestoreFSBackupItemParams.ResolveConflict, ResolveConflict.ToString()); providerParameters.Add(RestoreFSBackupItemParams.SourceFilePath, SourceFilePath); providerParameters.Add(RestoreFSBackupItemParams.TargetStorageAccountName, TargetStorageAccountName); providerParameters.Add(RestoreFSBackupItemParams.TargetFileShareName, TargetFileShareName); providerParameters.Add(RestoreFSBackupItemParams.TargetFolder, TargetFolder); providerParameters.Add(RestoreWLBackupItemParams.WLRecoveryConfig, WLRecoveryConfig); providerParameters.Add(RestoreVMBackupItemParams.RestoreDiskList, RestoreDiskList); providerParameters.Add(RestoreVMBackupItemParams.RestoreOnlyOSDisk, RestoreOnlyOSDisk); providerParameters.Add(RestoreVMBackupItemParams.RestoreAsUnmanagedDisks, RestoreAsUnmanagedDisks); providerParameters.Add(CRRParams.UseSecondaryRegion, RestoreToSecondaryRegion.IsPresent); providerParameters.Add(RestoreVMBackupItemParams.RestoreAsManagedDisk, RestoreAsManagedDisk.IsPresent); providerParameters.Add(RestoreVMBackupItemParams.UseSystemAssignedIdentity, UseSystemAssignedIdentity.IsPresent); providerParameters.Add(RestoreVMBackupItemParams.UserAssignedIdentityId, UserAssignedIdentityId); providerParameters.Add(RestoreVMBackupItemParams.RestoreType, RestoreType); providerParameters.Add(RestoreVMBackupItemParams.TargetVMName, TargetVMName); providerParameters.Add(RestoreVMBackupItemParams.TargetVNetName, TargetVNetName); providerParameters.Add(RestoreVMBackupItemParams.TargetVNetResourceGroup, TargetVNetResourceGroup); providerParameters.Add(RestoreVMBackupItemParams.TargetSubnetName, TargetSubnetName); if (DiskEncryptionSetId != null) { AzureVmRecoveryPoint rp = (AzureVmRecoveryPoint)RecoveryPoint; ServiceClientModel.BackupResourceEncryptionConfigExtendedResource vaultEncryptionSettings = ServiceClientAdapter.GetVaultEncryptionConfig(resourceGroupName, vaultName); if ((vaultEncryptionSettings.Properties.EncryptionAtRestType == "CustomerManaged") && rp.IsManagedVirtualMachine && !(rp.EncryptionEnabled) && !(RestoreToSecondaryRegion.IsPresent)) { providerParameters.Add(RestoreVMBackupItemParams.DiskEncryptionSetId, DiskEncryptionSetId); } } if (TargetZoneNumber != null) { // get storage type ServiceClientModel.BackupResourceConfigResource getStorageResponse = ServiceClientAdapter.GetVaultStorageType(resourceGroupName, vaultName); string storageType = getStorageResponse.Properties.StorageType; bool crrEnabled = (bool)getStorageResponse.Properties.CrossRegionRestoreFlag; if (storageType == AzureRmRecoveryServicesBackupStorageRedundancyType.ZoneRedundant.ToString() || (storageType == AzureRmRecoveryServicesBackupStorageRedundancyType.GeoRedundant.ToString() && crrEnabled)) { AzureVmRecoveryPoint rp = (AzureVmRecoveryPoint)RecoveryPoint; if (rp.RecoveryPointTier == RecoveryPointTier.VaultStandard) // RP recovery type should be vault only { if (rp.Zones != null) { //target region should support Zones /*if (RestoreToSecondaryRegion.IsPresent) * { * FeatureSupportRequest iaasvmFeatureRequest = new FeatureSupportRequest(); * ServiceClientAdapter.BmsAdapter.Client.FeatureSupport.ValidateWithHttpMessagesAsync(secondaryRegion, iaasvmFeatureRequest); * }*/ providerParameters.Add(RecoveryPointParams.TargetZone, TargetZoneNumber); } else { throw new ArgumentException(string.Format(Resources.RecoveryPointZonePinnedException)); } } else { throw new ArgumentException(string.Format(Resources.RecoveryPointVaultRecoveryTypeException)); } } else { throw new ArgumentException(string.Format(Resources.ZonalRestoreVaultStorageRedundancyException)); } } if (StorageAccountName != null) { providerParameters.Add(RestoreBackupItemParams.StorageAccountName, StorageAccountName); } if (StorageAccountResourceGroupName != null) { providerParameters.Add(RestoreBackupItemParams.StorageAccountResourceGroupName, StorageAccountResourceGroupName); } if (TargetResourceGroupName != null) { providerParameters.Add(RestoreVMBackupItemParams.TargetResourceGroupName, TargetResourceGroupName); } if (SourceFileType != null) { providerParameters.Add(RestoreFSBackupItemParams.SourceFileType, SourceFileType.ToString()); } if (MultipleSourceFilePath != null) { providerParameters.Add(RestoreFSBackupItemParams.MultipleSourceFilePath, MultipleSourceFilePath); } PsBackupProviderManager providerManager = new PsBackupProviderManager(providerParameters, ServiceClientAdapter); IPsBackupProvider psBackupProvider = null; if (string.Compare(ParameterSetName, AzureWorkloadParameterSet) != 0) { psBackupProvider = providerManager.GetProviderInstance( RecoveryPoint.WorkloadType, RecoveryPoint.BackupManagementType); } else { psBackupProvider = providerManager.GetProviderInstance( WorkloadType.MSSQL, BackupManagementType.AzureWorkload); } var jobResponse = psBackupProvider.TriggerRestore(); if (RestoreToSecondaryRegion.IsPresent) { var operationId = jobResponse.Request.RequestUri.Segments.Last(); var response = ServiceClientAdapter.GetCrrOperationStatus(secondaryRegion, operationId); string jobIdJson = JsonConvert.SerializeObject(response.Body.Properties); string[] jobSplits = jobIdJson.Split(new char[] { '\"' }); string jobId = jobSplits[jobSplits.Length - 2]; WriteObject(GetCrrJobObject(secondaryRegion, VaultId, jobId)); } else { HandleCreatedJob( jobResponse, Resources.RestoreOperation, vaultName: vaultName, resourceGroupName: resourceGroupName); } }, ShouldProcess(RecoveryPoint != null ? RecoveryPoint.ItemName : WLRecoveryConfig.ToString(), VerbsData.Restore)); }
public static RecoveryPointBase GetPSAzureVMRecoveryPoint( ServiceClientModel.RecoveryPointResource rp, ItemBase item) { Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id); string containerName = IdUtils.GetNameFromUri(containerUri); string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri); ServiceClientModel.IaasVMRecoveryPoint recoveryPoint = rp.Properties as ServiceClientModel.IaasVMRecoveryPoint; DateTime recoveryPointTime = DateTime.MinValue; if (recoveryPoint.RecoveryPointTime.HasValue) { recoveryPointTime = (DateTime)recoveryPoint.RecoveryPointTime; } else { throw new ArgumentNullException("RecoveryPointTime is null"); } bool isInstantILRSessionActive = recoveryPoint.IsInstantIlrSessionActive.HasValue ? (bool)recoveryPoint.IsInstantIlrSessionActive : false; AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerName, ContainerType = item.ContainerType, RecoveryPointTime = recoveryPointTime, RecoveryPointType = recoveryPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recoveryPoint.RecoveryPointAdditionalInfo, SourceVMStorageType = recoveryPoint.SourceVMStorageType, SourceResourceId = item.SourceResourceId, EncryptionEnabled = recoveryPoint.IsSourceVMEncrypted.HasValue ? recoveryPoint.IsSourceVMEncrypted.Value : false, IlrSessionActive = isInstantILRSessionActive, IsManagedVirtualMachine = recoveryPoint.IsManagedVirtualMachine.HasValue ? recoveryPoint.IsManagedVirtualMachine.Value : false, OriginalSAEnabled = recoveryPoint.OriginalStorageAccountOption.HasValue ? recoveryPoint.OriginalStorageAccountOption.Value : false, Zones = recoveryPoint.Zones, RehydrationExpiryTime = (DateTime?)null, }; if (recoveryPoint.RecoveryPointTierDetails != null) { bool isHardenedRP = false; bool isInstantRecoverable = false; bool isArchived = false; bool isRehydrated = false; foreach (ServiceClientModel.RecoveryPointTierInformation tierInfo in recoveryPoint.RecoveryPointTierDetails) { if (tierInfo.Status == ServiceClientModel.RecoveryPointTierStatus.Rehydrated) { if (tierInfo.Type == ServiceClientModel.RecoveryPointTierType.ArchivedRP) { isRehydrated = true; rpBase.RehydrationExpiryTime = (tierInfo.ExtendedInfo.ContainsKey("RehydratedRPExpiryTime")) ? DateTime.Parse(tierInfo.ExtendedInfo["RehydratedRPExpiryTime"]) : (DateTime?)null; } } if (tierInfo.Status == ServiceClientModel.RecoveryPointTierStatus.Valid) { if (tierInfo.Type == ServiceClientModel.RecoveryPointTierType.InstantRP) { isInstantRecoverable = true; } if (tierInfo.Type == ServiceClientModel.RecoveryPointTierType.HardenedRP) { isHardenedRP = true; } if (tierInfo.Type == ServiceClientModel.RecoveryPointTierType.ArchivedRP) { isArchived = true; } } } if ((isHardenedRP && isArchived) || (isRehydrated)) { rpBase.RecoveryPointTier = RecoveryPointTier.VaultStandardRehydrated; } else if (isInstantRecoverable && isHardenedRP) { rpBase.RecoveryPointTier = RecoveryPointTier.SnapshotAndVaultStandard; } else if (isInstantRecoverable && isArchived) { rpBase.RecoveryPointTier = RecoveryPointTier.SnapshotAndVaultArchive; } else if (isArchived) { rpBase.RecoveryPointTier = RecoveryPointTier.VaultArchive; } else if (isInstantRecoverable) { rpBase.RecoveryPointTier = RecoveryPointTier.Snapshot; } else if (isHardenedRP) { rpBase.RecoveryPointTier = RecoveryPointTier.VaultStandard; } } if (recoveryPoint.RecoveryPointMoveReadinessInfo != null) { rpBase.RecoveryPointMoveReadinessInfo = new Dictionary <string, RecoveryPointMoveReadinessInfo>(); foreach (var moveInfo in recoveryPoint.RecoveryPointMoveReadinessInfo) { RecoveryPointMoveReadinessInfo AzureVmMoveInfo = new RecoveryPointMoveReadinessInfo(); AzureVmMoveInfo.IsReadyForMove = moveInfo.Value.IsReadyForMove; AzureVmMoveInfo.AdditionalInfo = moveInfo.Value.AdditionalInfo; rpBase.RecoveryPointMoveReadinessInfo.Add(moveInfo.Key, AzureVmMoveInfo); } } if (rpBase.EncryptionEnabled && recoveryPoint.KeyAndSecret != null) { rpBase.KeyAndSecretDetails = new KeyAndSecretDetails() { SecretUrl = recoveryPoint.KeyAndSecret.BekDetails.SecretUrl, KeyUrl = recoveryPoint.KeyAndSecret.KekDetails.KeyUrl, SecretData = recoveryPoint.KeyAndSecret.BekDetails.SecretData, KeyBackupData = recoveryPoint.KeyAndSecret.KekDetails.KeyBackupData, KeyVaultId = recoveryPoint.KeyAndSecret.KekDetails.KeyVaultId, SecretVaultId = recoveryPoint.KeyAndSecret.BekDetails.SecretVaultId, }; } return(rpBase); }
/// <summary> /// Helper function to convert ps recovery points list model from service response. /// </summary> public static List <RecoveryPointBase> GetPSAzureRecoveryPoints( ServiceClientModel.RecoveryPointListResponse rpList, ItemBase item) { if (rpList == null || rpList.RecoveryPointList == null || rpList.RecoveryPointList.RecoveryPoints == null) { throw new ArgumentNullException("RPList"); } Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id); string containerName = IdUtils.GetNameFromUri(containerUri); string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri); List <RecoveryPointBase> result = new List <RecoveryPointBase>(); foreach (ServiceClientModel.RecoveryPointResource rp in rpList.RecoveryPointList.RecoveryPoints) { if (rp.Properties.GetType() == typeof(ServiceClientModel.RecoveryPoint)) { ServiceClientModel.RecoveryPoint recPoint = rp.Properties as ServiceClientModel.RecoveryPoint; DateTime recPointTime = DateTime.ParseExact( recPoint.RecoveryPointTime, @"MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerName, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, SourceVMStorageType = recPoint.SourceVMStorageType, SourceResourceId = item.SourceResourceId, EncryptionEnabled = recPoint.IsSourceVMEncrypted.HasValue ? recPoint.IsSourceVMEncrypted.Value : false, IlrSessionActive = recPoint.IsInstantILRSessionActive, }; result.Add(rpBase); } if (rp.Properties.GetType() == typeof(ServiceClientModel.GenericRecoveryPoint)) { ServiceClientModel.GenericRecoveryPoint recPoint = rp.Properties as ServiceClientModel.GenericRecoveryPoint; DateTime recPointTime = DateTime.ParseExact( recPoint.RecoveryPointTime, @"MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); AzureSqlRecoveryPoint rpBase = new AzureSqlRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerUri, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, FriendlyName = recPoint.FriendlyName, }; result.Add(rpBase); } } return(result); }
// <summary> /// Helper function to convert ps recovery point model from service response. /// </summary> public static RecoveryPointBase GetPSAzureRecoveryPoints( ServiceClientModel.RecoveryPointResponse rpResponse, ItemBase item) { if (rpResponse == null || rpResponse.RecPoint == null) { throw new ArgumentNullException(Resources.GetRPResponseIsNull); } RecoveryPointBase result = null; Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string containerName = IdUtils.GetNameFromUri(containerUri); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id); string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri); if (rpResponse.RecPoint.Properties.GetType() == typeof(ServiceClientModel.RecoveryPoint)) { ServiceClientModel.RecoveryPoint recPoint = rpResponse.RecPoint.Properties as ServiceClientModel.RecoveryPoint; DateTime recPointTime = DateTime.ParseExact( recPoint.RecoveryPointTime, @"MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); AzureVmRecoveryPoint vmResult = new AzureVmRecoveryPoint() { RecoveryPointId = rpResponse.RecPoint.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerName, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rpResponse.RecPoint.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, EncryptionEnabled = recPoint.IsSourceVMEncrypted.HasValue ? recPoint.IsSourceVMEncrypted.Value : false, IlrSessionActive = recPoint.IsInstantILRSessionActive, SourceResourceId = item.SourceResourceId, SourceVMStorageType = recPoint.SourceVMStorageType, }; if (vmResult.EncryptionEnabled && recPoint.KeyAndSecret != null) { vmResult.KeyAndSecretDetails = new KeyAndSecretDetails() { SecretUrl = recPoint.KeyAndSecret.BekDetails.SecretUrl, KeyUrl = recPoint.KeyAndSecret.KekDetails.KeyUrl, SecretData = recPoint.KeyAndSecret.BekDetails.SecretData, KeyBackupData = recPoint.KeyAndSecret.KekDetails.KeyBackupData, KeyVaultId = recPoint.KeyAndSecret.KekDetails.KeyVaultId, SecretVaultId = recPoint.KeyAndSecret.BekDetails.SecretVaultId, }; } result = vmResult; } if (rpResponse.RecPoint.Properties.GetType() == typeof(ServiceClientModel.GenericRecoveryPoint)) { ServiceClientModel.GenericRecoveryPoint recPoint = rpResponse.RecPoint.Properties as ServiceClientModel.GenericRecoveryPoint; DateTime recPointTime = DateTime.ParseExact( recPoint.RecoveryPointTime, @"MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture); AzureSqlRecoveryPoint sqlResult = new AzureSqlRecoveryPoint() { RecoveryPointId = rpResponse.RecPoint.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerName, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rpResponse.RecPoint.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, SourceResourceId = item.SourceResourceId, FriendlyName = recPoint.FriendlyName }; result = sqlResult; } return(result); }
/// <summary> /// Helper function to convert ps recovery points list model from service response. /// </summary> public static List <RecoveryPointBase> GetPSAzureRecoveryPoints( List <ServiceClientModel.RecoveryPointResource> rpList, ItemBase item) { if (rpList == null) { throw new ArgumentNullException("RPList"); } Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id); string containerName = IdUtils.GetNameFromUri(containerUri); string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri); List <RecoveryPointBase> result = new List <RecoveryPointBase>(); foreach (ServiceClientModel.RecoveryPointResource rp in rpList) { if (rp.Properties.GetType() == typeof(ServiceClientModel.IaasVMRecoveryPoint)) { ServiceClientModel.IaasVMRecoveryPoint recPoint = rp.Properties as ServiceClientModel.IaasVMRecoveryPoint; DateTime recPointTime = DateTime.MinValue; if (recPoint.RecoveryPointTime.HasValue) { recPointTime = (DateTime)recPoint.RecoveryPointTime; } else { throw new ArgumentNullException("RecoveryPointTime is null"); } bool isInstantILRSessionActive = recPoint.IsInstantIlrSessionActive.HasValue ? (bool)recPoint.IsInstantIlrSessionActive : false; AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerName, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, SourceVMStorageType = recPoint.SourceVMStorageType, SourceResourceId = item.SourceResourceId, EncryptionEnabled = recPoint.IsSourceVMEncrypted.HasValue ? recPoint.IsSourceVMEncrypted.Value : false, IlrSessionActive = isInstantILRSessionActive, IsManagedVirtualMachine = recPoint.IsManagedVirtualMachine.HasValue ? recPoint.IsManagedVirtualMachine.Value : false, OriginalSAEnabled = recPoint.OriginalStorageAccountOption.HasValue ? recPoint.OriginalStorageAccountOption.Value : false, }; result.Add(rpBase); } if (rp.Properties.GetType() == typeof(ServiceClientModel.GenericRecoveryPoint)) { ServiceClientModel.GenericRecoveryPoint recPoint = rp.Properties as ServiceClientModel.GenericRecoveryPoint; DateTime recPointTime = DateTime.MinValue; if (recPoint.RecoveryPointTime.HasValue) { recPointTime = (DateTime)recPoint.RecoveryPointTime; } else { throw new ArgumentNullException("RecoveryPointTime is null"); } AzureSqlRecoveryPoint rpBase = new AzureSqlRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerUri, ContainerType = item.ContainerType, RecoveryPointTime = recPointTime, RecoveryPointType = recPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recPoint.RecoveryPointAdditionalInfo, FriendlyName = recPoint.FriendlyName, }; result.Add(rpBase); } } return(result); }
public static RecoveryPointBase GetPSAzureVMRecoveryPoint( ServiceClientModel.RecoveryPointResource rp, ItemBase item) { Dictionary <UriEnums, string> uriDict = HelperUtils.ParseUri(item.Id); string containerUri = HelperUtils.GetContainerUri(uriDict, item.Id); string protectedItemUri = HelperUtils.GetProtectedItemUri(uriDict, item.Id); string containerName = IdUtils.GetNameFromUri(containerUri); string protectedItemName = IdUtils.GetNameFromUri(protectedItemUri); ServiceClientModel.IaasVMRecoveryPoint recoveryPoint = rp.Properties as ServiceClientModel.IaasVMRecoveryPoint; DateTime recoveryPointTime = DateTime.MinValue; if (recoveryPoint.RecoveryPointTime.HasValue) { recoveryPointTime = (DateTime)recoveryPoint.RecoveryPointTime; } else { throw new ArgumentNullException("RecoveryPointTime is null"); } bool isInstantILRSessionActive = recoveryPoint.IsInstantIlrSessionActive.HasValue ? (bool)recoveryPoint.IsInstantIlrSessionActive : false; AzureVmRecoveryPoint rpBase = new AzureVmRecoveryPoint() { RecoveryPointId = rp.Name, BackupManagementType = item.BackupManagementType, ItemName = protectedItemName, ContainerName = containerName, ContainerType = item.ContainerType, RecoveryPointTime = recoveryPointTime, RecoveryPointType = recoveryPoint.RecoveryPointType, Id = rp.Id, WorkloadType = item.WorkloadType, RecoveryPointAdditionalInfo = recoveryPoint.RecoveryPointAdditionalInfo, SourceVMStorageType = recoveryPoint.SourceVMStorageType, SourceResourceId = item.SourceResourceId, EncryptionEnabled = recoveryPoint.IsSourceVMEncrypted.HasValue ? recoveryPoint.IsSourceVMEncrypted.Value : false, IlrSessionActive = isInstantILRSessionActive, IsManagedVirtualMachine = recoveryPoint.IsManagedVirtualMachine.HasValue ? recoveryPoint.IsManagedVirtualMachine.Value : false, OriginalSAEnabled = recoveryPoint.OriginalStorageAccountOption.HasValue ? recoveryPoint.OriginalStorageAccountOption.Value : false, }; if (rpBase.EncryptionEnabled && recoveryPoint.KeyAndSecret != null) { rpBase.KeyAndSecretDetails = new KeyAndSecretDetails() { SecretUrl = recoveryPoint.KeyAndSecret.BekDetails.SecretUrl, KeyUrl = recoveryPoint.KeyAndSecret.KekDetails.KeyUrl, SecretData = recoveryPoint.KeyAndSecret.BekDetails.SecretData, KeyBackupData = recoveryPoint.KeyAndSecret.KekDetails.KeyBackupData, KeyVaultId = recoveryPoint.KeyAndSecret.KekDetails.KeyVaultId, SecretVaultId = recoveryPoint.KeyAndSecret.BekDetails.SecretVaultId, }; } return(rpBase); }
public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; Dictionary <Enum, object> providerParameters = new Dictionary <Enum, object>(); string secondaryRegion = ""; if (RestoreToSecondaryRegion.IsPresent) { ARSVault vault = ServiceClientAdapter.GetVault(resourceGroupName, vaultName); secondaryRegion = BackupUtils.regionMap[vault.Location]; providerParameters.Add(CRRParams.SecondaryRegion, secondaryRegion); } providerParameters.Add(VaultParams.VaultName, vaultName); providerParameters.Add(VaultParams.ResourceGroupName, resourceGroupName); providerParameters.Add(VaultParams.VaultLocation, VaultLocation); providerParameters.Add(RestoreBackupItemParams.RecoveryPoint, RecoveryPoint); providerParameters.Add(RestoreVMBackupItemParams.OsaOption, UseOriginalStorageAccount.IsPresent); providerParameters.Add(RestoreFSBackupItemParams.ResolveConflict, ResolveConflict.ToString()); providerParameters.Add(RestoreFSBackupItemParams.SourceFilePath, SourceFilePath); providerParameters.Add(RestoreFSBackupItemParams.TargetStorageAccountName, TargetStorageAccountName); providerParameters.Add(RestoreFSBackupItemParams.TargetFileShareName, TargetFileShareName); providerParameters.Add(RestoreFSBackupItemParams.TargetFolder, TargetFolder); providerParameters.Add(RestoreWLBackupItemParams.WLRecoveryConfig, WLRecoveryConfig); providerParameters.Add(RestoreVMBackupItemParams.RestoreDiskList, RestoreDiskList); providerParameters.Add(RestoreVMBackupItemParams.RestoreOnlyOSDisk, RestoreOnlyOSDisk); providerParameters.Add(RestoreVMBackupItemParams.RestoreAsUnmanagedDisks, RestoreAsUnmanagedDisks); providerParameters.Add(CRRParams.UseSecondaryRegion, RestoreToSecondaryRegion.IsPresent); if (DiskEncryptionSetId != null) { AzureVmRecoveryPoint rp = (AzureVmRecoveryPoint)RecoveryPoint; BackupResourceEncryptionConfigResource vaultEncryptionSettings = ServiceClientAdapter.GetVaultEncryptionConfig(resourceGroupName, vaultName); if ((vaultEncryptionSettings.Properties.EncryptionAtRestType == "CustomerManaged") && rp.IsManagedVirtualMachine && !(rp.EncryptionEnabled) && !(RestoreToSecondaryRegion.IsPresent)) { providerParameters.Add(RestoreVMBackupItemParams.DiskEncryptionSetId, DiskEncryptionSetId); } } if (StorageAccountName != null) { providerParameters.Add(RestoreBackupItemParams.StorageAccountName, StorageAccountName); } if (StorageAccountResourceGroupName != null) { providerParameters.Add(RestoreBackupItemParams.StorageAccountResourceGroupName, StorageAccountResourceGroupName); } if (TargetResourceGroupName != null) { providerParameters.Add(RestoreVMBackupItemParams.TargetResourceGroupName, TargetResourceGroupName); } if (SourceFileType != null) { providerParameters.Add(RestoreFSBackupItemParams.SourceFileType, SourceFileType.ToString()); } if (MultipleSourceFilePath != null) { providerParameters.Add(RestoreFSBackupItemParams.MultipleSourceFilePath, MultipleSourceFilePath); } PsBackupProviderManager providerManager = new PsBackupProviderManager(providerParameters, ServiceClientAdapter); IPsBackupProvider psBackupProvider = null; if (string.Compare(ParameterSetName, AzureWorkloadParameterSet) != 0) { psBackupProvider = providerManager.GetProviderInstance( RecoveryPoint.WorkloadType, RecoveryPoint.BackupManagementType); } else { psBackupProvider = providerManager.GetProviderInstance( WorkloadType.MSSQL, BackupManagementType.AzureWorkload); } var jobResponse = psBackupProvider.TriggerRestore(); if (RestoreToSecondaryRegion.IsPresent) { var operationId = jobResponse.Request.RequestUri.Segments.Last(); var response = ServiceClientAdapter.GetCrrOperationStatus(secondaryRegion, operationId); string jobIDJson = JsonConvert.SerializeObject(response.Body.Properties); string[] jobSplits = jobIDJson.Split(new char[] { '\"' }); string jobID = jobSplits[jobSplits.Length - 2]; WriteObject(GetCrrJobObject(secondaryRegion, VaultId, jobID)); } else { HandleCreatedJob( jobResponse, Resources.RestoreOperation, vaultName: vaultName, resourceGroupName: resourceGroupName); } }, ShouldProcess(RecoveryPoint != null ? RecoveryPoint.ItemName : WLRecoveryConfig.ToString(), VerbsData.Restore)); }
public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; Dictionary <Enum, object> providerParameters = new Dictionary <Enum, object>(); providerParameters.Add(VaultParams.VaultName, vaultName); providerParameters.Add(VaultParams.ResourceGroupName, resourceGroupName); providerParameters.Add(VaultParams.VaultLocation, VaultLocation); providerParameters.Add(RestoreBackupItemParams.RecoveryPoint, RecoveryPoint); providerParameters.Add(RestoreVMBackupItemParams.OsaOption, UseOriginalStorageAccount.IsPresent); providerParameters.Add(RestoreFSBackupItemParams.ResolveConflict, ResolveConflict.ToString()); providerParameters.Add(RestoreFSBackupItemParams.SourceFilePath, SourceFilePath); providerParameters.Add(RestoreFSBackupItemParams.TargetStorageAccountName, TargetStorageAccountName); providerParameters.Add(RestoreFSBackupItemParams.TargetFileShareName, TargetFileShareName); providerParameters.Add(RestoreFSBackupItemParams.TargetFolder, TargetFolder); providerParameters.Add(RestoreWLBackupItemParams.WLRecoveryConfig, WLRecoveryConfig); providerParameters.Add(RestoreVMBackupItemParams.RestoreDiskList, RestoreDiskList); providerParameters.Add(RestoreVMBackupItemParams.RestoreOnlyOSDisk, RestoreOnlyOSDisk); providerParameters.Add(RestoreVMBackupItemParams.RestoreAsUnmanagedDisks, RestoreAsUnmanagedDisks); if (DiskEncryptionSetId != null) { AzureVmRecoveryPoint rp = (AzureVmRecoveryPoint)RecoveryPoint; BackupResourceEncryptionConfigResource vaultEncryptionSettings = ServiceClientAdapter.GetVaultEncryptionConfig(resourceGroupName, vaultName); // do not allow for CRR - to be added if ((vaultEncryptionSettings.Properties.EncryptionAtRestType == "CustomerManaged") && rp.IsManagedVirtualMachine && !(rp.EncryptionEnabled)) { providerParameters.Add(RestoreVMBackupItemParams.DiskEncryptionSetId, DiskEncryptionSetId); } } if (StorageAccountName != null) { providerParameters.Add(RestoreBackupItemParams.StorageAccountName, StorageAccountName); } if (StorageAccountResourceGroupName != null) { providerParameters.Add(RestoreBackupItemParams.StorageAccountResourceGroupName, StorageAccountResourceGroupName); } if (TargetResourceGroupName != null) { providerParameters.Add(RestoreVMBackupItemParams.TargetResourceGroupName, TargetResourceGroupName); } if (SourceFileType != null) { providerParameters.Add(RestoreFSBackupItemParams.SourceFileType, SourceFileType.ToString()); } if (MultipleSourceFilePath != null) { providerParameters.Add(RestoreFSBackupItemParams.MultipleSourceFilePath, MultipleSourceFilePath); } PsBackupProviderManager providerManager = new PsBackupProviderManager(providerParameters, ServiceClientAdapter); IPsBackupProvider psBackupProvider = null; if (string.Compare(ParameterSetName, AzureWorkloadParameterSet) != 0) { psBackupProvider = providerManager.GetProviderInstance( RecoveryPoint.WorkloadType, RecoveryPoint.BackupManagementType); } else { psBackupProvider = providerManager.GetProviderInstance( WorkloadType.MSSQL, BackupManagementType.AzureWorkload); } var jobResponse = psBackupProvider.TriggerRestore(); WriteDebug(string.Format("Restore submitted")); HandleCreatedJob( jobResponse, Resources.RestoreOperation, vaultName: vaultName, resourceGroupName: resourceGroupName); }, ShouldProcess(RecoveryPoint != null ? RecoveryPoint.ItemName : WLRecoveryConfig.ToString(), VerbsData.Restore)); }