转载自: Microsoft MVP Award Program Blog
来源:Microsoft MVP Award Program Blog 的博客:https://blogs.msdn.microsoft.com/mvpawardprogram/2014/06/02/sql-server-2014-backup-encryption/
How does backup encryption work ?
There is no encryption without keys, and backup encryption is no exception : we first need to create a Database Master Key (DMK) of the master system database, and a certificate. The DMK is a symmetric key, and is unique to each database in each SQL Server instance : we cannot restore an encrypted backup file on a distinct or re-installed SQL Server instance without the master system database DMK (or an encrypted database without its DMK).
数据库备份文件加密是如何工作的?
加密必须要使用秘钥,SQL备份文件加密也不例外。
我们首先需要在系统数据库master上创建一个database master key(DMK)以及一张证书。
DMK是一个对称秘钥,并且在每个SQL实例的每个数据库都是唯一的。在master系统数据库不存在DMK的情况下,我们无法使用加密的备份文件(或者加密的数据库),在不同的SQL实例或者重新安装的SQL实例上进行还原。
The Database Master Key is encrypted by the AES 256 algorithm, using the Service Master Key, which is also a symmetric key. It is encrypted based on the SQL Server service account credentials and the Windows Data Protection API machine key. The Service Master Key is unique per SQL Server instance, and created during SQL Server installation. It is stored in the master system database and in the user database, so as to enable its automatic decryption (cf. sys.symmetric_keys system view).
DMK使用SMK、AES 256算法进行加密,它也是一个对称秘钥。加密基于SQL实例的启动账号证书和Windows数据保护API机器秘钥。SMK在每个SQL实例下是唯一的,随SQL实例的安装同时生成。它保存在master系统数据库以及用户数据库中,为了达到自动解密的目的。(查阅系统视图sys.symmetric_keys system view)
Finally, a Certificate contains a public key that is digitally signed, and may contain a private key. This private key is protected by the DMK. While SQL Server can generate IETF X.509v3-compliant certificates, it also allows to use certificates generated by third-parties (cf. sys.certificates system view).
最终,一张证书包含一个数字签名的公钥,并可能包含一个私钥。这个私钥受到DMK的保护。SQL Server能够生成IETF X.509v3-兼容证书,这种类型允许使用第三方生成的证书。
We can summarize the encryption hierarchy levels with the following diagram :
以下是加密的层级架构:
The backup process works on a data page basis : it copies data pages from the data files into the backup file. Since SQL Server 2008, we can compress database backups. This feature is supported by an algorithm similar to the ones behind file compression software : it factorizes patterns of data found in the data pages. Whether the backup data pages are compressed or not, SQL Server 2014 is able to encrypt these pages with the AES 128, AES 192, AES 256 or Triple DES algorithms.
备份程序以数据页为最小单元进行工作:将数据页复制到备份文件中。从SQL Server 2008之后,数据库备份文件支持压缩功能。这个特性被一种和文件压缩软件相似的算法支持:it factorizes patterns of data found in the data pages。无论备份文件的数据页是否使用被压缩,SQL Server 2014都能够使用AES 128, AES 192, AES 256 or 三重 DES算法加密这些数据页。
Encrypting a database backup in T-SQL
In this example, we will use the
Contoso demo database. The code needed to take an encrypted database backup is following the steps described in the previous section. First of all, we must backup the service master key :
使用T-SQL代码加密数据库备份文件
以下示例中,我们将使用名为Contoso的数据库进行演示。首先,我们必须备份SMK:
USE master
GO
— 将SQL实例的 service master key备份到磁盘上
— 理想情况下, 此服务器主钥,应该存储在访问受到保护的,和数据库服务器不同的机器上。
BACKUP SERVICE MASTER KEY
TO FILE ='g:\service-master-key.key'
ENCRYPTION BY PASSWORD = ‘q1w2e3r4.smk’;
GO
We then have to create the master key, which again is straightforward. The password used to encrypt the master key can be different than the one used to encrypt the service master key backup file.
然后我们要在master数据库下创建主钥,这一步很简单。同时,你可以使用和SMK不同的密码值来加密(数据库)主钥。
— 创建一个新的数据库主钥 DMK
CREATE MASTER KEY
ENCRYPTION BY PASSWORD = 'q1w2e3r4.dmk'; --这个密码在之后使用open master key时,解密主钥时会用到(译者注)
GO
— 将数据库的主钥备份到磁盘上
— 理想情况下, 此数据库主钥,应该存储在访问受到保护的,和数据库服务器不同的机器上。
BACKUP MASTER KEY
TO FILE = 'g:\database(master)-master-key.key'
ENCRYPTION BY PASSWORD ='q1w2e3r4.dmk-1';
GO
We can confirm the creation of the master key by examining the sys.symmetric_keys system view : it now shows one, named ##MS_DatabaseMasterKey##. Next, we need to create the certificate, which is achieved with a single instruction :
我们可以通过查询系统视图:sys.symmetric_keys 来确认刚才创建的(数据库)主钥。正常的返回结果应该是一个名字包含##MS_DatabaseMasterKey##的记录。下一步,我们使用一句简单的単行命令创建证书:
USE master
GO
— 创建证书
CREATE CERTIFICATE Contoso_BackupEncryptionWithSQLServer2014
WITH SUBJECT = 'used for contoso db';
GO
Similarly, interrogating the sys.certificates system view in the master database context will now return a supplementary row, and the column pvt_key_encryption_type_desc indicates ENCRYPTED_BY_MASTER_KEY.
同样地,在master数据库中,查询系统视图sys.certificates将返回一行新的结果,其中名为pvt_key_encryption_type_desc列的值应该显示为:ENCRYPTED_BY_MASTER_KEY。
As for every key, we want to save the certificate : this requires to specify a private key file. It clearly implies that one does not work without the other. Here again, a password is required to cipher the certificate file, and
optionally a different one can be defined for the decryption. This password is the public key of the certificate.
对于每一个秘钥,我们可以使用以下方法保存他的证书:这一步骤需要指定一个私钥文件。 It clearly implies that one does not work without the other。同样的,证书文件需要一个密码去加密它(证书本身)。另外一个可选的选项是,解密可以使用不同的密码。这个密码是证书的公钥。
— 保存证书
— 理想情况下, 此服务器主钥,应该存储访问受到保护的,和数据库服务不同的机器上。
BACKUP CERTIFICATE Contoso_BackupEncryptionWithSQLServer2014
TO FILE ='g:\cert.cert'
WITH PRIVATE KEY
(
FILE ='g:\private-key.key'
, ENCRYPTION BY PASSWORD = 'q1w2e3r4.pk'
);
GO
At this point, the groundwork is laid for taking encrypted backups. We have 4 files that we need to keep in a secure and distinct storage area :
进行到这一步,我们将获得以下4个文件:
1) service-master-key.key
2) database(master)-master-key.key
3) private-key.key
4) cert.cert
Backing up a database and ciphering the resulting file only requires us to choose an encryption algorithm and to specify the certificate we want to use :
之后,我们备份这个数据库,并且加密它的备份文件,很简单,只需要提供加密算法类型以及证书就行了:
— Backing up the ContosoRetailDW demo database, with encryption
BACKUP DATABASE ContosoRetailDW
TO DISK = 'g:\ContosoRetailDW_FULL_ENCRYPTED.bak'
WITH INIT, CHECKSUM, COMPRESSION, STATS = 1
, ENCRYPTION
(
ALGORITHM = AES_256
, SERVER CERTIFICATE = Contoso_BackupEncryptionWithSQLServer2014
)
Restoring a database from an encrypted backup file
Before we walk thorugh the restoration process, we must keep in mind that SQL Server 2014 introduces native backup encryption. Thus, restoring a database from a natively encrypted backup file on a version of SQL Server that is anterior to SQL Server 2014 is not supported.
使用加密的备份文件进行还原
在开始还原操作之前,我们必须理解SQL Server 2014本地备份文件加密技术是什么样子的。因此,想在比SQL Server 2014版本跟早的版本之上,使用加密的备份文件还原一个数据库是不支持的。
Restoring the database on the same SQL Server 2014 instance
Restoring a database from an encrypted backup file on the same SQL Server 2014 instance as the one on which its backup has been taken is operated as usual : all the keys and the certificate are already registered in the master database. Consequently, they are opened automatically when needed for decryption.
使用加密的备份文件,在同SQL实例下还原数据库。由于所有涉及到的钥匙都已经就位于master数据库中,因此当需要使用到他们时,会自动打开以及解密。
RESTORE DATABASE ContosoRetailDW_RestoredFromEncryptedBackupFile
FROM DISK = ‘E:\SQLServerBackup\ContosoRetailDW\ContosoRetailDW_FULL_ENCRYPTED.bak’
WITH MOVE ‘ContosoRetailDW2.0’ TO ‘E:\Contoso\ContosoRetailDW_EncrypteBackup_data.mdf’ --这里改成你的路径
, MOVE ‘ContosoRetailDW2.0_log’ TO ‘E:\Contoso\ContosoRetailDW_EncrypteBackup_log.ldf’ --这里改成你的路径
, STATS = 1
- SQL Server 2014新特性-原生备份加密
注:本篇文章是IT68找我的约稿,原文地址:http://tech.it168.com/a2014/0610/1633/000001633147.shtml SQL Server 2014 ...
- 看完SQL Server 2014 Q/A答疑集锦:想不升级都难!
看完SQL Server 2014 Q/A答疑集锦:想不升级都难! 转载自:http://mp.weixin.qq.com/s/5rZCgnMKmJqeC7hbe4CZ_g 本期嘉宾为微软技术中心技术 ...
- SQL Server 2014里的IO资源调控器
在本文中,我们将来看看SQL Server 2014在资源调控器方面增加了哪些新的功能.资源调控器(Resource Governor)是从SQL Server 2008开始出现的一项功能.它是用于管 ...
- SQL Server 2014 虚拟机的自动备份 (Resource Manager)
自动备份将在运行 SQL Server 2014 Standard 或 Enterprise 的 Azure VM 上自动为所有现有数据库和新数据库配置托管备份到 Azure. 这样,便可以配置使用持 ...
- SQL Server 2014 新特性——内存数据库
SQL Server 2014 新特性——内存数据库 目录 SQL Server 2014 新特性——内存数据库 简介: 设计目的和原因: 专业名词 In-Memory OLTP不同之处 内存优化表 ...
- 谈谈我的微软特约稿:《SQL Server 2014 新特性:IO资源调控》
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 撰写经历(Experience) 特约稿正文(Content-body) 第一部分:生活中资源 ...
- Windows 10 安装 Sql Server 2014 反复提示需要安装 .NET Framework 3.5 SP1 的解决方案
一.首先安装.NET Framework 3.5: 离线安装方式: 1.装载相对应的系统安装盘,我是Windows 10 x64 企业版,所以装载Windows 10 x64 企业版安装镜像ISO,盘 ...
- SQL Server 2014 Database Mail重复发送邮件特殊案例
在一数据库服务器(Microsoft SQL Server 2014 (SP2) (KB3171021) - 12.0.5000.0 (X64))发现有个作业调用Database Mail发送邮件时, ...
- SQL Server 2014新特性:其他
AlwaysOn 增强功能 SQL Server 2014 包含针对 AlwaysOn 故障转移群集实例和 AlwaysOn 可用性组的以下增强功能: “添加 Azure 副本向导”简化了用于 Alw ...
随机推荐
- winfrom 隐藏任务栏(win7)
1:新建winfrom 窗体应用程序 2:拖入contextMenuStrip.notifyIcon 2个控件 3:如图 4:code: 注意:复制控件事件要注册 using System; usin ...
- node01-创建服务器
node学习笔记目录:node01-创建服务器 node02-util node03-events node04-buffer node05-fs node06-path node07-http no ...
- button按钮
button按钮只加类名不加type时,点击此按钮页面会刷新
- Android Telephony —— 手机信号实时变化源码分析过程记录
源码版本:4.4 跳过InCallActivity等UI实现.先看service以及底层. 1, 在frameworks/opt下面会发现如下文件列表: ./telephony/src/java/co ...
- Oracle资源管理器介绍(一)
数据库资源管理器通过控制数据库内部的执行调度来控制资源在各个会话之间的分布.通过控制所要运行的会话以及会话运行的时间长度,数据库资源管理器可以确保资源分布与计划指令相匹配,因此也符合业务目标. ...
- Jetty官方文档翻译
最近在学习Jetty,没有找到合适的资料,所有只能看官方文档了,但是只有英文的,想着自己翻译着学也是学还不如把学习的过程放到网上,也可以给需要的人看,英文水平毕竟有限,也是用有道翻译着来的,不过也加了 ...
- 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。
题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...
- Linux下定时任务配置-crontab
实际中经常有一些任务需要定期执行,人工操作比较麻烦,如果定时执行将会省去很多人力,还可以在一些资源占用不多的时间段执行,linux下crontab命令就实现了这一便捷的功能,实现脚本的自动化运行. 常 ...
- 在android studio 中使用applicationid的问题
现在我需要对项目app的某个功能做性能测试,主要测试耗电量的多少. 1.我想到的方式是,我需要在同一台手机测试,同一个应用,需要安装在手机两次,第二次安装不覆盖第一次的安装. 在android stu ...
- 单例模式双重检查锁(DCL)问题
单例模式DoubleCheck 锁问题 先贴代码 public class DoubleCheckSingleton { private static DoubleCheckSingleton ins ...