Hi Johannes Maly,
You can restore an encrypted database to SQL Server only if it was encrypted with a customer-managed key
and the destination server has access to the same key that's used to encrypt the database. If you are using Service-managed key
you must disable TDE encryption on the source database before taking the backup as Service-managed key
is managed by Microsoft, you cannot restore the database using this key.
To restore databases that are encrypted at rest by using Transparent Data Encryption (TDE), the destination instance of SQL Server must have access to the same key that's used to protect the source database through the SQL Server Connector for Azure Key Vault
Steps are as below:
- Set Up a Microsoft Entra Service Principal, generate a client secret for this application. Note the Application (client) ID and the client secret's value; these will be needed later.
- Create an Azure Key Vault and Ensure that the above created Microsoft Entra service principal has at least the following permissions for the Key Vault:
get
,list
,wrapKey
, andunwrapKey
. - Download and install the SQL Server Connector, which allows SQL Server to communicate with Azure Key Vault.
- Configure SQL Server to use EKM by running the following Transact-SQL script in master database:
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
-- Enable EKM provider
EXEC sp_configure 'EKM provider enabled', 1;
GO
RECONFIGURE;
- Create a cryptographic provider by using the SQL Server Connector, which is an EKM provider for the Azure Key Vault. In this example, the provider name is
AzureKeyVault_EKM
.
CREATE CRYPTOGRAPHIC PROVIDER AzureKeyVault_EKM
FROM FILE = 'C:\Program Files\SQL Server Connector for Microsoft Azure Key Vault\Microsoft.AzureKeyVaultService.EKM.dll';
GO
- Set up a SQL Server credential for a SQL Server login to use the key vault.
CREATE CREDENTIAL sysadmin_ekm_cred
WITH IDENTITY = 'DocsSampleEKMKeyVault', -- for public Azure
-- WITH IDENTITY = 'DocsSampleEKMKeyVault.vault.usgovcloudapi.net', -- for Azure Government
-- WITH IDENTITY = 'DocsSampleEKMKeyVault.vault.azure.cn', -- for Microsoft Azure operated by 21Vianet
-- WITH IDENTITY = 'DocsSampleEKMKeyVault.vault.microsoftazure.de', -- for Azure Germany
-- WITH IDENTITY = '<name of Managed HSM>.managedhsm.azure.net', -- for Managed HSM (HSM URI in the Azure portal resource)
--<----Application (Client) ID ---><--Microsoft Entra app (Client) ID secret-->
SECRET = 'd956f6b9xxxxxxxyrA8X~PldtMCvUZPxxxxxxxx'
FOR CRYPTOGRAPHIC PROVIDER AzureKeyVault_EKM;
-- Add the credential to the SQL Server administrator's domain login
ALTER LOGIN [<domain>\<login>]
ADD CREDENTIAL sysadmin_ekm_cred;
- Open your Azure Key Vault key in your SQL Server instance.
CREATE ASYMMETRIC KEY EKMSampleASYKey
FROM PROVIDER [AzureKeyVault_EKM]
WITH PROVIDER_KEY_NAME = 'ContosoRSAKey0',
CREATION_DISPOSITION = OPEN_EXISTING;
- Create a new login by using the asymmetric key in SQL Server that you created in the preceding step.
--Create a Login that will associate the asymmetric key to this login
CREATE LOGIN TDE_Login
FROM ASYMMETRIC KEY EKMSampleASYKey;
- Create a new login from the asymmetric key in SQL Server. Drop the credential mapping so that the credentials can be mapped to the new login.
--Now drop the credential mapping from the original association
ALTER LOGIN [<domain>\<login>]
DROP CREDENTIAL sysadmin_ekm_cred;
- Alter the new login, and map the EKM credentials to the new login.
--Now add the credential mapping to the new Login
ALTER LOGIN TDE_Login
ADD CREDENTIAL sysadmin_ekm_cred;
- To restore your database to SQL Server, run the following sample T-SQL command with file paths appropriate to your environment
RESTORE DATABASE [SampleDB]
FROM URL = 'https://<mystorageaccountname>.blob.core.windows.net/<containername>/SampleDB.bak'
WITH
MOVE 'data_0' TO 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\SampleDB_data_0.mdf',
MOVE 'log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\SampleDBlog.ldf',
MOVE 'XTP' TO 'C:\Program Files\Microsoft SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\SampleDB_xtp.xtp'
Reference:
https://learn.microsoft.com/en-us/sql/relational-databases/security/encryption/setup-steps-for-extensible-key-management-using-the-azure-key-vault?view=sql-server-ver16&tabs=portal - till step 5 configure master database.
https://learn.microsoft.com/en-us/azure/azure-sql/managed-instance/restore-database-to-sql-server?view=azuresql&tabs=managed-identity - from restore to SQL server
You can also refer this video for more understanding - https://www.youtube.com/watch?v=B8U_UZvQrfs
If this answers your query, do click Accept Answer
and Yes for was this answer helpful. And, if you have any further query do let us know.