diff --git a/src/RecoveryServices/RecoveryServices.Backup.Providers/Providers/AzureFilesPsBackupProvider.cs b/src/RecoveryServices/RecoveryServices.Backup.Providers/Providers/AzureFilesPsBackupProvider.cs index 0a274d9845c8..2c2fd0009fa0 100644 --- a/src/RecoveryServices/RecoveryServices.Backup.Providers/Providers/AzureFilesPsBackupProvider.cs +++ b/src/RecoveryServices/RecoveryServices.Backup.Providers/Providers/AzureFilesPsBackupProvider.cs @@ -241,7 +241,71 @@ public RestAzureNS.AzureOperationResponse TriggerRestore() targetFolder = "/"; } - GenericResource storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(recoveryPoint.ContainerName.Split(';')[2]); + GenericResource storageAccountResource = null; + string sourceResourceId = null; + string storageAccountLocation = vaultLocation; // Default to vault location + + try + { + storageAccountResource = ServiceClientAdapter.GetStorageAccountResource(recoveryPoint.ContainerName.Split(';')[2]); + if (storageAccountResource != null) + { + sourceResourceId = storageAccountResource.Id; + storageAccountLocation = storageAccountResource.Location; + Logger.Instance.WriteDebug("Successfully retrieved storage account resource"); + } + } + catch (Exception ex) + { + Logger.Instance.WriteDebug($"Failed to get storage account resource: {ex.Message}. Will attempt to retrieve from protected item."); + storageAccountResource = null; + } + + // If storage account retrieval failed, fall back to getting it from protected item + if (storageAccountResource == null || string.IsNullOrEmpty(sourceResourceId)) + { + try + { + Dictionary keyValueDict = HelperUtils.ParseUri(recoveryPoint.Id); + string containerUri = HelperUtils.GetContainerUri(keyValueDict, recoveryPoint.Id); + string protectedItemUri = HelperUtils.GetProtectedItemUri(keyValueDict, recoveryPoint.Id); + + var protectedItemResponse = ServiceClientAdapter.GetProtectedItem( + containerUri, + protectedItemUri, + null, + vaultName: vaultName, + resourceGroupName: resourceGroupName); + + if (protectedItemResponse?.Body?.Properties is AzureFileshareProtectedItem afsProtectedItem) + { + sourceResourceId = afsProtectedItem.SourceResourceId; + Logger.Instance.WriteDebug("Retrieved source resource ID from protected item"); + + // Create a minimal resource object with the source resource ID + if (!string.IsNullOrEmpty(sourceResourceId)) + { + storageAccountResource = new GenericResource + { + Id = sourceResourceId, + Location = storageAccountLocation + }; + } + } + } + catch (Exception ex) + { + Logger.Instance.WriteDebug($"Failed to get protected item: {ex.Message}"); + } + } + + // Validate that we have a source resource ID + if (storageAccountResource == null || string.IsNullOrEmpty(storageAccountResource.Id)) + { + throw new ArgumentException(string.Format(Resources.AzureFileShareNotFound, + recoveryPoint.ContainerName)); + } + GenericResource targetStorageAccountResource = null; string targetStorageAccountLocation = null; if (targetStorageAccountName != null) diff --git a/src/RecoveryServices/RecoveryServices/ChangeLog.md b/src/RecoveryServices/RecoveryServices/ChangeLog.md index e56c6f238444..7a8036d7ae27 100644 --- a/src/RecoveryServices/RecoveryServices/ChangeLog.md +++ b/src/RecoveryServices/RecoveryServices/ChangeLog.md @@ -19,6 +19,7 @@ --> ## Upcoming Release * Added new optional parameter CVMOsDiskEncryptionSetId in Restore-AzRecoveryServicesBackupItem. +* Fixed bug in Restore-AzRecoveryServicesBackupItem cmdlet for Azure File Share restore. ## Version 7.7.2 * Added fix for resume protection with AzureFileshare.