<%
'乘风多用户计数器 Sql版
'作者QQ：178575
'作者EMail：yliangcf@163.com
'作者网站：http://www.qqcf.com
'详细简介：http://www.qqcf.com/cfcount.htm
'上面有程序在线演示，安装演示，使用疑难解答，最新版本下载等内容
'因为这些内容可能时常更新，就没有放在程序里，请自己上网站上查看
'有完整版本的演示
%>
<%
On Error Resume Next

Action=Request("Action")

If Action<>"" Then '检查文件是否存在
 Set Fs = Server.CreateObject("Scripting.FileSystemObject")
 If Fs.FileExists(server.mappath("Tool_DbRecover.lock")) Then
  Response.write "Tool_DbRecover.lock文件已经存在，操作中止!"
  Response.End
 End If
End If

If Action="dbrecoversave" Then

If Trim(Request("Pwd"))="" Then
 Response.write "数据库超级管理员用户sa的密码不能为空密码！"
 Response.End
Else
 Pwd=Trim(Request("Pwd"))
End If
	
Sql = "use master	"&chr(13)&chr(10)
Sql = Sql & "declare	"&chr(13)&chr(10)
Sql = Sql & "@sql varchar(1000),	"&chr(13)&chr(10)
Sql = Sql & "@spid int,	"&chr(13)&chr(10)
Sql = Sql & "@dbname varchar(50),	"&chr(13)&chr(10)
Sql = Sql & "@dbpath varchar(255),	"&chr(13)&chr(10)
Sql = Sql & "@lqdataname varchar(50),	"&chr(13)&chr(10)
Sql = Sql & "@lqlogname varchar(50),	"&chr(13)&chr(10)
Sql = Sql & "@dbsavepath varchar(255),	"&chr(13)&chr(10)
Sql = Sql & "@datapath varchar(255),	"&chr(13)&chr(10)
Sql = Sql & "@logpath varchar(255),	"&chr(13)&chr(10)
Sql = Sql & "@sqlver varchar(1000)	"&chr(13)&chr(10)

Sql = Sql & "--需要还原的数据库备份的完全路径，以备份文件.bak扩展名结尾，如d:\1.bak		"&chr(13)&chr(10)
Sql = Sql & "set @dbpath = '"&Replace(Request("dbpath"),"'","''")&"'		"&chr(13)&chr(10)

Sql = Sql & "--把备份还原到哪个数据库上，如果数据库不存在则会自动新建		"&chr(13)&chr(10)
Sql = Sql & "set @dbname = '"&Replace(Request("dbname"),"'","''")&"'		"&chr(13)&chr(10)

Sql = Sql & "--还原后数据库文件存放路径,以\结尾，如d:\		"&chr(13)&chr(10)
Sql = Sql & "set @dbsavepath = '"&Replace(Request("dbsavepath"),"'","''")&"'		"&chr(13)&chr(10)


Sql = Sql & "SELECT @sqlver = @@VERSION	"&chr(13)&chr(10)
Sql = Sql & "if charindex('8.00', @sqlver)<>0	"&chr(13)&chr(10)
Sql = Sql & "begin	"&chr(13)&chr(10)
Sql = Sql & " create table #restoreFileList (LogicalName varchar(255), PhysicalName varchar(255),Type varchar(20), FileGroupName varchar(255), Size varchar(20), MaxSize varchar(20) )	"&chr(13)&chr(10)
Sql = Sql & " insert into #restoreFileList execute('restore filelistonly from disk=''' + @dbpath + '''')	"&chr(13)&chr(10)
Sql = Sql & " select @lqdataname=LogicalName from #restoreFileList where Type='D'	"&chr(13)&chr(10)
Sql = Sql & " select @lqlogname=LogicalName from #restoreFileList where Type='L'	"&chr(13)&chr(10)
Sql = Sql & " drop table #restoreFileList	"&chr(13)&chr(10)
Sql = Sql & "end	"&chr(13)&chr(10)
Sql = Sql & "else	"&chr(13)&chr(10)
Sql = Sql & "begin	"&chr(13)&chr(10)
Sql = Sql & " create table #restoreFileList2 (LogicalName varchar(255), PhysicalName varchar(255),Type varchar(20), FileGroupName varchar(255), Size varchar(20), MaxSize varchar(20), Fileld VARCHAR(20), CreateLSN VARCHAR(20), DropLSN VARCHAR(20), Uniqueld uniqueidentifier, ReadOnlyLSN VARCHAR(20), ReadWriteLSN VARCHAR(20), BackupSizeInBytes VARCHAR(255), SourceBlockSize VARCHAR(20), FileGroupld VARCHAR(20), LogGroupGUID VARCHAR(20), DifferentialBaseLSN VARCHAR(20), DifferentialBaseGUID uniqueidentifier, IsReadOnly VARCHAR(20), IsPresent VARCHAR(20))	"&chr(13)&chr(10)
Sql = Sql & " insert into #restoreFileList2 execute('restore filelistonly from disk=''' + @dbpath + '''')	"&chr(13)&chr(10)
Sql = Sql & " select @lqdataname=LogicalName from #restoreFileList2 where Type='D'	"&chr(13)&chr(10)
Sql = Sql & " select @lqlogname=LogicalName from #restoreFileList2 where Type='L'	"&chr(13)&chr(10)
Sql = Sql & " drop table #restoreFileList2	"&chr(13)&chr(10)
Sql = Sql & "End	"&chr(13)&chr(10)

