/// <summary> /// Set Azure Recovery Services Vault context. /// </summary> private void SetARSVaultContext( ARSVault arsVault) { try { using (var powerShell = System.Management.Automation.PowerShell.Create()) { var result = powerShell .AddCommand("Get-AzureRmRecoveryServicesVaultSettingsFile") .AddParameter( "Vault", arsVault) .Invoke(); var vaultSettingspath = (string)result[0] .Members["FilePath"] .Value; powerShell.Commands.Clear(); result = powerShell .AddCommand("Import-AzureRmRecoveryServicesAsrVaultSettingsFile") .AddParameter( "Path", vaultSettingspath) .Invoke(); this.WriteObject(result); powerShell.Commands.Clear(); } } catch (InvalidOperationException e) { this.WriteDebug(e.Message); } }
/// <summary> /// Method to get Recovery Services Vault. /// </summary> /// <param name="resouceGroupName">Name of the resouce group</param> /// <param name="vaultName">Name of the vault</param> /// <returns>Azure Recovery Services Vault</returns> public ARSVault GetVault(string resouceGroupName, string vaultName) { Vault response = RSAdapter.Client.Vaults.GetWithHttpMessagesAsync(resouceGroupName, vaultName, cancellationToken: RSAdapter.CmdletCancellationToken).Result.Body; ARSVault vault = new ARSVault(response); return(vault); }
/// <summary> /// Set Azure Recovery Services Vault context. /// </summary> private void SetARSVaultContext( ARSVault arsVault) { try { VaultExtendedInfoResource vaultExtendedInfo = null; try { vaultExtendedInfo = this.RecoveryServicesClient .GetVaultExtendedInfo(this.Vault.ResourceGroupName, this.Vault.Name); } catch (Exception ex) { // code interanally handled the cloud exception thrown earlier.But still there are changes of other exception. // suggesting alternate way to user to unblock if this command is failing. Logger.Instance.WriteWarning(ex.Message); throw new Exception(Resources.TryDownloadingVaultFile); } ASRVaultCreds asrVaultCreds = new ASRVaultCreds(); asrVaultCreds.ResourceName = this.Vault.Name; asrVaultCreds.ResourceGroupName = this.Vault.ResourceGroupName; asrVaultCreds.ChannelIntegrityKey = vaultExtendedInfo.IntegrityKey; asrVaultCreds.ResourceNamespace = ARMResourceTypeConstants .RecoveryServicesResourceProviderNameSpace; asrVaultCreds.ARMResourceType = ARMResourceTypeConstants.RecoveryServicesVault; asrVaultCreds.PrivateEndpointStateForSiteRecovery = this.Vault.Properties.PrivateEndpointStateForSiteRecovery; Utilities.UpdateCurrentVaultContext(asrVaultCreds); this.RecoveryServicesClient.ValidateVaultSettings( asrVaultCreds.ResourceName, asrVaultCreds.ResourceGroupName); this.WriteObject(new ASRVaultSettings(asrVaultCreds)); } catch (InvalidOperationException e) { this.WriteDebug(e.Message); } }
/// <summary> /// Set Azure Recovery Services Vault context. /// </summary> private void SetARSVaultContext( ARSVault arsVault) { try { VaultExtendedInfoResource vaultExtendedInfo = null; try { vaultExtendedInfo = this.RecoveryServicesClient .GetVaultExtendedInfo(this.Vault.ResourceGroupName, this.Vault.Name); } catch (CloudException ex) { throw new Exception(Resources.TryDownloadingVaultFile); } ASRVaultCreds asrVaultCreds = new ASRVaultCreds(); asrVaultCreds.ResourceName = this.Vault.Name; asrVaultCreds.ResourceGroupName = this.Vault.ResourceGroupName; asrVaultCreds.ChannelIntegrityKey = vaultExtendedInfo.IntegrityKey; asrVaultCreds.ResourceNamespace = ARMResourceTypeConstants .RecoveryServicesResourceProviderNameSpace; asrVaultCreds.ARMResourceType = ARMResourceTypeConstants.RecoveryServicesVault; Utilities.UpdateCurrentVaultContext(asrVaultCreds); this.RecoveryServicesClient.ValidateVaultSettings( asrVaultCreds.ResourceName, asrVaultCreds.ResourceGroupName); this.WriteObject(new ASRVaultSettings(asrVaultCreds)); } catch (InvalidOperationException e) { this.WriteDebug(e.Message); } }
public override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; if (ParameterSetName == JobFilterSet) { JobId = Job.JobId; } WriteDebug("Fetching job with ID: " + JobId); if (UseSecondaryRegion.IsPresent) { CrrModel.CrrJobRequest jobRequest = new CrrModel.CrrJobRequest(); jobRequest.JobName = JobId; jobRequest.ResourceId = VaultId; // check this GetVault for rainy day scenario ARSVault vault = ServiceClientAdapter.GetVault(resourceGroupName, vaultName); string secondaryRegion = BackupUtils.regionMap[vault.Location]; CrrModel.JobResource jobDetailsCrr = ServiceClientAdapter.GetCRRJobDetails(secondaryRegion, jobRequest); WriteObject(JobConversions.GetPSJobCrr(jobDetailsCrr)); } else { JobResource jobDetails = ServiceClientAdapter.GetJob( JobId, vaultName: vaultName, resourceGroupName: resourceGroupName); WriteObject(JobConversions.GetPSJob(jobDetails)); } }); }
/// <summary> /// Set Azure Recovery Services Vault context. /// </summary> private void SetARSVaultContext( ARSVault arsVault) { try { using (var powerShell = System.Management.Automation.PowerShell.Create()) { var vaultExtendedInfo = this.RecoveryServicesClient .GetVaultExtendedInfo(this.Vault.ResourceGroupName, this.Vault.Name); ASRVaultCreds asrVaultCreds = new ASRVaultCreds(); asrVaultCreds.ResourceName = this.Vault.Name; asrVaultCreds.ResourceGroupName = this.Vault.ResourceGroupName; asrVaultCreds.ChannelIntegrityKey = vaultExtendedInfo.IntegrityKey; asrVaultCreds.ResourceNamespace = ARMResourceTypeConstants .RecoveryServicesResourceProviderNameSpace; asrVaultCreds.ARMResourceType = ARMResourceTypeConstants.RecoveryServicesVault; Utilities.UpdateCurrentVaultContext(asrVaultCreds); this.RecoveryServicesClient.ValidateVaultSettings( asrVaultCreds.ResourceName, asrVaultCreds.ResourceGroupName); this.WriteObject(new ASRVaultSettings(asrVaultCreds)); powerShell.Commands.Clear(); } } catch (InvalidOperationException e) { this.WriteDebug(e.Message); } }
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 override void ExecuteCmdlet() { ExecutionBlock(() => { base.ExecuteCmdlet(); ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId); string vaultName = resourceIdentifier.ResourceName; string resourceGroupName = resourceIdentifier.ResourceGroupName; // initialize values to default DateTime rangeStart = DateTime.UtcNow.AddDays(-1); DateTime rangeEnd = DateTime.UtcNow; if (From.HasValue) { rangeStart = From.Value; } if (!From.HasValue && To.HasValue) { throw new Exception(Resources.JobFromNotProvided); } if (To.HasValue) { rangeEnd = To.Value; } if (rangeStart.Kind != DateTimeKind.Utc || rangeEnd.Kind != DateTimeKind.Utc) { throw new Exception(Resources.JobTimeFiltersShouldBeSpecifiedInUtc); } // validate filters if (rangeEnd <= rangeStart) { throw new Exception(Resources.JobToShouldBeGreaterThanFrom); } else if (rangeEnd.Subtract(rangeStart) > TimeSpan.FromDays(30)) { throw new Exception(Resources.JobAllowedDateTimeRangeExceeded); } else if (rangeStart > DateTime.UtcNow) { throw new Exception(Resources.JobStartTimeShouldBeLessThanCurrent); } // validate JobId and Job objects if (!string.IsNullOrEmpty(JobId)) { // if JobId and Job are provided together and they don't match then throw exception if (Job != null && JobId != Job.JobId) { throw new Exception(Resources.JobJobIdAndJobMismatch); } } else if (Job != null) { JobId = Job.JobId; } List <JobBase> result = new List <JobBase>(); WriteDebug(string.Format("Filters provided are: StartTime - {0} " + "EndTime - {1} Status - {2} Operation - {3} Type - {4} UseSecondaryRegion - {5}", From, To, Status, Operation, BackupManagementType, UseSecondaryRegion.ToString())); int resultCount = 0; if (UseSecondaryRegion.IsPresent) { ARSVault vault = ServiceClientAdapter.GetVault(resourceGroupName, vaultName); string secondaryRegion = BackupUtils.regionMap[vault.Location]; WriteDebug(" Getting CRR jobs from secondary region: " + secondaryRegion); var adapterResponse = ServiceClientAdapter.GetCrrJobs(VaultId, JobId, ServiceClientHelpers.GetServiceClientJobStatus(Status), Operation.ToString(), rangeStart, rangeEnd, ServiceClientHelpers.GetServiceClientBackupManagementType(BackupManagementType), secondaryRegion); JobConversions.AddServiceClientJobsToPSList( adapterResponse, result, ref resultCount); } else { var adapterResponse = ServiceClientAdapter.GetJobs( JobId, ServiceClientHelpers.GetServiceClientJobStatus(Status), Operation.ToString(), rangeStart, rangeEnd, ServiceClientHelpers.GetServiceClientBackupManagementType( BackupManagementType), vaultName: vaultName, resourceGroupName: resourceGroupName); JobConversions.AddServiceClientJobsToPSList( adapterResponse, result, ref resultCount); } WriteDebug("Number of jobs fetched: " + result.Count); WriteObject(result, enumerateCollection: true); }); }
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)); }