Sql-Server

恢復大量事務日誌

  • July 17, 2015

我正在使用 SQL Server 2008 R2,我需要將 36 個 .trn 日誌還原到數據庫。我已經使用 SSMS 中的還原事務日誌功能還原了前 4 個(右鍵點擊數據庫 > 任務 > 還原 > 事務日誌)。

有沒有辦法使用這個工具或腳本來按順序恢復它們?.trn 文件位於遠端伺服器上的文件夾中。我不需要恢復文件夾中的所有文件,只需要恢復特定日期之間的文件。

我正在尋找一種自動化的方法,這樣我就不必手動恢復每個單獨的日誌。

我使用如下地址訪問文件。

\遠端伺服器 IP\SQL_Backup\LMSQL02\TLog\file.trn

我建議使用Restore Gene:Automating SQL Server Database Restores - Paul Brewer編寫

如果需要,Restore Gene 儲存過程會生成所需的恢復腳本,包括 DBCC CHECKDB 命令。它本身就是一個有用的工具;在災難恢復的情況下,它可以在幾秒鐘內建構一個恢復腳本。它可選地包括目標數據庫名稱的覆蓋選項和數據、日誌文件位置的 WITH MOVE 覆蓋,這可能有助於通過將數據庫的臨時版本恢復到同一實例來幫助恢復失去的數據。

它具有用於自動化的TSQLPowerShell版本。

這使用 xp_cmdshell 查看日誌文件的文件系統。如果你沒問題的話……

DECLARE @databaseName AS VARCHAR(100)
DECLARE @restoreName AS VARCHAR(100)
SET @databaseName = 'OriginalDBName' 
SET @restoreName = 'RestoredDBName' 

CREATE TABLE #dirList (
 id int identity(1,1),
 line nvarchar(1000)
 )

DECLARE @cmdShellStatement VARCHAR(2000)
--Modify this file path if the backup files are not located in the standard backup directory.
SET @cmdShellStatement = 'DIR "C:\Program'+' '+'Files\Microsoft'+' '+'SQL'+'     '+'Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\' + @databaseName + '\"'
INSERT INTO #dirList (line) EXEC master..xp_cmdshell @cmdShellStatement
SELECT SUBSTRING(line,0,21) AS 'DateTime'
--Modify this file path if the backup files are not located in the standard backup directory.
,'RESTORE DATABASE ' + @restoreName + ' FROM DISK = ''C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\' + @databaseName     + '\' +   SUBSTRING(line,40,100) + ''' WITH REPLACE, NORECOVERY' as 'Script'
FROM #dirList
WHERE line like '%.bak'
ORDER BY CAST(SUBSTRING(line,0,21) AS DateTime)

SELECT SUBSTRING(line,0,21) AS 'DateTime'
--Modify this file path if the backup files are not located in the standard backup directory.
,'RESTORE LOG ' + @restoreName + ' FROM DISK = ''C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\' + @databaseName + '\' +   SUBSTRING(line,40,100) + ''' WITH NORECOVERY' as 'Script'
FROM #dirList
WHERE line like '%.trn'
ORDER BY CAST(SUBSTRING(line,0,21) AS DateTime)

DROP TABLE #dirList

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