在 SQL 中恢复加密备份

Restore encrypted Backup in SQL

我用这个代码

USE master;  
GO  
CREATE MASTER KEY ENCRYPTION BY PASSWORD = '1234';  
GO  
CREATE CERTIFICATE Cer
   WITH SUBJECT = 'Hello';  
   GO

然后用这段代码备份数据库

backup database Temp to disk = 'D:\Backup\temp.bak'
 WITH  
  COMPRESSION,  
  ENCRYPTION   
   (  
   ALGORITHM = AES_256,  
   SERVER CERTIFICATE = Cer
   ),
  STATS = 10

现在我无法将它恢复到另一台服务器 我在另一台服务器上创建了这个主密钥,但它不起作用

master key用于保护certificate用于备份加密。 certificate 是唯一的 - 为执行 CREATE CERTIFICATE 语句指定相同的名称会创建不同的证书。

如果这不是真的,每个有权访问您的备份的人都可以解密它。

因此,您需要导出证书:

-- Backup the certificate
BACKUP CERTIFICATE BackupEncryptCert TO FILE = 'H:\MSSQL\Backup\Keys\BackupEncryptCert.cer'
    WITH PRIVATE KEY (FILE = 'H:\MSSQL\Backup\Keys\BackupEncryptCert.PrivateKey.pvk', 
    ENCRYPTION BY PASSWORD = 'doodle')
GO

然后在新实例上创建它:

-- Restoring a certificate from existing certificate backup
CREATE CERTIFICATE BackupEncryptCert
    FROM FILE = 'H:\MSSQL\Backup\Keys\BackupEncryptCert.cer'
    WITH PRIVATE KEY (FILE = 'H:\MSSQL\Backup\BackupEncryptCert.PrivateKey.pvk',
    DECRYPTION BY PASSWORD = 'doodle')
GO

然后您将能够恢复您的加密备份。