-- Let's create the database master key and a certificate with the same name
-- But not from the files. Note the difference in passwords
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = 'SecondServerPassw0rd!';
GO
-- Though this certificate has the same name, the restore won't work
CREATE CERTIFICATE TDECert
WITH SUBJECT = 'TDE Cert for Test';
GO
-- Since we don't have the corrected certificate, this will fail, too.
RESTORE DATABASE [RecoveryWithTDE]
FROM DISK = N'C:\SQLBackups\RecoveryWithTDE_Full.bak'
WITH MOVE 'RecoveryWithTDE' TO N'C:\SQLData\RecoveryWithTDE_2ndServer.mdf',
MOVE 'RecoveryWithTDE_log' TO N'C:\SQLData\RecoveryWithTDE_2ndServer_log.mdf';
GO
复制代码
请注意数据库主密钥密码(与第一台机器上的主密钥密码)是的不同,这两个主密钥的密码这是不同的,但这不是我们在恢复中失败的原因。和之前的情况一样,我们没有正确的证书。结果,您将得到与前一种情况相同的错误。 Failed Restore - The Right Certificate, but Without the Private Key
为了执行成功的恢复,我们需要主数据库中的数据库主密钥,我们需要恢复用于加密数据库的证书,但我们需要确保使用私钥恢复它。以核对表形式:1,在主数据库中有一个数据库主密钥master key。2,用于加密数据库的证书将与其私钥一起恢复(译者注:还原证书时需要与创建证书时的私钥的密码一致,也即 DECRYPTION BY PASSWORD必须与创建证书时一致,否则无法还原证书)。3,完成数据库恢复。既然有了数据库主密钥master key,那么让我们执行最后两个步骤。当然,由于我们必须清除以前的证书,我们将在我们发出的命令中有一个删除证书:
-- Let's do this one more time. This time, with everything,
-- Including the private key.
DROP CERTIFICATE TDECert;
GO
-- Restoring the certificate, but without the private key.
CREATE CERTIFICATE TDECert
FROM FILE = 'C:\SQLBackups\TDECert.cer'
WITH PRIVATE KEY (
FILE = N'C:\SQLBackups\TDECert_key.pvk',
DECRYPTION BY PASSWORD = 'APrivateKeyP4ssw0rd!'
);
GO
-- We have the correct certificate and we've also restored the
-- private key. Now everything should work. Finally!
RESTORE DATABASE [RecoveryWithTDE]
FROM DISK = N'C:\SQLBackups\RecoveryWithTDE_Full.bak'
WITH MOVE 'RecoveryWithTDE' TO N'C:\SQLData\RecoveryWithTDE_2ndServer.mdf',
MOVE 'RecoveryWithTDE_log' TO N'C:\SQLData\RecoveryWithTDE_2ndServer_log.mdf';
GO
复制代码
一切就绪,我们终于成功了!
如果在执行恢复时没有看到升级步骤消息,请不要惊慌。对于这个例子,我在SQL Server 2008 R2实例上创建了初始数据库,并将其恢复到SQL Server 2014实例上。因此,如果您有关于是否可以将受TDE保护的数据库恢复到不同版本的SQL Server的问题,那么答案是肯定的,只要新实例运行的是企业版,并且新实例具有相同或更高版本的SQL Server(包括服务包、累积更新和任何热修复程序/补丁)。第二个要求不应该令人惊讶,因为这是将数据库恢复到不同服务器的标准要求。