Sql-Server

在啟用 CDC 的情況下進行時間點恢復;可能的?

  • November 30, 2015

本週我發現,數據庫恢復選項NORECOVERY和數據庫恢復選項KEEP_CDC互斥的。所以,這就引出了問題。您如何使用完整備份和日誌備份進行數據庫還原,保持 CDC 完整?

KEEP_CDC對 MSDN 等進行研究。除了指定的單個完整數據庫還原之外,我找不到任何關於使用任何其他選項還原數據庫的文件RECOVERY

我能夠找到一個嘗試完成完整日誌和後續日誌的嘗試,而無需keep_cdc等待最終日誌。RECOVERY直到那時,表才使用andKEEP_CDC選項聯機。結果是損壞的 CDC 架構,如此處所示。

如果打算進行KEEP_CDC還原,您是否真的僅限於完整備份,或者是否有類似於上述嘗試的機制在除原始伺服器之外的伺服器上的多文件還原期間保持完整備份?

KEEP_CDC將數據庫還原到原始伺服器時,更改數據擷取或 CDC 不需要關鍵字。將數據庫恢復到原始伺服器以外的伺服器時,可以使用KEEP_CDC關鍵字指定將更改數據擷取數據保留在數據庫中。這是設計使然,並在 BOL中標題為“恢復或附加啟用更改數據擷取的數據庫”部分下明確說明

此程式碼顯示 CDC 在RESTORE DATABASE ... WITH NORECOVERY

CREATE DATABASE CDC_RESTORE_TEST 
ON (NAME='CDC_RESTORE_TEST', FILENAME = 'C:\SQLServer\Data\CDC_RESTORE_TEST.mdf', SIZE = 10MB, FILEGROWTH = 1MB)
LOG ON (NAME = 'CDC_RESTORE_TEST_LOG', FILENAME = 'C:\SQLServer\Logs\CDC_RESTORE_TEST.ldf', SIZE = 1MB, FILEGROWTH = 1MB);
GO
ALTER DATABASE CDC_RESTORE_TEST
SET RECOVERY FULL;
GO
USE CDC_RESTORE_TEST;
GO
EXEC sys.sp_cdc_enable_db;
GO
CREATE TABLE Test
( 
   ID INT NOT NULL CONSTRAINT PK_Test PRIMARY KEY CLUSTERED IDENTITY(1,1)
   , DATA NVARCHAR(255) NOT NULL
);
EXEC sys.sp_cdc_enable_table 'dbo', 'Test', 'dbo_Test', 1, NULL;
GO
ALTER TABLE Test ADD Data1 NVARCHAR(255) NOT NULL;
WAITFOR DELAY '00:00:15'; /* wait for event to be captured */
EXEC sys.sp_cdc_get_ddl_history 'dbo_Test';

USE Master;
GO
BACKUP DATABASE CDC_RESTORE_TEST TO DISK='C:\SQLServer\CDC_RESTORE_TEST.BAK';
BACKUP LOG CDC_RESTORE_TEST TO DISK='C:\SQLServer\CDC_RESTORE_TEST_LOG.BAK';
GO
ALTER DATABASE CDC_RESTORE_TEST SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
DROP DATABASE CDC_RESTORE_TEST;
GO
RESTORE DATABASE CDC_RESTORE_TEST FROM DISK='C:\SQLServer\CDC_RESTORE_TEST.bak'
WITH NORECOVERY;
RESTORE LOG CDC_RESTORE_TEST FROM DISK='C:\SQLServer\CDC_RESTORE_TEST_LOG.bak'
WITH RECOVERY;
GO
USE CDC_RESTORE_TEST;
GO
EXEC sys.sp_cdc_get_ddl_history 'dbo_Test';

將相同的備份還原到與新數據庫名稱相同的伺服器上似乎可以保留 CDC:

USE Master;
GO
RESTORE DATABASE CDC_RESTORE_TEST1 FROM DISK='C:\SQLServer\CDC_RESTORE_TEST.bak' 
WITH NORECOVERY
   , MOVE 'CDC_RESTORE_TEST' TO 'C:\SQLServer\Data\CDC_Restore_test1.mdf'
   , MOVE 'CDC_RESTORE_TEST_LOG' TO 'C:\SQLServer\Logs\CDC_Restore_test1.ldf';
RESTORE LOG CDC_RESTORE_TEST1 FROM DISK='C:\SQLServer\CDC_RESTORE_TEST_LOG.bak'
WITH RECOVERY
   , MOVE 'CDC_RESTORE_TEST' TO 'C:\SQLServer\Data\CDC_Restore_test1.mdf'
   , MOVE 'CDC_RESTORE_TEST_LOG' TO 'C:\SQLServer\Logs\CDC_Restore_test1.ldf';
GO
USE CDC_RESTORE_TEST;
GO
EXEC sys.sp_cdc_get_ddl_history 'dbo_Test';

引用自:https://dba.stackexchange.com/questions/50986