sql 生成TLog还原脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 生成TLog还原脚本相关的知识,希望对你有一定的参考价值。

USE Master; 
GO  
SET NOCOUNT ON 

-- 1 - Variable declaration 
DECLARE @dbName sysname 
DECLARE @backupPath NVARCHAR(500) 
DECLARE @cmd NVARCHAR(500) 
DECLARE @fileList TABLE (backupFile NVARCHAR(255)) 
DECLARE @lastFullBackup NVARCHAR(500) 
DECLARE @lastDiffBackup NVARCHAR(500) 
DECLARE @backupFile NVARCHAR(500) 

-- 2 - Initialize variables 
SET @dbName = 'Thirtyone_Special' 
SET @backupPath = 'G:\mssql\PREPROD_Data1\Backup\Thirtyone_Special_23\LOG\' 

-- 3 - get list of files 
SET @cmd = 'DIR /b ' + @backupPath 

INSERT INTO @fileList(backupFile) 
EXEC master.sys.xp_cmdshell @cmd 

--SELECT * FROM  @fileList

---- 4 - Find latest full backup 
--SELECT @lastFullBackup = MAX(backupFile)  
--FROM @fileList  
--WHERE backupFile LIKE '%.BAK'  
--   AND backupFile LIKE @dbName + '%' 

--SET @cmd = 'RESTORE DATABASE ' + @dbName + ' FROM DISK = '''  
--       + @backupPath + @lastFullBackup + ''' WITH NORECOVERY, REPLACE' 
--PRINT @cmd 

---- 4 - Find latest diff backup 
--SELECT @lastDiffBackup = MAX(backupFile)  
--FROM @fileList  
--WHERE backupFile LIKE '%.DIF'  
--   AND backupFile LIKE @dbName + '%' 
--   AND backupFile > @lastFullBackup 

---- check to make sure there is a diff backup 
--IF @lastDiffBackup IS NOT NULL 
--BEGIN 
--   SET @cmd = 'RESTORE DATABASE ' + @dbName + ' FROM DISK = '''  
--       + @backupPath + @lastDiffBackup + ''' WITH NORECOVERY' 
--   PRINT @cmd 
--   SET @lastFullBackup = @lastDiffBackup 
--END 

-- 5 - check for log backups 
DECLARE backupFiles CURSOR FOR  
   SELECT backupFile  
   FROM @fileList 
   WHERE backupFile LIKE '%.TRN'  
   --AND backupFile LIKE @dbName + '%' 
   --AND backupFile > @lastFullBackup 

OPEN backupFiles  

-- Loop through all the files for the database  
FETCH NEXT FROM backupFiles INTO @backupFile  

WHILE @@FETCH_STATUS = 0  
BEGIN  
   SET @cmd = 'RESTORE LOG ' + @dbName + ' FROM DISK = '''  
       + @backupPath + @backupFile + ''' WITH NORECOVERY' 
   PRINT @cmd 
   FETCH NEXT FROM backupFiles INTO @backupFile  
END 

CLOSE backupFiles  
DEALLOCATE backupFiles  

-- 6 - put database in a useable state 
SET @cmd = 'RESTORE DATABASE ' + @dbName + ' WITH RECOVERY' 
PRINT @cmd 




以上是关于sql 生成TLog还原脚本的主要内容,如果未能解决你的问题,请参考以下文章

sql 收集旧服务器实例中的所有DB并生成还原脚本。我在移动数据库时使用了这个

sql tlog_backup.sql

SQL脚本如何还原成SQL数据库呢?

mysql数据库备份和还原

sql server2008备份出来的数据库如何在在sql server 2005或2000中还原

为什么MSBuild会将.tlog文件生成到CMakeFiles / CompilerIdC中,如何让它停止?