Sql = Sql & "set @datapath = @dbsavepath + @dbname + '_data.mdf'		"&chr(13)&chr(10)
Sql = Sql & "set @logpath = @dbsavepath + @dbname + '_log.ldf'		"&chr(13)&chr(10)

Sql = Sql & "set @sql='declare getspid cursor for select spid from sysprocesses where dbid=db_id('''+@dbname+''')'		"&chr(13)&chr(10)
Sql = Sql & "exec (@sql)		"&chr(13)&chr(10)
Sql = Sql & "open getspid		"&chr(13)&chr(10)
Sql = Sql & "fetch next from getspid into @spid		"&chr(13)&chr(10)
Sql = Sql & "while @@fetch_status <> -1		"&chr(13)&chr(10)
Sql = Sql & "begin		"&chr(13)&chr(10)
Sql = Sql & "exec('kill '+@spid)		"&chr(13)&chr(10)
Sql = Sql & "fetch next from getspid into @spid		"&chr(13)&chr(10)
Sql = Sql & "end		"&chr(13)&chr(10)
Sql = Sql & "close getspid		"&chr(13)&chr(10)
Sql = Sql & "deallocate getspid		"&chr(13)&chr(10)

Sql = Sql & "restore   database   @dbname		"&chr(13)&chr(10)
Sql = Sql & "from   disk = @dbpath		"&chr(13)&chr(10)
Sql = Sql & "WITH REPLACE		"&chr(13)&chr(10)
Sql = Sql & ",move @lqdataname   to  @datapath		"&chr(13)&chr(10)
Sql = Sql & ",move   @lqlogname   to   @logpath		"&chr(13)&chr(10)
 
 
DbAddress="YANLIANG\MSSQL2005"	 	  '请输入Sqlserver服务器的IP地址，数据库和推广系统在同一台服务器上只要填写localhost就可以
DbUser="sa"           		  '请输入连接Sqlserver服务器的用户名
DbPassword=Pwd     '请输入连接Sqlserver服务器的密码
DbName="master"           '请输入Sqlserver服务器的数据库名称
DbPort="1433"             '默认为1433,一般此处不需要更改



On Error Resume Next
If DbPort="1433" Then
 DbPort=""
Else
 DbPort=","&DbPort
End If

ConnStr="Provider = Sqloledb;Data Source ="&DbAddress&DbPort&";User ID ="&DbUser&";Password ="&DbPassword&";Initial Catalog ="&DbName&";"
Set Conn = Server.CreateObject ("Adodb.Connection")
Conn.Open ConnStr
If Err Then
 err.Clear
 Set Conn = Nothing
 Response.Write "数据库连接出错，请检查连接字串。"
 Response.End
End If

Conn.Execute Sql

 Set Fso = Server.CreateObject("Scripting.FileSystemObject")
 
 MyStr = ""
 Set Fout = Fso.Createtextfile(server.mappath("Tool_DbRecover.lock"),true)
 Fout.WriteLine MyStr
 
 Response.write "<span style='font-size:12px'>数据库还原成功，请设置好conn.asp和web.config文件里连接数据库的参数，关闭本页面或返回<a href='?'>修复页面首页</a></span>"
 Response.End
End If


