给你一个例子看看[故障起因:]
在使用数据导入导工具将本地表往服务器传输时,忘记点掉“选择全部对象”,因此将远程的140张表超过1000万条数据全部覆盖(操作员当时点完提交就去吃饭,因此中途没有取消),数据库没有备份。[恢复过程:]
使用工具是LogExplore,(下载地址:http://five.ttdown.com/L/Log%20Explorer%20for%20SQL%20Serverv%20v3.21.KG.exe)安装后打开log explorer file=>attach log file->选择服务器和登陆方式->connect->
选择数据库->attach->左面对话框中browse->view log->就可以看到log记录,
点击“View DDL Commands”里面就有很多drop table 命令
点击下面的“undo”按钮是生成表结构的语句(create table ....)
点击下面的“Salvage”按钮是生成插入语句的(insert into ...values....)
(以上lynx1111提供)我是按照上述方法的“Salvage”来生成被删除表的Insert语句,实际上用这个方法生成的SQL脚本已经包含了CreateTable。该过程速度大概用了8个小时,当时觉得慢,后来相比恢复过程,这个速度简直快的不行。最大的表脚本生成后超过1G。生成所有的SQL脚本后,防止万一,我将数据库停下,并把Date文件夹的Log和.MDF文件拷出来(怕破坏LOG文件,没有使用数据库的备份方式备份),文件大小总共为5.7G此后开始进行正式的恢复工作。新建一个数据库,先试着用SQL查询分析器运行了一个小表的脚本,完全没有问题。但后来发现导入比较大的SQL脚本文件,查询分析器就报错了。请教了realgz得知logExplorer本身对大脚本有良好支持,因此改用LogExplorer--》Run SQL Script 功能来运行脚本。果然大文件也可以恢复了。但开始运行后发现包含有ntext字段的表恢复起来异常缓慢,打开一个包含nText字段的表的恢复脚本发现里面使用writeText来写入数据。恢复一个30万数据的表居然用了将近12小时的时间,而数据库中又有大量这样的表,为了加快数据,我又在几个机器上装了LogExplorer加入恢复过程,终于经过3天的时间,全部的表都搞的差不多了,不过恢复过程有少量的错误。接下来我将几个机器的表导到同一个数据库中,不过此时恢复的表是没有包含索引、标识等扩展属性的,因此需要重新建立索引、标识、默认值以及触发器。在建立主键的时候发现居然有数据重复没办法只好删除重复数据。使用 select distinct * into t_New from t_Old 可以删除重复数据,但遇到有ntext字段的表是不能用这个方法的,最后只好用 Delete From t_Table Where ID IN (Select ID From t_Table a where (Select Count(*) From t_Table a where a.ID = ID ) > 1 )直接删除了有重复数据的记录经过72小时的努力,99.9%的数据恢复。并于4月8日晚上恢复运行网站。
在使用数据导入导工具将本地表往服务器传输时,忘记点掉“选择全部对象”,因此将远程的140张表超过1000万条数据全部覆盖(操作员当时点完提交就去吃饭,因此中途没有取消),数据库没有备份。[恢复过程:]
使用工具是LogExplore,(下载地址:http://five.ttdown.com/L/Log%20Explorer%20for%20SQL%20Serverv%20v3.21.KG.exe)安装后打开log explorer file=>attach log file->选择服务器和登陆方式->connect->
选择数据库->attach->左面对话框中browse->view log->就可以看到log记录,
点击“View DDL Commands”里面就有很多drop table 命令
点击下面的“undo”按钮是生成表结构的语句(create table ....)
点击下面的“Salvage”按钮是生成插入语句的(insert into ...values....)
(以上lynx1111提供)我是按照上述方法的“Salvage”来生成被删除表的Insert语句,实际上用这个方法生成的SQL脚本已经包含了CreateTable。该过程速度大概用了8个小时,当时觉得慢,后来相比恢复过程,这个速度简直快的不行。最大的表脚本生成后超过1G。生成所有的SQL脚本后,防止万一,我将数据库停下,并把Date文件夹的Log和.MDF文件拷出来(怕破坏LOG文件,没有使用数据库的备份方式备份),文件大小总共为5.7G此后开始进行正式的恢复工作。新建一个数据库,先试着用SQL查询分析器运行了一个小表的脚本,完全没有问题。但后来发现导入比较大的SQL脚本文件,查询分析器就报错了。请教了realgz得知logExplorer本身对大脚本有良好支持,因此改用LogExplorer--》Run SQL Script 功能来运行脚本。果然大文件也可以恢复了。但开始运行后发现包含有ntext字段的表恢复起来异常缓慢,打开一个包含nText字段的表的恢复脚本发现里面使用writeText来写入数据。恢复一个30万数据的表居然用了将近12小时的时间,而数据库中又有大量这样的表,为了加快数据,我又在几个机器上装了LogExplorer加入恢复过程,终于经过3天的时间,全部的表都搞的差不多了,不过恢复过程有少量的错误。接下来我将几个机器的表导到同一个数据库中,不过此时恢复的表是没有包含索引、标识等扩展属性的,因此需要重新建立索引、标识、默认值以及触发器。在建立主键的时候发现居然有数据重复没办法只好删除重复数据。使用 select distinct * into t_New from t_Old 可以删除重复数据,但遇到有ntext字段的表是不能用这个方法的,最后只好用 Delete From t_Table Where ID IN (Select ID From t_Table a where (Select Count(*) From t_Table a where a.ID = ID ) > 1 )直接删除了有重复数据的记录经过72小时的努力,99.9%的数据恢复。并于4月8日晚上恢复运行网站。
create proc proc_insert (@tablename varchar(256))
as
begin
set nocount on
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert '+@tablename
select @sqlstr1=''
select @sqlstr2=' ('
select @sqlstr1= ' values ( ''+'
select @sqlstr1=@sqlstr1+col+'+'',''+' ,@sqlstr2=@sqlstr2+name +',' from (select case
-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'
-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as col,a.colid,a.name
from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36
)t order by colid
select @sqlstr=@sqlstr+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '+@tablename
-- print @sqlstr
exec( @sqlstr)
set nocount off
end