From c0c13ef0230bd5621896e33e7f41c864120dec62 Mon Sep 17 00:00:00 2001 From: ianna1-admin Date: Wed, 30 Jul 2025 01:09:24 -0500 Subject: [PATCH 1/2] fix afs restore ps command in case source storage account is deleted --- .../Providers/AzureFilesPsBackupProvider.cs | 66 ++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) 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) From e78c2f712e215e26b9f5547e20ea39495214b943 Mon Sep 17 00:00:00 2001 From: ianna1-admin Date: Wed, 30 Jul 2025 01:14:12 -0500 Subject: [PATCH 2/2] update change log --- src/RecoveryServices/RecoveryServices/ChangeLog.md | 1 + 1 file changed, 1 insertion(+) 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.