In Focus

Backup Encryption In SQL Server

Nowadays security breaches happen way to frequently. As DBA’s we should all take extra care with sensitive data and ensure we are encrypting correctly. But some of us forget to take it a step farther and don’t encrypt our backups. For those working in an ultra data sensitive environment I highly recommend you go this extra step.

Nowadays security breaches happen way too frequently. As DBA’s we should all take extra care with sensitive data and ensure we are encrypting correctly. But some of us forget to take it a step further and don’t encrypt our backups. For those working in an ultra data sensitive environment I highly recommend you go this extra step.

As of SQL 2014, SQL Server now has the ability to encrypt your data during the backup process. Not only that but they have made the process simple. Prior to 2014, the only way to encrypt backups was Transparent Data Encryption (TDE), and that requires Enterprise Edition which made this out of reach for most.

First you must create a MASTER KEY and a Certificate ( or asymmetric key). You will notice that the Certificate has an expiration date. If this expires don't fret, you will still be able to restore you backup, however you will not be able to perform new backups until you create a new certificate. Depending on your environment you may have to have a routine process of managing your certificates and renewals. If you don't, take care about what date you set  to to avoid this.

  1. USE Master;   
  2. GO   
  3. CREATE MASTER KEY ENCRYPTION BY PASSWORD = '0BLrgboCfn6hwDc';   
  4. GO  
  5.   
  6. USE Master;   
  7. GO  
  8. CREATE CERTIFICATE ExampleCert1    
  9.    WITH SUBJECT = 'This is a Test',    
  10.    EXPIRY_DATE = '20200831';   
  11. GO  

To verify it's created you can run the below.

  1. SELECT C.name,C.certificate_id,C.pvt_key_encryption_type_desc,C.subject,C.start_date,C.expiry_date FROM sys.certificates AS C WHERE C.name = 'ExampleCert1';  

After that is done you’re ready to create your encrypted backup. Before you do you must however make sure you have the VIEW DEFINITION permission for the Certificate.

  1. use [Master]  
  2. GO  
  3. GRANT VIEW DEFINITION ON CERTIFICATE::[ExampleCert1] TO JoeShmo  
  4. GO  

Using GUI,

Under the MASTER Database go to Security then User.

Choose the User you want to grant permission to then go to properties.

Click on Securables, you will note it's blank. You need to choose search.

Securables

Choose CERTIFICATES,

CERTIFICATES

Check the Certificate you are granted rights to and check GRANT for VIEW DEFINITION

VIEW DEFINITION

Now try backing up your database. Note the Encryption option and the server certificate name.

  1. BACKUP DATABASE [AdventureWorks2014]   
  2. TO DISK = N'C:\temp\AWEncrypt.bak'   
  3. WITH   
  4.   COMPRESSION,   
  5.   ENCRYPTION    
  6.    (   
  7.    ALGORITHM = AES_256,   
  8.    SERVER CERTIFICATE = [ExampleCert1]   
  9.    ),   
  10.   STATS = 10   
  11. GO  

Or using GUI,

GUI

If you don’t have the correct permission, you will see this error when you try to perform the back up.

error

Now that you have a backup we need to see if we can restore it. But first this is important, so I am going to quote Microsoft directly here from MSDN, without the key and certificate YOU CANNOT RESTORE!

“Create a backup of the encryption certificate and keys to a location other than your local machine where the instance is installed. To account for disaster recovery scenarios, consider storing a backup of the certificate or key to an off-site location. You cannot restore an encrypted backup without the certificate used to encrypt the backup.”

Restoring is just a normal restore as long as the key and certificate are present on that server.

  1. USE [master]  
  2. RESTORE DATABASE [AdventureWorks2014]  
  3. FROM DISK = N'C:\temp\AWEncrypt.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5  
  4. GO  

It’s a pretty simple process to ensure your backups are encrypted. The tricky part is to make sure you back up the Master Key and Certificate and safe guard them. If you don’t have them you can't restore (it's worth repeating). So take the extra step if you can and make your data even more secure by encrypting your backups.