If Action="adminpwdlockfilemodifysave" Then

 Set Fso = Server.CreateObject("Scripting.FileSystemObject")
 
 MyStr = ""
 Set Fout = Fso.Createtextfile(server.mappath("Tool_DbRecover.lock"),true)
 Fout.WriteLine MyStr
 
 Response.write "<span style='font-size:12px'>还原锁定文件生成成功，请关闭本页面或返回<a href='?'>修复页面首页</a></span>"
 Response.End
End If
%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<html><head><title>数据库在线还原</title>
<style type="text/css">
body {text-align: left; font-family:Arial; margin:0; padding:0; background: #FFF; font-size:12px; color:#333333;}
table{font-size:12px;}

.tb_1{
 background-color:#ffffff;
 border:1px solid #C9DDF0;
 margin:5px 0px 5px 5px;
 width:768px;
 float:left;
}

 .tb_2{
 width:980px;
 background-color:#ffffff;
 border:1px solid #C9DDF0;
 margin:15px auto;
 clear:both;
}

 .tb_3{
 background-color:#ffffff;
 border:1px solid #C9DDF0;
 margin:15px auto;
 padding:5px;
 clear:both;
}

.tb_4{
 border:0px solid #C9DDF0;
 margin:5px 0px 5px 5px;
 border-collapse:collapse;
 font-size:12px;
 text-align:left;
 width:530px;
}

.tb_4 td{border-bottom: 1px dotted #C9DDF0;padding-left:6px;empty-cells:show;}


.tr_1{
 padding-left:5px;
 padding-top:5px;
 font-weight:bold;
 font-size:14px;
 height:24px;
 text-align:center;
 background-color:#F3F9FE;
}

.tr_2{
 text-align:center;
}



.td_1{
 text-align:left;
}
.td_2{
 text-align:center;
}
.td_3{
 text-align:right;
}

.right{
	display:inline;
	float:right
}
.clear{
	clear:both;
	height:0px;
}
.wrap{
	width:950px;;
	margin:0 auto;
}
.bord{
	border:#b0bec7 1px solid;
}


.t_button
{
	display: inline-table;
	display: -moz-inline-box;
	display: inline-block;
	margin: 1px;
	border-style: solid;
	border-width: 1px;
	border-color: #999999;
	border-top-color: #cccccc;
	border-left-color: #cccccc;
	background-color: #eeeeee;
	color: #333333;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 100%;
	white-space: nowrap;
	height:21px;
	line-height:21px;
	padding:0 3px;
}
.t_button:LINK
{
	text-decoration: none;
	color: #333333;
	background-color: #eeeeee;
}

.t_button:VISITED
{
	text-decoration: none;
	color: #333333;
	background-color: #eeeeee;
}
</style>
</HEAD>
<body>
<table class="tb_3" width="600">
          <tr class="tr_1"> 
            <td >数据库在线还原工具</td>
          </tr>
          
            <tr>
              <td style="margin:5px;font-size:12px;text-align:center">
<%
 '检查文件是否存在
Set Fs = Server.CreateObject("Scripting.FileSystemObject")
If Fs.FileExists(server.mappath("Tool_DbRecover.lock")) Then
%>
Tool_DbRecover.lock文件存在，无法进行数据库新安装或还原<br><br>
请删除此文件后刷新本页面即可进行数据库还原工作<br><br>
操作完成后，会重新生成Tool_DbRecover.lock文件，每次操作前必须删除此文件<br><br>
<a href="#" class="t_button" onClick="location.href='?RanStr=' + Math.random()">刷新本页面</a>
<%
Else
%>
锁定文件Tool_DbRecover.lock不存在，点击下面修复按钮即可进行数据库还原工作<br><br>
操作后，会重新生成Tool_DbRecover.lock文件<br><br>
<table class="tb_4">
<form name="form_dbrecover" id="form_dbrecover" method="post" action="?Action=dbrecoversave">
  <tr>
    <td><div align="center"><strong>系统Data目录里的数据库</strong></div></td>
  </tr>
  <tr>
    <td>

<%
foldername=server.mappath("data")
path=foldername
If Instr(path,"\data")=0 Then Call AlertBack("不能浏览这个目录！",1)
ShowFolderList(path) 



Function ShowFolderList(folderspec) 
temp=request.ServerVariables("HTTP_REFERER") 
temp=left(temp,Instrrev(temp,"/")) 
temp1=len(folderspec)-len(server.MapPath("./"))-1 
if temp1>0 then 
temp1=right(folderspec,cint(temp1)) 
elseif temp1=-1 then 
temp1="" 
end if 
tempurl=temp+replace(temp1,"\","/")+"/" 
Set fso = CreateObject("Scripting.FileSystemObject") 
upfolderspec=fso.GetParentfoldername(folderspec&"\") 
%>
<table style="width:100%">
                  <tr> 
                    <td>名称</td>
                    
          <td>大小</td>
                    
          <td>类型</td>
                    <td><div>修改时间</td>
                  </tr>
                  <% 
'列出目录 
Set f = fso.GetFolder(folderspec) 
Set fc = f.SubFolders 
For Each f1 in fc 
%>
                  <tr> 
                    <td><%= f1.name%></td>
                    <td> 
                        <%MySize=f1.size/1024
	  If MySize<1024 Then Response.write FormatNumber(MySize,2,-1,0,0)&"K"
	  If MySize>=1024 Then Response.write FormatNumber(MySize/1024,2,-1,0,0)&"M"%>                    </td>
                    <td>文件夹</td>
                    <td><%= f1.datelastmodified%></td>
                  </tr>
                  <% 
Next 
foldername=StrReverse(left(StrReverse(folderspec),instr(StrReverse(folderspec),"\")-1))&"/"
'列出文件 
Set fc = f.Files 
For Each f1 in fc 
%>
                  <tr> 
                    <td><%= f1.name%>&nbsp;&nbsp;<%If Instr(Lcase(f1.name),".bak")>0 Then%>[<a href="?dbname=<%= f1.name%>&dbpath=<%= path&"\"&f1.name%>&dbsavepath=<%= path&"\"%>" onClick="return confirm('确定要还原这个备份的数据库吗？确定后还需要填写下面的数据库名和sa数据库用户的密码')">还原数据库</a>]<%End If%></td>
                    <td>
                        <%MySize=f1.size/1024
	  If MySize<1024 Then Response.write  FormatNumber(MySize,2,-1,0,0)&"K"
	  If MySize>=1024 Then Response.write  FormatNumber(MySize/1024,2,-1,0,0)&"M"%>                    </td>
                    <td>文件</td>
                    <td><%= f1.datelastmodified%></td>
                  </tr>
                  <% 
Next 
set fso=nothing 
%>
        </table>
<%End Function %>		</td>
  </tr>
  <tr>
    <td><div align="center"><strong>在线还原数据库</strong></div></td>
  </tr>
  <tr>
    <td>需要还原的备份文件路径(以备份文件.bak扩展名结尾，如d:\1.bak)： 
      <input name="dbpath" type="text" id="dbpath" value="<%=Request("dbpath")%>" size="66">
      </td>
    </tr>
  <tr>
    <td>把备份还原到哪个数据库上(如果数据库不存在则会自动新建)：
      <input name="dbname" type="text" id="dbname" size="66">
      </td>
    </tr>
  <tr>
    <td>还原后数据库文件存放路径(以\结尾，如d:\)：
      <input name="dbsavepath" type="text" id="dbsavepath" value="<%=Request("dbsavepath")%>" size="66">
      </td>
    </tr>
  <tr>
    <td>数据库超级用户密码(指sa用户的密码，不能为空)：
      <input name="pwd" type="text" id="pwd" size="66">
      </td>
    </tr>
  <tr>
    <td><div align="center"><a href="#" class="t_button"  onclick="{if(confirm('确定要还原数据库吗?')){document.getElementById('form_dbrecover').submit();return true;}return false;};">点击还原,并生成Tool_DbRecover.lock还原锁定文件</a></div></td>
    </tr>
          </form>
  <tr>
    <td style="padding-top:20px"><div align="center"><strong>不安装或还原数据库</strong></div></td>
    </tr>
  <tr>
    <td><div align="center"><a href="?Action=adminpwdlockfilemodifysave" class="t_button"  onClick="return confirm('确定要不修复模板直接生成Tool_DbRecover.lock模板修复锁定文件吗?')">不还原数据库,直接生成Tool_DbRecover.lock还原锁定文件</a></div></td>
    </tr>
</table>


<br>
<br>


<%
End If
%>
 </td>
            </tr>

</table>  
