本文介绍将备份追加到现有媒体集时压缩备份的行为。
原始产品版本:SQL Server
原始 KB 数: 2297053
总结
压缩备份的主要限制之一是,压缩和未压缩的备份不能共存于介质集中。 备份压缩(SQL Server)中记录了此限制。
本文补充了该文档,并提供了有关压缩备份的预期行为的详细信息,与服务器配置选项 - 备份压缩默认值有关。
现象
假设出现了下面这种情景:
在此方案中,你会注意到备份成功,但最终可能处于与预期不同的压缩状态。
详细信息
将备份追加到现有介质集时,备份将从介质集继承压缩设置。
创建媒体集时,有关此媒体集的压缩设置的信息将写入媒体头文件。
仅当这些备份的压缩设置与媒体集的压缩设置相同时,才会共存到现有介质集。 以下三个因素会影响压缩备份的行为:
SQL Server 的配置选项 - 备份压缩默认值
备份集选项 - COMPRESSION 或 NO_COMPRESSION
对于现有媒体,需要考虑的一个重要因素是介质集当前包含压缩备份还是未压缩备份。
下表总结了基于上述三个因素的压缩备份的行为:
| Backup 语句 | 新媒体集 | 追加到具有压缩备份的现有媒体集 | 追加到具有未压缩备份的现有媒体集 |
|---|---|---|---|
语句级别子句 WITH COMPRESSION |
备份成功,将压缩 | 成功 | 错误 |
语句级别子句 WITH NO_COMPRESSION |
备份成功,将取消压缩 | 错误 | 成功 |
| 没有语句级别压缩子句的备份语句 | 备份成功,压缩取决于系统存储过程的选项backup compression defaultsp_configure |
备份成功,将压缩 | 备份成功,将取消压缩 |
如上表所示,使用服务器上的选项 backup compression default 并将压缩备份追加到现有媒体集时,由于压缩设置不匹配,备份永远不会失败。 它有效,但继承媒体集标头中的设置。 但是,如果在指定子句 WITH COMPRESSION 或 WITH NO_COMPRESSION 备份语句中,如果在媒体集中存储的备份与正在执行的当前备份不匹配,则在压缩设置方面将引发错误。
注意
通过在 SQL Server Management Studio 中运行系统存储过程backup compression default,可以找到该选项sp_configure的当前设置。 如果要将压缩备份追加到现有媒体,则可以使用语句 RESTORE HEADERONLY 获取标头信息。 有关详细信息,请参阅示例部分。
示例
下面是一些脚本示例,用于演示各种情况的行为。 备份是磁带还是磁盘的行为相同。
示例 1:当选项的值为
backup compression default时0,使用语句级别子句WITH COMPRESSION将备份追加到具有未压缩备份设置的现有媒体集:检查压缩值:
-- The value of the option "backup compression default" is 0 by default sp_configure 'backup compression default'使用子句
WITH FORMAT创建新的媒体集:BACKUP DATABASE test TO DISK = N'E:\testbackup.bak' WITH FORMAT, INIT, NAME = N'testbackup-Full Database Backup', SKIP, NOUNLOAD, STATS = 10 GO检查备份和标头,并查看压缩列值为 0:
RESTORE HEADERONLY FROM DISK = N'E:\testbackup.bak'使用子句
test备份数据库WITH COMPRESSION:-- The backup will fail as compressed and non compressed backups can't be mixed within the same media set BACKUP DATABASE test TO DISK = N'E:\testbackup.bak' WITH NAME = N'testbackup-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION, STATS = 10 GO执行 SQL 脚本后,可能会收到 错误消息 3098 和 3013。
示例 2:将选项
backup compression default1的值设置为:在服务器级别打开
backup compression default:-- The option "backup compression default" as this point is set to 1. sp_configure 'backup compression default', 1 GO RECONFIGURE GO将备份追加到同一介质集:
-- Given that you may expect the backup to be compressed and it will be if it is a new media set. -- However, if you have a backup and append the backup to the same media set, -- the backup works but results in an uncompressed backup. BACKUP DATABASE test TO DISK = N'E:\testbackup.bak' WITH NAME = N'testbackup-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10 GO执行 SQL 脚本后,可以看到以下输出:
Processed two pages for database `test`, file _test_log_ on file 2. BACKUP DATABASE successfully processed 162 pages in 6.211 seconds (0.203 MB/sec).检查备份和媒体集标头:
-- Then, you will see that though Server default is set to compressed, the backup given that -- it is appended to an existing media set inherits the compression setting of the media set itself. -- You may expect this to have failed with the same error as when specifying the clause `WITH COMPRESSION` -- in the backup statement given that compressed and non compressed backups can't co-exist in the media set. RESTORE HEADERONLY FROM DISK = N'E:\testbackup.bak'
示例 3:将选项
backup compression default0的值设置为,并将备份追加到具有压缩备份设置的现有媒体集:使用子句
WITH FORMAT创建新的媒体集:-- If you create a new media set by using the FORMAT option, the current compression setting is inherited BACKUP DATABASE test TO DISK = N'E:\testbackup.bak' WITH FORMAT, INIT, NAME = N'testbackup-Full Database Backup', SKIP, NOUNLOAD, STATS = 10 GO检查备份和媒体集标头:
RESTORE HEADERONLY FROM DISK = N'E:\testbackup.bak'将选项
backup compression default设置回0:sp_configure 'backup compression default', 0 GO RECONFIGURE GO使用子句
test将数据库WITH INIT备份到同一媒体集:-- If you use the clause "WITH INIT", the backup sets are overwritten but the media header is not BACKUP DATABASE test TO DISK = N'E:\testbackup.bak' WITH INIT, NAME = N'testbackup-Full Database Backup', SKIP, NOUNLOAD, STATS = 10 GO检查备份和媒体集标头:
-- Note that even though we changed backup compression default to 0, the old media header is preserved which has it as 1, and the backup goes as compressed RESTORE HEADERONLY FROM DISK = N'E:\testbackup.bak'
示例 4:压缩备份无法与具有未压缩设置的 NT 备份共存:
执行 NT 备份并验证备份标头:
-- You can see that it is not a SQL backup and the value of compressed is 0 RESTORE HEADERONLY FROM TAPE = N'\\.\Tape0'使用子句将数据库
test备份到同一媒体集,并WITH INIT:WITH COMPRESSIONBACKUP DATABASE test TO TAPE = N'\\.\Tape0' WITH INIT, COMPRESSION, NAME = N'testbackup-Full Database Backup', SKIP, NOUNLOAD, STATS = 10 GO执行 SQL 脚本后,可能会收到 错误消息 3098 和 3013。
示例 5:具有未压缩设置的非压缩备份和 NT 备份可以共存:
在不初始化的情况下将数据库
test备份到同一媒体集,且不进行压缩:--The backups ( NT and non-compressed backup) can co-exist BACKUP DATABASE test TO TAPE = N'\\.\Tape0' WITH NAME = N'testbackup-Full Database Backup', SKIP, NOUNLOAD, STATS = 10 GO验证备份标头,并查看 SQL 和 NT 备份:
RESTORE HEADERONLY FROM TAPE = N'\\.\Tape0'使用 NT 备份在磁带上强制压缩备份:
BACKUP DATABASE test TO TAPE = N'\\.\Tape0' WITH COMPRESSION, NAME = N'testbackup1 Full Database Backup', SKIP, NOUNLOAD, STATS = 10 GO执行 SQL 脚本后,可能会收到 错误消息 3098 和 3013。
错误消息 3098 和 3013
错误消息 3098
Msg 3098, Level 16, State 2, Line 1 The backup cannot be performed because 'COMPRESSION' was requested after the media was formatted with an incompatible structure. To append to this media set, either omit 'COMPRESSION' or specify 'NO_COMPRESSION'. Alternatively, you can create a new media set by using WITH FORMAT in your BACKUP statement. If you use WITH FORMAT on an existing media set, all its backup sets will be overwritten.错误消息 3013
Msg 3013, Level 16, State 1, Line 1 BACKUP DATABASE is terminating abnormally.