日期:2014-05-18  浏览次数:20536 次

如何简单的恢复日志文件
我用提sqlserver   2005   ,备份方案是   一天做一次完整备份,5分钟做一次日志备份,现在的主要问题是:   如果数据库出现问题,怎样才能尽快的进行恢复。

  因为日志文件有上百个,如果手工进行恢复,要费很多时间,希望能提够一个进行恢复的脚本或方法!

------解决方案--------------------
declare @db_name sysname--数据库名
declare @logfile_path varchar(200)--日志路径
declare @b varchar(40)--前缀
declare @e varchar(40)--后缀
declare @m varchar(2000)
declare @begin varchar(40)--开始字串
declare @end varchar(40)--结束字串
declare @p int--备份间隔 分钟
declare @begin_date datetime
declare @end_date datetime


select @db_name = 't1 '
select @logfile_path= 'E:\SQLSERVER_DATA\MSSQL\BACKUP '
select @p = 5
select @b = 't1_tlog_ '
select @e = '.TRN '
select @begin = '200708141047 '
select @end = '200708141137 '
-- t1_tlog_200708141047.TRN
select @m = @b+@begin+@e
-- print @m
select @begin_date = substring(@begin,1,4)+ '- '+substring(@begin,5,2)+ '- '+substring(@begin,7,2)+ ' '+substring(@begin,9,2)+ ': '+substring(@begin,11,2)+ ':00 '
select @end_date = substring(@end,1,4)+ '- '+substring(@end,5,2)+ '- '+substring(@end,7,2)+ ' '+substring(@end,9,2)+ ': '+substring(@end,11,2)+ ':00 '
while(@begin_date < @end_date)
begin
-- print @begin_date
select @begin_date = dateadd(mi,@p,@begin_date)
if @begin_date < @end_date
select @m = ' restore log '+@db_name+ ' from disk= ' ' '+@logfile_path+ '\ '+@b+left(replace(replace(replace(convert(varchar(20),@begin_date,120), '- ', ' '), ': ', ' '), ' ', ' '),12)+@e+ ' ' ' ' + ' WITH NORECOVERY '
else if @begin_date = @end_date
select @m = ' restore log '+@db_name+ ' from disk= ' ' '+@logfile_path+ '\ '+@b+left(replace(replace(replace(convert(varchar(20),@begin_date,120), '- ', ' '), ': ', ' '), ' ', ' '),12)+@e+ ' ' ' ' + ' WITH RECOVERY '
print @m

end
结果
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141052.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141057.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141102.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141107.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141112.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141117.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141122.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141127.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141132.TRN ' WITH NORECOVERY
restore log t1 from disk= 'E:\SQLSERVER_DATA\MSSQL\BACKUP\t1_tlog_200708141137.TRN ' WITH RECOVERY