我正在尝试在 SQL 2017 中配置备份加密,但遇到了一些问题。
我在一台机器上创建主密钥和证书没有问题。将证书安装在另一台机器上并读取备份也没有问题,但是,如果另一台机器运行的服务帐户与创建证书的服务帐户不同,我有一个问题。
以下是我正在采取的步骤(我也尝试恢复主密钥,但这也会引发错误):
/* Server 1 */
/* Create the master key */
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'SomeRandomSecureString';
GO
/* Create the certificate to be used for backups */
CREATE CERTIFICATE BackupCert
WITH SUBJECT = 'Backup Encryption Certificate';
GO
/* Backup the master key */
BACKUP MASTER KEY TO FILE = '\\FileShare\DatabaseMasterKey_Master.key'
ENCRYPTION BY PASSWORD = 'SomeRandomPwd';
BACKUP CERTIFICATE BackupCert TO FILE = '\\FileShare\BackupCert.cer'
WITH PRIVATE KEY (FILE = '\\FileShareBackupCert.pvk',
ENCRYPTION BY PASSWORD = 'RandomEncryptionPwd');
GO
/* Server 2 */
/* Create master key */
USE master;
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'SomeRandomSecureString';
GO
/* Restore the cert */
CREATE CERTIFICATE BackupCert FROM FILE = '\\FileShare\BackupCert.cer'
WITH PRIVATE KEY (FILE = '\\FileShare\BackupCert.pvk',
DECRYPTION BY PASSWORD = 'RandomEncryptionPwd');
--Msg 15208, Level 16, State 6, Line 32
--The certificate, asymmetric key, or private key file is not valid or does not exist; or you do not have permissions for it.
/* Try restoring the master key instead */
DROP MASTER KEY;
RESTORE MASTER KEY FROM FILE = '\\FileShare\DatabaseMasterKey_Master.key'
DECRYPTION BY PASSWORD = 'RandomEncryptionPwd'
ENCRYPTION BY PASSWORD = 'RandomEncryptionPwd';
--Msg 15317, Level 16, State 2, Line 39
--The master key file does not exist or has invalid format.
我查看的参考资料试图弄清楚这里发生了什么:
- https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/backup-encryption?view=sql-server-2017
- https://learn.microsoft.com/en-us/sql/relational-databases/security/encryption/create-a-database-master-key?view=sql-server-2017
- https://learn.microsoft.com/en-us/sql/t-sql/statements/create-certificate-transact-sql?view=sql-server-2017
- https://learn.microsoft.com/en-us/sql/relational-databases/backup-restore/create-an-encrypted-backup?view=sql-server-2017
我还确保所有机器之间的 SMK 都是相同的,但在恢复证书方面仍然没有运气。
我确信我在这里做错了什么,但我无法弄清楚是什么。
任何想法,将不胜感激。谢谢。
您需要修复证书上的 NTFS 权限。默认情况下,SQL 在创建备份时会设置 NTFS 权限,这样只有运行 SQL 服务的帐户才能读取证书备份文件。
这就是为什么它适用于单个帐户,而不适用于两个帐户。