Changes for page sql-tde
Last modified by Nikhil Singh on 2026/07/03 08:32
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
-
Attachments (0 modified, 0 added, 1 removed)
-
Objects (0 modified, 0 added, 1 removed)
Details
- Page properties
-
- Content
-
... ... @@ -1,317 +1,267 @@ 1 -= **1. Steps for ImplementingTransparentDataEncryption(TDE)FROM SQL Server**=1 += TDE(1) = 2 2 3 +1 CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'YourStrongPasswordHere!'; 4 +GO 3 3 4 -This document outlines the process of **encrypting SQL Server databases** using **Transparent Data Encryption (TDE)** and backing them up to **Azure Storage**. TDE ensures data at rest is encrypted, leveraging a **Master Key (MK)**, **TDE Certificate**, and **Database Encryption Key (DEK)** for encryption. 6 +CREATE CERTIFICATE TDE_Certificate 7 +WITH SUBJECT = 'TDE Certificate'; 8 +GO 5 5 6 -The process also includes creating a **credential** for secure backup to **Azure Blob Storage**, providing a scalable and secure solution for storing encrypted databases in the cloud. 10 +2 USE master; 11 +GO 7 7 13 +SELECT 14 + cert.name AS Certificate_Name, 15 + cert.subject AS Certificate_Subject, 16 + cert.issuer_name AS Issuer_Name, 17 + cert.pvt_key_encryption_type AS Private_Key_Encryption_Type 18 +FROM 19 + sys.certificates cert 20 +WHERE 21 + cert.name LIKE 'TDE%'; 8 8 9 -[[image:image-20250305152543-1.png]] 10 10 24 +3 BACKUP CERTIFICATE TDE_Certificate 25 +TO FILE = https://zagpebslab.blob.core.windows.net/sql-backups?sp=racwl&st=2025-02-25T13:28:42Z&se=2026-02-25T21:28:42Z&spr=https&sv=2022-11-02&sr=c&sig=eQKxB%2F0bg5cX71PmhUTlGHLCnIwSbe5CLOWVVkaDR1g%3D\TDE_Certificate.cer' 26 +WITH PRIVATE KEY ( 27 + FILE = https://zagpebslab.blob.core.windows.net/sql-backups?sp=racwl&st=2025-02-25T13:28:42Z&se=2026-02-25T21:28:42Z&spr=https&sv=2022-11-02&sr=c&sig=eQKxB%2F0bg5cX71PmhUTlGHLCnIwSbe5CLOWVVkaDR1g%3D\TDE_PrivateKey.pvk', 28 + ENCRYPTION BY PASSWORD = 'AnotherStrongPasswordHere!' 29 +); 30 +GO 11 11 12 -**~1. Create a Master Key in the master Database** 13 -The first step is to create a **Master Key** in the master database. This key will be used to encrypt other cryptographic objects, such as certificates and symmetric keys, within SQL Server. 14 14 33 +USE Normal; 34 +GO 15 15 16 -**2. Create a TDE Certificate (Encrypted by the Master Key)** 17 -Next, generate a **TDE certificate** that will be used to encrypt the **Database Encryption Key (DEK)**. This certificate is encrypted by the **Master Key** created in step 1, providing an additional layer of security. 36 +-- 6. Create a Database Encryption Key (DEK) and encrypt it with the TDE certificate 37 +CREATE DATABASE ENCRYPTION KEY 38 +WITH ALGORITHM = AES_256 39 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 40 +GO 18 18 42 +USE [dba] 43 +GO 19 19 20 -**3. Choose the Database to Create the Database Encryption Key (DEK)** 21 -For the selected database, create the **Database Encryption Key (DEK)**. The DEK will be encrypted by the **TDE certificate** and will use the **AES-256 encryption algorithm** to ensure data is securely encrypted at rest. 45 +CREATE DATABASE ENCRYPTION KEY 46 +WITH ALGORITHM = AES_256 47 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 48 +GO 22 22 23 23 24 - **4.EnableEncryption for the Database**25 -O nce the **DEK** has been created, enable **TDE** for the database. This ensures that all data written to the database is automatically encrypted at rest, providing full protection for sensitive information.51 +USE [dba1] 52 +GO 26 26 54 +CREATE DATABASE ENCRYPTION KEY 55 +WITH ALGORITHM = AES_256 56 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 57 +GO 27 27 28 -**5. Create a Credential with a SAS Token** 29 -Finally, create a **credential** that allows SQL Server to access Azure Blob Storage. This credential is created using a **Shared Access Signature (SAS) token**, which ensures secure and authenticated access to the storage account for backup purposes. 30 30 31 -{{code language="sql"}} 32 -drop credential [https://zagpebslab.blob.core.windows.net/sql-backups] 33 -CREATE CREDENTIAL [https://zagpebslab.blob.core.windows.net/sql-backups] 34 -WITH IDENTITY='SHARED ACCESS SIGNATURE' 35 -, SECRET = 'sp=racwdli&st=2025-02-25T13:28:42Z&se=2026-02-25T21:28:42Z&spr=https&sv=2022-11-02&sr=c&sig=kBFwlj5S5eqBEE32LM1EFebBY0W94uEFiwOXo3R0yt4%3D' 60 +USE [dba2] 36 36 GO 37 - 38 -{{/code}} 39 39 63 +CREATE DATABASE ENCRYPTION KEY 64 +WITH ALGORITHM = AES_256 65 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 66 +GO 40 40 41 -**~ 6. Use a Stored Procedure to Back Up to Azure Storage Account** 42 42 43 -{{code language="sql"}} 44 -EXECUTE dba.dbo.DatabaseBackup 45 -@Databases = 'dba', 46 -@URL = 'https://zagpebslab.blob.core.windows.net/sql-backups', 47 -@BackupType = 'Full', 48 -@CopyOnly = 'Y', 49 -@Compress = 'Y', 50 -@Verify = 'N' 51 -{{/code}} 69 +USE [dba3] 70 +GO 52 52 53 -=== === 72 +CREATE DATABASE ENCRYPTION KEY 73 +WITH ALGORITHM = AES_256 74 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 75 +GO 54 54 55 -=== Conclusion: Backup to Azure Storage Account with TDE Encrypted Databases === 77 +USE [xwiki] 78 +GO 56 56 57 -The attempt to back up an **encrypted database** (with Transparent Data Encryption - TDE) from **SQL Server Management Studio (SSMS)** to an Azure Storage Account was unsuccessful. This issue arises because **Azure does not permit TDE-encrypted databases to be backed up directly to Azure Storage using SSMS**. 80 +CREATE DATABASE ENCRYPTION KEY 81 +WITH ALGORITHM = AES_256 82 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 83 +GO 58 58 59 -However, after decrypting the database, we were able to successfully back it up to the Azure Storage Account via SSMS. This confirms that **backups can be performed on an unencrypted database**, but **not on an encrypted database**. 60 60 86 +-- 7. Enable Transparent Data Encryption (TDE) on the database 87 +ALTER DATABASE dba 88 +SET ENCRYPTION ON; 89 +GO 61 61 62 62 92 +select name, database_id, state_desc 93 +from sys.databases 63 63 64 64 65 -= **2. Enabling Transparent Data Encryption (TDE) Using Azure Key Vault** = 96 +SELECT 97 + database_id, 98 + key_algorithm, 99 + key_length, 100 + encryption_state_desc 101 + encryptor_type 66 66 103 +FROM 104 + sys.dm_database_encryption_keys; 67 67 68 -**Introduction:** This document outlines the process of enabling **Transparent Data Encryption (TDE)** on an **Azure SQL Managed Instance (SQL MI)** using a **Customer-Managed Key (CMK)** stored in **Azure Key Vault**. The encryption is managed using an asymmetric key from **Azure Key Vault**, ensuring that all data within the SQL Managed Instance is encrypted using a strong encryption algorithm. 69 69 107 + Select * from sys.dm_database_encryption_keys 70 70 71 -===== **1. Generate a Key in Azure Key Vault** ===== 72 72 110 + BACKUP DATABASE [dba2] 111 +TO URL = 'https://zagpebslab.blob.core.windows.net/sql-backups?sp=racwl&st=2025-02-25T13:28:42Z&se=2026-02-25T21:28:42Z&spr=https&sv=2022-11-02&sr=c&sig=eQKxB%2F0bg5cX71PmhUTlGHLCnIwSbe5CLOWVVkaDR1g%3D' 112 +With copy_only 113 +GO 73 73 74 -**- Navigate to Azure Key Vault:** 75 75 76 -* Go to the **Azure Portal** and select **Key Vault** (DemoRudiTest). 116 +BACKUP DATABASE [dba2] 117 +TO URL = 'https://zagpebslab.blob.core.windows.net/sql-backups?sp=racwl&st=2025-02-25T13:28:42Z&se=2026-02-25T21:28:42Z&spr=https&sv=2022-11-02&sr=c&sig=eQKxB%2F0bg5cX71PmhUTlGHLCnIwSbe5CLOWVVkaDR1g%3D', 118 + 119 + COPY_ONLY, -- Ensures the backup does not affect the regular backup chain 120 + COMPRESSION, -- Optional: Compresses the backup to save storage space 121 + STATS = 10 -- Optional: Provides backup progress status 122 +GO 77 77 78 -**- Generate a New Key:** 79 79 80 -* Go to the **Keys** section and click **Generate** to create a new key. 81 81 82 -**- Configure Key Settings:** 126 +-- Step 1: Drop the existing credential (if needed) 127 +DROP CREDENTIAL [https://zagpebslab.blob.core.windows.net/sql-backups]; 128 +GO 83 83 84 -* **Name the Key**: Choose a name for your key, e.g., mysqlmikey. 85 -* **Key Type**: Select **RSA** as the key type. 86 -* **RSA Key Size**: Choose an **RSA key size** of **2048-bit**. (optional) 130 +-- Step 2: Create a new credential with the SAS token 131 +CREATE CREDENTIAL [https://zagpebslab.blob.core.windows.net/sql-backups] 132 +WITH IDENTITY = 'SHARED ACCESS SIGNATURE', 133 +SECRET = 'sp=racwdli&st=2025-02-25T13:28:42Z&se=2026-02-25T21:28:42Z&spr=https&sv=2022-11-02&sr=c&sig=kBFwlj5S5eqBEE32LM1EFebBY0W94uEFiwOXo3R0yt4%3D'; 134 +GO 87 87 88 - Note: 136 +-- Step 3: Perform the database backup with the provided parameters 137 +EXECUTE dba.dbo.DatabaseBackup 138 + @Databases = 'dba', -- Replace with your database name 139 + @URL = 'https://zagpebslab.blob.core.windows.net/sql-backups', -- Azure Blob Storage URL 140 + @BackupType = 'Full', -- Full backup 141 + @CopyOnly = 'Y', -- Copy-only backup to avoid breaking backup chain 142 + @Compress = 'Y', -- Compress the backup 143 + @Verify = 'N'; -- No verification of backup 144 +GO 89 89 90 -* switching from 2048-bit RSA to 4096-bit RSA for TDE will affect performance, but the actual impact might be minor, especially on modern hardware and typical workloads. 91 -* **It will likely affect CPU usage** more than disk I/O, and the impact might be more noticeable during key management operations (key generation, encryption, etc.). 92 -* If your database is not under heavy load and your hardware can handle the extra processing, the trade-off for better security may be worth it. 93 93 94 -**- Create the Key:** 95 95 96 -* Click **Create** to generate the key. 97 97 98 -===== **2. Enable TDE on the SQL Managed Instance** ===== 99 99 100 -===== ===== 150 +select name, database_id, state_desc 151 +from sys.databases 101 101 102 -**- Navigate to Your Managed Instance:** 103 103 104 -* Go to your **SQL Managed Instance** (sqlmi-ebs-lab). 154 +SELECT 155 + database_id, 156 + key_algorithm, 157 + key_length, 158 + encryption_state_desc 159 + encryptor_type 105 105 106 -**- Enable Transparent Data Encryption (TDE):** 161 +FROM 162 + select * from sys.dm_database_encryption_keys; 107 107 108 -* Under **Security**, select **Transparent Data Encryption**. 109 109 110 - **-ConfigureTDE withaCustomer-ManagedKey(CMK):**165 + select name, is_encrypted from sys.databases 111 111 112 -* Select **Customer-managed key** as the encryption type. 113 -* Choose the key you created earlier from **Azure Key Vault** (mysqlmikey). 114 114 115 -**- Set the Key as Default TDE Protector:** 168 + SELECT 169 + cert.name AS Certificate_Name, 170 + cert.subject AS Certificate_Subject, 171 + cert.issuer_name AS Issuer_Name, 172 + cert.pvt_key_encryption_type AS Private_Key_Encryption_Type 173 +FROM 174 + sys.certificates cert 175 +WHERE 176 + cert.name LIKE 'TDE%'; 116 116 117 -* Make the key the **default TDE protector** for your instance. 178 + ALTER DATABASE dba1 179 +SET ENCRYPTION off; 180 +GO 118 118 119 -**- Save Configuration:** 120 120 121 -* Click **Save** to apply the changes. 183 +Use dba1; 184 +DROP DATABASE ENCRYPTION KEY; 122 122 123 -=== **Conclusion** === 124 124 125 -After following these steps, **TDE** has been successfully enabled on your **SQL Managed Instance** using an **asymmetric key** stored in **Azure Key Vault**. All databases within the instance are now encrypted using the same encryption key (identified by the same encryption thumbprint). You can now securely back up these encrypted databases from **SSMS** to **Azure Storage**. 187 +USE [dba1] 188 +GO 126 126 127 -This process ensures that your data is protected both at rest and during backup, offering enhanced security for your managed databases in the cloud. 190 +CREATE DATABASE ENCRYPTION KEY 191 +WITH ALGORITHM = AES_256 192 +ENCRYPTION BY SERVER CERTIFICATE TDE_Certificate; 193 +GO 128 128 129 129 130 130 131 -=== **1. Backup Specific Databases Using SQL Server Agent Jobs** === 197 +ALTER DATABASE dba1 198 +SET ENCRYPTION ON 132 132 133 -This document describes the process of creating an automated **SQL Server Agent Job** to back up only the **DBA databases** in a SQL Server instance. The backups will occur **daily at 3:00 AM** and will be stored in an **Azure Storage Account** for secure and reliable cloud storage. 134 134 135 ----- 136 136 137 -==== **1. Create the Backup Script** ==== 138 138 139 139 140 -====== **- Declaring Variables** ====== 141 141 142 -{{code language="sql"}} 143 -DECLARE @DatabaseName NVARCHAR(128) 144 -DECLARE @BackupContainerURL NVARCHAR(512) 145 -{{/code}} 146 146 147 -DECLARE @DatabaseName NVARCHAR(128) DECLARE @BackupContainerURL NVARCHAR(512) 148 148 149 -* **@DatabaseName**: A variable to hold the name of the database during each iteration of the loop. 150 -* **@BackupContainerURL**: A variable to store the URL of the Azure Blob Storage container where the database backups will be stored. 151 151 152 ----- 153 153 154 -====== **2. Setting the Azure Blob Storage URL** ====== 155 155 156 -{{code language="sql"}} 157 -SET @BackupContainerURL = 'https://<your_storage_account>.blob.core.windows.net/sql-backups/' 158 -{{/code}} 159 159 160 160 161 ----- 162 162 163 -====== **3. Declaring the Cursor** ====== 164 164 165 165 166 -{{code language="sql"}} 167 -DECLARE db_cursor CURSOR FOR 168 -SELECT name 169 -FROM sys.databases 170 -WHERE name LIKE 'dba%' -- Only pick databases that start with 'dba' 171 -{{/code}} 172 172 173 -* **Cursor Declaration**: 174 -** The cursor db_cursor is declared to loop through the list of databases in the sys.databases system view. 175 -** The **WHERE name LIKE 'dba%'** clause ensures that only databases with names starting with dba will be processed. For example, databases like dbaTest, dbaProd, etc., will be included in the loop. 176 -** **sys.databases**: A system catalog view that contains information about each database in the SQL Server instance. 177 177 178 ----- 179 179 180 -====== **4. Opening the Cursor** ====== 181 181 182 -{{code language="sql"}} 183 -OPEN db_cursor 184 -FETCH NEXT FROM db_cursor INTO @DatabaseName 185 -{{/code}} 186 186 187 -OPEN db_cursor FETCH NEXT FROM db_cursor INTO @DatabaseName 188 188 189 -* **OPEN db_cursor**: This opens the cursor for reading the results. 190 -* **FETCH NEXT**: The FETCH NEXT statement retrieves the first database name that meets the LIKE 'dba%' condition and stores it in the @DatabaseName variable. This will be used in the next step where the backup procedure is executed. 191 191 192 ----- 193 193 194 -====== **5. Looping Through Databases** ====== 195 195 196 -{{code language="sql"}} 197 --- Loop through each database and execute the stored procedure 198 -WHILE @@FETCH_STATUS = 0 199 -BEGIN 200 - -- Print current database for logging/debugging 201 - PRINT 'Backing up database: ' + @DatabaseName 202 202 203 - -- Execute the DatabaseBackup stored procedure for each database 204 - EXECUTE dba.dbo.DatabaseBackup 205 - @Databases = @DatabaseName, -- Specify the current database 206 - @URL = @BackupContainerURL, -- Azure Blob Storage URL 207 - @BackupType = 'Full', -- Full backup type 208 - @CopyOnly = 'Y', -- Use copy-only backup (this doesn’t affect the transaction log chain) 209 - @Compress = 'Y', -- Enable compression 210 - @Verify = 'N'; -- Skip verification after backup 211 211 212 -{{/code}} 213 213 214 214 215 -* **WHILE @@FETCH_STATUS = 0**: The loop will continue as long as the FETCH command successfully retrieves a database name. @@FETCH_STATUS is a system function that returns 0 if the fetch operation is successful. 216 -* **PRINT**: This line prints the name of the database that is currently being backed up. It helps with logging or debugging purposes, as you can track which database is being processed at any given time. 217 -* ((( 218 -**EXECUTE dba.dbo.DatabaseBackup**: 219 219 220 -* This calls a stored procedure named dba.dbo.DatabaseBackup for each database. 221 -* The parameters passed to the stored procedure: 222 -** **@Databases = @DatabaseName**: Specifies which database to back up (the current database being processed). 223 -** **@URL = @BackupContainerURL**: Specifies the destination Azure Blob Storage URL. 224 -** **@BackupType = 'Full'**: Specifies the backup type, which is a **Full** backup (this means all data in the database is backed up). 225 -** **@CopyOnly = 'Y'**: Specifies that this is a **copy-only backup**. This ensures that the backup does not affect the transaction log chain and does not interfere with regular backups. 226 -** **@Compress = 'Y'**: Enables compression of the backup, reducing the size of the backup file. 227 -** **@Verify = 'N'**: Specifies that the backup verification step should be skipped. (Verification can be added if needed for additional safety.) 228 -))) 229 229 230 ----- 231 231 232 -====== **6. Fetch the Next Database** ====== 233 233 234 -{{code language="sql"}} 235 - -- Fetch the next database in the cursor 236 - FETCH NEXT FROM db_cursor INTO @DatabaseName 237 237 238 -{{/code}} 239 239 240 -~-~- Fetch the next database in the cursor FETCH NEXT FROM db_cursor INTO @DatabaseName 241 241 242 -* **FETCH NEXT**: After executing the backup for the current database, this statement retrieves the next database from the cursor and stores it in the @DatabaseName variable. The loop will continue until all matching databases have been processed. 243 - 244 ----- 245 - 246 -====== **7. Closing and Deallocating the Cursor** ====== 247 - 248 248 {{code language="sql"}} 249 - --Closeand deallocate the cursorto clean upresources250 - CLOSE db_cursor251 - DEALLOCATEdb_cursor252 - 236 +USE master; 237 +GO 238 +CREATE MASTER KEY ENCRYPTION BY PASSWORD = '******'; 239 +GO 253 253 254 -{{/code}} 241 +CREATE CERTIFICATE EBSphere_TDE_SQL2019_Cert WITH SUBJECT = 'Database_Encryption'; 242 +GO 255 255 256 -* **CLOSE db_cursor**: This closes the cursor once the loop finishes processing all databases. 257 -* **DEALLOCATE db_cursor**: This deallocates the cursor, freeing up any resources used by the cursor. It’s a good practice to always deallocate cursors to avoid resource leaks. 244 +BACKUP CERTIFICATE EBSphere_TDE_SQL2019_Cert TO FILE = 'D:\temp\EBSphere_TDE_SQL2019_Cert' 245 +WITH PRIVATE KEY (file = 'D:\temp\EBSphere_TDE_SQL2019_Cert_Key.pvk', 246 +ENCRYPTION BY PASSWORD='*****') 258 258 259 -====== ====== 248 +USE Everest_TDE_Master; 249 +GO 250 +CREATE DATABASE ENCRYPTION KEY 251 +WITH ALGORITHM = AES_256 252 +ENCRYPTION BY SERVER CERTIFICATE EBSphere_TDE_SQL2019_Cert; 253 +GO 254 +ALTER DATABASE Everest_TDE_Master 255 +SET ENCRYPTION ON; 256 +GO 260 260 261 -====== **8. Full Script** ====== 262 - 263 - 264 -{{code language="sql"}} 265 -DECLARE @DatabaseName NVARCHAR(128) 266 -DECLARE @BackupContainerURL NVARCHAR(512) 267 - 268 -SET @BackupContainerURL = 'https://zagpebslab.blob.core.windows.net/sql-backups' 269 - 270 -DECLARE db_cursor CURSOR FOR 271 -SELECT name 272 -FROM sys.databases 273 -WHERE name LIKE 'dba%' 274 - 275 -OPEN db_cursor 276 -FETCH NEXT FROM db_cursor INTO @DatabaseName 277 - 278 -WHILE @@FETCH_STATUS = 0 279 -BEGIN 280 - 281 -PRINT 'Backing up database: ' + @DatabaseName 282 - 283 -EXECUTE dba.dbo.DatabaseBackup 284 - @Databases = @DatabaseName, 285 - @URL = @BackupContainerURL, 286 - @BackupType = 'Full', 287 - @CopyOnly = 'Y', 288 - @Compress = 'Y', 289 - @Verify = 'N'; 290 - 291 -FETCH NEXT FROM db_cursor INTO @DatabaseName 292 -END 293 - 294 -CLOSE db_cursor 295 -DEALLOCATE db_cursor 296 - 258 +USE Everest_TDE_Master_Documents; 259 +GO 260 +CREATE DATABASE ENCRYPTION KEY 261 +WITH ALGORITHM = AES_256 262 +ENCRYPTION BY SERVER CERTIFICATE EBSphere_TDE_SQL2019_Cert; 263 +GO 264 +ALTER DATABASE Everest_TDE_Master_Documents 265 +SET ENCRYPTION ON; 266 +GO 297 297 {{/code}} 298 - 299 - 300 - 301 - 302 -=== === 303 - 304 - 305 - 306 - 307 - 308 - 309 - 310 - 311 - 312 - 313 - 314 - 315 - 316 - 317 -
- image-20250305152543-1.png
-
- Author
-
... ... @@ -1,1 +1,0 @@ 1 -XWiki.rudim - Size
-
... ... @@ -1,1 +1,0 @@ 1 -19.6 KB - Content
- XWiki.XWikiComments[0]
-
- Date
-
... ... @@ -1,1 +1,0 @@ 1 -2025-02-26 16:00:04.223 - Author
-
... ... @@ -1,1 +1,0 @@ 1 -XWiki.rudim - Comment
-
... ... @@ -1,1 +1,0 @@ 1 -put the sql code in code tags to start with otherwise its very hard to read