<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="admin.asp"-->
<!--#include file="../inc/Function.asp"-->
<!--#include file="../inc/conn.asp"-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数据备份</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>

<body id="page">
<h2 class="title">网站信息</h2>

<table width="99%" border="0" align="center" class="line_table">
  <tr>
    <td height="20">&nbsp;网站后台管理 &gt;&gt; <a href="cw_data.asp">数据库管理</a></td>
  </tr>
</table>
<%
dim Action,FoundErr,ErrMsg,PowerFlag
Action=trim(request("Action"))
dim ObjInstalled
dbpath=server.mappath(dbroot)
ObjInstalled=IsObjInstalled("Scripting.FileSystemObject")
%>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="1">
      <tr>
        <td ><table width="99%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td height="30">
                <input type="button" name="Submit" value="备份数据" onclick="location.href='?Action=Backup'" class="btn" />
                &nbsp;&nbsp;
                  <input type="button" name="Submit2" value="恢复数据库" onclick="location.href='?Action=Restore'" class="btn" />
                &nbsp;&nbsp;
                  <input type="button" name="Submit3" value="压缩数据库" onclick="location.href='?Action=Compact'" class="btn"/>
                  </a>
                  <%	If ObjInstalled=false Then
		Response.Write "<b><font color=red>你的服务器不支持 FSO(Scripting.FileSystemObject)! 不能使用本功能</font></b>"
	end if%></td>
            </tr>
            <tr>
              <td height="30">管理员必读：因为ACCESS在数据量多时会出现占空间的现象，所以先备份数据库，再压缩数据库，以减小空间和提高数据库运行的效率，如果出错则恢复数据库。</td>
            </tr>
        </table></td>
      </tr>
      <tr>
        <td  height="30" style="color:#FF0000;"><%
 if request("Action")="BackupData" then
 call BackupData()
 else
 if request("Action")="RestoreData" then
 call RestoreData()
 else
 if request("Action")="CompactData" then
 call CompactData()
 else
 response.Write"请谨慎操作!注意相关说明!"
 end if
 end if
 end if
 %></td>
      </tr>
      <%if request("Action")="Backup" then  %>
      <tr>
        <td ><form id="form1" name="form1" method="post" action="?action=BackupData">
            <table width="99%" border="0" cellspacing="0">
              <tr>
                <td width="200" height="33" align="right">备份目录：</td>
                <td><input type="text" size="30" name="bkfolder" value="Databackup" readonly="true" /></td>
                <td>相对路径目录，如目录不存在，将自动创建</td>
              </tr>
              <tr>
                <td width="200" height="34" align="right">备份名称：</td>
                <td height="34"><input type="text" size="30" name="bkDBname" value="databack" readonly="true" /></td>
                <td height="34">不用输入文件名后缀（默认为“.asa”）。如有同名文件，将覆盖</td>
              </tr>
              <tr>
                <td height="34" align="right">&nbsp;</td>
                <td height="34"><input name="submit3" type="submit" value=" 开始备份数据" <%If ObjInstalled=false Then response.Write "disabled"%> readonly="true" class="btn" onmouseover="this.className='btn_over'" onmouseout="this.className='btn_out'" /></td>
                <td height="34">&nbsp;</td>
              </tr>
            </table>
        </form></td>
      </tr>
      <%end if%>
      <%if request("Action")="Restore" then%>
      <tr>
        <td ><form id="form2" name="form2" method="post" action="?action=RestoreData">
            <table width="99%" border="0" cellpadding="0" cellspacing="0">
              <tr>
                <td width="200" height="30" align="right">备份数据库路径（相对）：</td>
                <td height="30"><input name="backpath" type="text" id="backpath" value="DataBackup/databack.asa" size="50" maxlength="200" readonly="true" /></td>
              </tr>
              <tr>
                <td height="30">&nbsp;</td>
                <td><input name="submit" type="submit" value="开始恢复数据 " <%If ObjInstalled=false Then response.Write "disabled"%> class="btn" onmouseover="this.className='btn_over'" onmouseout="this.className='btn_out'" /></td>
              </tr>
            </table>
        </form></td>
      </tr>
      <%end if%>
      <%if request("Action")="Compact" then%>
      <tr>
        <td><form id="form3" name="form3" method="post" action="?action=CompactData">
            <table width="99%" border="0" cellpadding="0" cellspacing="0">
              <tr>

                <td width="200" height="30">&nbsp;</td>
                <td>压缩前，建议先备份数据库，以免发生意外错误。 </td>
              </tr>
              <tr>
                <td height="30">&nbsp;</td>
                <td><input name="submit2" type="submit" value="开始压缩数据" <%If ObjInstalled=false Then response.Write "disabled"%> class="btn" onmouseover="this.className='btn_over'" onmouseout="this.className='btn_out'" /></td>
              </tr>
            </table>
        </form></td>
      </tr>
      <%end if%>
    </table>
</body>
</html>
<%
sub BackupData()
	dim bkfolder,bkdbname,fso
	bkfolder=trim(request("bkfolder"))
	bkdbname=trim(request("bkdbname"))
	if bkfolder="" then
		FoundErr=True
		ErrMsg=ErrMsg & "请指定备份目录！"
	end if
	if bkdbname="" then
		FoundErr=True
		ErrMsg=ErrMsg & "请指定备份文件名"
	end if
	if FoundErr=True then exit sub
	bkfolder=server.MapPath(bkfolder)
	Set Fso=server.createobject("scripting.filesystemobject")
	if fso.FileExists(dbpath) then
		If fso.FolderExists(bkfolder)=false Then
			fso.CreateFolder(bkfolder)
		end if
		fso.copyfile dbpath,bkfolder & "\" & bkdbname & ".asa"			
		response.write "备份数据库成功，备份的数据库为 " & bkfolder & "\" & bkdbname & ".asa"
	Else
		response.write "找不到源数据库文件。"
	End if
end sub

sub CompactData()
	Dim fso, Engine, strDBPath
	strDBPath = left(dbPath,instrrev(DBPath,"\"))
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	If fso.FileExists(dbPath) Then
		Set Engine = CreateObject("JRO.JetEngine")
		Engine.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & dbpath," Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb"
		fso.CopyFile strDBPath & "temp.mdb",dbpath
		fso.DeleteFile(strDBPath & "temp.mdb")
		Set fso = nothing
		Set Engine = nothing
		response.write "数据库压缩成功!"
	Else
		response.write "数据库没有找到!"
	End If
end sub

sub RestoreData()
	dim backpath,fso
	backpath=request.form("backpath")
	if backpath="" then
		FoundErr=True
		ErrMsg=ErrMsg & "请指定原备份的数据库文件名！"
		exit sub	
	end if
	backpath=server.mappath(backpath)
	Set Fso=server.createobject("scripting.filesystemobject")
	if fso.fileexists(backpath) then  					
		fso.copyfile Backpath,Dbpath
		response.write "成功恢复数据！"
	else
		response.write "找不到指定的备份文件！"
	end if
end sub

'**************************************************
'函数名：IsObjInstalled
'作  用：检查组件是否已经安装
'参  数：strClassString ----组件名
'返回值：True  ----已经安装
'        False ----没有安装
'**************************************************
Function IsObjInstalled(strClassString)
	On Error Resume Next
	IsObjInstalled = False
	Err = 0
	Dim xTestObj
	Set xTestObj = Server.CreateObject(strClassString)
	If 0 = Err Then IsObjInstalled = True
	Set xTestObj = Nothing
	Err = 0
End Function
%>