Move a TDE Protected Database to Another SQL Server
This topic describes how to to protect a database by using transparent data encryption (TDE), and then move the database to another instance of SQL Server by using SQL Server Management Studio or Transact-SQL. TDE performs real-time I/O encryption and decryption of the data and log files. The encryption uses a database encryption key (DEK), which is stored in the database boot record for availability during recovery. The DEK is a symmetric key secured by using a certificate stored in the master database of the server or an asymmetric key protected by an EKM module.
In This Topic
-
Before you begin:
-
To create a database protected by transparent data encryption, using:
-
To move a database, using:
Limitations and Restrictions
-
When moving a TDE protected database, you must also move the certificate or asymmetric key that is used to open the DEK. The certificate or asymmetric key must be installed in the master database of the destination server, so that SQL Server can access the database files. For more information, see Transparent Data Encryption (TDE).
-
You must retain copies of both the certificate file and the private key file in order to recover the certificate. The password for the private key does not have to be the same as the database master key password.
-
SQL Server stores the files created here in C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA by default. Your file names and locations might be different.
Security
Permissions
-
Requires CONTROL DATABASE permission on the master database to create the database master key.
-
Requires CREATE CERTIFICATE permission on the master database to create the certificate that protects the DEK.
-
Requires CONTROL DATABASE permission on the encrypted database and VIEW DEFINITION permission on the certificate or asymmetric key that is used to encrypt the database encryption key.
Using SQL Server Management Studio
-
Create a database master key and certificate in the master database. For more information, see Using Transact-SQL below.
-
Create a backup of the server certificate in the master database. For more information, see Using Transact-SQL below.
-
In Object Explorer, right-click the Databases folder and select New Database.
-
In the New Database dialog box, in the Database name box, enter the name of the new database.
-
In the Owner box, enter the name of the new database's owner. Alternately, click the ellipsis (…) to open the Select Database Owner dialog box. For more information on creating a new database, see Create a Database.
-
In Object Explorer, click the plus sign to expand the Databases folder.
-
Right-click the database you created, point to Tasks, and select Manage Database Encryption.
The following options are available on the Manage Database Encryption dialog box.
-
When finished, click OK.
Using Transact-SQL
-
In Object Explorer, connect to an instance of Database Engine.
-
On the Standard bar, click New Query.
-
Copy and paste the following example into the query window and click Execute.
-- Create a database master key and a certificate in the master database. USE master ; GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = '*rt@40(FL&dasl1'; GO CREATE CERTIFICATE TestSQLServerCert WITH SUBJECT = 'Certificate to protect TDE key' GO -- Create a backup of the server certificate in the master database. -- The following code stores the backup of the certificate and the private key file in the default data location for this instance of SQL Server -- (C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA). BACKUP CERTIFICATE TestSQLServerCert TO FILE = 'TestSQLServerCert' WITH PRIVATE KEY ( FILE = 'SQLPrivateKeyFile', ENCRYPTION BY PASSWORD = '*rt@40(FL&dasl1' ); GO -- Create a database to be protected by TDE. CREATE DATABASE CustRecords ; GO -- Switch to the new database. -- Create a database encryption key, that is protected by the server certificate in the master database. -- Alter the new database to encrypt the database using TDE. USE CustRecords; GO CREATE DATABASE ENCRYPTION KEY WITH ALGORITHM = AES_128 ENCRYPTION BY SERVER CERTIFICATE TestSQLServerCert; GO ALTER DATABASE CustRecords SET ENCRYPTION ON; GO
For more information, see:
Using SQL Server Management Studio
-
In Object Explorer, right-click the database you encrypted above, point to Tasks and select Detach….
The following options are available in the Detach Database dialog box.
-
Click OK.
-
Using Windows Explorer, move or copy the database files from the source server to the same location on the destination server.
-
Using Windows Explorer, move or copy the backup of the server certificate and the private key file from the source server to the same location on the destination server.
-
Create a database master key on the destination instance of SQL Server. For more information, see Using Transact-SQL below.
-
Recreate the server certificate by using the original server certificate backup file. For more information, see Using Transact-SQL below.
-
In Object Explorer in SQL Server Management Studio, right-click the Databases folder and select Attach….
-
In the Attach Databases dialog box, under Databases to attach, click Add.
-
In the Locate Database Files – server_name dialog box, select the database file to attach to the new server and click OK.
The following options are available in the Attach Databases dialog box.
Using Transact-SQL
-
In Object Explorer, connect to an instance of Database Engine.
-
On the Standard bar, click New Query.
-
Copy and paste the following example into the query window and click Execute.
-- Detach the TDE protected database from the source server. USE master ; GO EXEC master.dbo.sp_detach_db @dbname = N'CustRecords'; GO -- Move or copy the database files from the source server to the same location on the destination server. -- Move or copy the backup of the server certificate and the private key file from the source server to the same location on the destination server. -- Create a database master key on the destination instance of SQL Server. USE master; GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = '*rt@40(FL&dasl1'; GO -- Recreate the server certificate by using the original server certificate backup file. -- The password must be the same as the password that was used when the backup was created. CREATE CERTIFICATE TestSQLServerCert FROM FILE = 'TestSQLServerCert' WITH PRIVATE KEY ( FILE = 'SQLPrivateKeyFile', DECRYPTION BY PASSWORD = '*rt@40(FL&dasl1' ); GO -- Attach the database that is being moved. -- The path of the database files must be the location where you have stored the database files. CREATE DATABASE [CustRecords] ON ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustRecords.mdf' ), ( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\CustRecords_log.LDF' ) FOR ATTACH ; GO
For more information, see:
