當您將備份附加至現有的媒體集時,壓縮備份的行為

本文介紹將備份附加至現有媒體集時壓縮備份的行為。

原始產品版本:SQL Server
原始 KB 編號: 2297053

摘要

壓縮備份的主要限制之一是壓縮和未壓縮的備份無法並存於媒體集中。 這項限制記載於 備份壓縮 (SQL Server) 中。

本文補充檔,並提供與伺服器組態選項 - 壓縮備份預期行為的詳細資訊。

徵兆

請參考下列案例:

在此案例中,您會注意到備份成功,但最終可能會處於與預期不同的壓縮狀態。

其他相關資訊

當您將備份附加至現有的媒體集時,備份會從媒體集繼承壓縮設定。

建立媒體集時,此媒體集的壓縮設定資訊會寫入媒體頭檔。

只有在這些備份的壓縮設定與媒體集的壓縮設定相同時,才會共存到現有媒體集的備份。 下列三個因素會影響壓縮備份的行為:

下表摘要說明根據上述三個因素,壓縮備份的行為:

Backup 語句 新媒體集 附加至具有壓縮備份的現有媒體集 附加至具有未壓縮備份的現有媒體集
語句層級子句 WITH COMPRESSION 備份成功且將會壓縮 成功 錯誤
語句層級子句 WITH NO_COMPRESSION 備份成功且將會取消壓縮 錯誤 成功
沒有語句層級壓縮子句的備份語句 備份成功,壓縮取決於系統預存程序的選項backup compression defaultsp_configure 備份成功且將會壓縮 備份成功且將會取消壓縮

如上表所示,當您在伺服器上使用 選項 backup compression default 並將壓縮備份附加至現有的媒體集時,備份永遠不會因為壓縮設定不符而失敗。 它可運作,但會繼承媒體集標頭中的設定。 不過,如果您在備份語句中指定 子句 WITH COMPRESSIONWITH NO_COMPRESSION ,則會在媒體集中儲存的備份與目前備份之間,就壓縮設定而言,引發錯誤。

注意

您可以在 SQL Server Management Studio 中執行系統預存程式backup compression default,以找到選項sp_configure的目前設定。 如果您要將壓縮備份附加至現有的媒體,您可以使用 RESTORE HEADERONLY 語句來取得標頭資訊。 如需詳細資訊,請參閱範例一節。

範例

以下是一些腳本範例,示範各種案例的行為。 不論備份是磁帶還是磁碟,行為都相同。

  • 範例 1:當選項 backup compression default 的值是 0時,請使用 語句層級子句 WITH COMPRESSION ,將備份附加至具有未壓縮備份設定的現有媒體集:

    1. 檢查壓縮值:

      -- The value of the option "backup compression default" is 0 by default
      sp_configure 'backup compression default'
      
    2. 使用 子句 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
      
    3. 檢查備份和標頭,並查看壓縮的數據行值為 0

      RESTORE HEADERONLY FROM DISK = N'E:\testbackup.bak'
      
    4. 使用 子句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的值設定為 時,將備份附加至相同的媒體集:

    1. 在伺服器層級開啟 backup compression default

      -- The option "backup compression default" as this point is set to 1.
      sp_configure 'backup compression default', 1
      GO
      RECONFIGURE
      GO
      
    2. 將備份附加至相同的媒體集:

      -- 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).
      
    3. 檢查備份和媒體集標頭:

      -- 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 default 的值設定為 0,並將備份附加至具有壓縮備份設定的現有媒體集:

    1. 使用 子句 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
      
    2. 檢查備份和媒體集標頭:

      RESTORE HEADERONLY FROM DISK = N'E:\testbackup.bak'
      
    3. 設定回backup compression default0

      sp_configure 'backup compression default', 0
      GO
      RECONFIGURE
      GO
      
    4. 使用 子句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
      
    5. 檢查備份和媒體集標頭:

      -- 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 備份共存:

    1. 進行 NT 備份並確認備份標頭:

      -- You can see that it is not a SQL backup and the value of compressed is 0
      RESTORE HEADERONLY FROM TAPE = N'\\.\Tape0'
      
    2. 使用 子句testWITH INIT將資料庫WITH COMPRESSION備份到相同的媒體集:

      BACKUP 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 備份可以共存:

    1. 將資料庫 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
      
    2. 確認備份標頭,並查看 SQL 和 NT 備份:

      RESTORE HEADERONLY FROM TAPE = N'\\.\Tape0'
      
    3. 使用 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.