<%@ Language=VBScript %>
<!--#include virtual="/inc-freehost-function/FreeHostFunction.asp"-->
<!--#include virtual="/inc-freehost-function/FreeHostFunctionMain.asp"-->
<%
server.ScriptTimeout =2400
If session("SqlAUID")="" Then
	ef=True
	ec="授权非法，有可能是超时造成的。"
ElseIf Request("id")="" Then
	ef=True
	ec="请输入产品ID！"
ElseIf Instr(1, Request("id"), "'", 1)>0 then
	ef=True
	ec="非法产品ID！"
ElseIf not IsNumeric(Request("id"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf not IsNumeric(Request("backupid"))  Then
	ef=True
	ec="产品ID应该是数字！"
ElseIf Instr(1, session("username"), "'", 1)>0 then
	ef=True
	ec="非法用户名！"
end if

if not ef then
Set conn=Server.CreateObject("ADODB.Connection")
	conn.Open Application("FreeHostDBlink")
	Set dsph = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_Product_Sql where id='"&session("SqlAUID")&"'"
			if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsph.Open Sql,conn,1,3
if dsph.Eof then
	ef=True
	ec="不存在的数据库ID！"
end if
end If

If not ef then
if not dsph("status")="正常" then
		ef=True
		ec="必须是处于正常状态的数据库才能管理！"
end if
end if

if not ef then
	Set dsHostpdlist = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_SQLProductlist where SQLtype='"&dsph("SQLtype")&"'"
	if Instr(1,sql, ";", 1)>0 or Instr(1,sql, "--", 1)>0 then
Response.Write "非法字符！"
Response.End
end if
	dsHostpdlist.Open Sql,conn,1,1
if dsHostpdlist.Eof then
	ef=True
	ec="产品不存在！"
end if
end if

if not ef then
	Set dsserver = Server.CreateObject("ADODB.Recordset")	
	Sql="Select * from FreeHost.FreeHost_ServerSQLlist where id='"&dsph ("ServerlistID")&"'"
	dsserver.Open Sql,conn,1,1
if dsserver.Eof then
	ef=True
	ec="服务器不存在！"
end if
end if

if not ef then

if isnull(dsph("randir")) then
Randomize
id=Int((99900000 - 0 + 1) * Rnd + 0)
dsph("randir")=id
dsph.update
end if

URL=dsserver("site")
FreeHostsharekey=dsserver("FreeHostsharekey")
userid=dsserver ("admname")
pwd=dsserver("admpass")
homedir=dsserver("homedir")
ip=dsserver("ip")
port=dsserver("port")
sqlname=dsph("sqlname")
sqlpassword=dsph("sqlpassword")

'backuptime=year(now)&month(now)&day(now)&hour(now)&Minute(now)&Second(now)
backuptime=Request("backupid")


dim softtype,MySQLver,MSSQLver
softtype=dsserver("softtype")
MySQLver=dsserver("MySQLver")
MSSQLver=dsserver("MSSQLver")
if Request("Submit")="建立备份用的FTP" then
if isnull(dsph("ftpendtime")) then
Randomize
idnew=Int((9999 - 0 + 1) * Rnd + 0)
username=sqlname&"-"&idnew
Randomize
idnew2=Int((999207770 - 0 + 1) * Rnd + 0)

ftpRkey=CLng((100000000 - 20 + 7850) * Rnd + 100)
Ftppassword=Ucase(Left(md5(now&ftpRkey),10))&right(md5(now&ftpRkey),4)'2010-8-5
idnew2=Ftppassword
ftppass=idnew2
ftpdir=replace(dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir"),"\\","\")
createuser=1
addacl=1
ftptype=1'默认1有读写的权限，改成2只读，改成3只写。
varcon=" /E /T /G "&username&":R"
ret=CreateDir(URL,FreeHostsharekey,replace(dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir"),"\\","\"))
ret2=CreateFTP(URL,FreeHostsharekey,username,ftppass,ftpdir,varcon,createuser,addacl, ftptype)
'ret2=CreateFTP(URL,FreeHostsharekey,username,ftppass,ftpdir,varcon,createuser,addacl, ftptype)
dsph("ftpname")=username
dsph("ftppassword")=ftppass
dsph("ftpendtime")=dateadd("d",3,now)'now
dsph.update
end if
end if

if Request("Submit")="删除备份用的FTP" then
if not isnull(dsph("ftpendtime")) then
username=dsph("ftpname")
ftpdir=replace(dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir"),"\\","\")
varcon=" /E /T /R "&username
todeluser=1
delacl=1
todelftp=1
ret2=DelFTP(URL,FreeHostsharekey,username,ftpdir,varcon,todeluser,delacl,todelftp)
dsph("ftpname")=null
dsph("ftppassword")=null
dsph("ftpendtime")=null
dsph.update
dsph.update
end if
end if

if Request("Submit")="建立备份" then
if softtype="SQL2000" then
backupdir=dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir")&"\"&backuptime&".bak"
ret=CreateDir(URL,FreeHostsharekey,dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir"))
ret2="备份返回信息："&SqlBACKUP(URL,FreeHostsharekey,userid,pwd,sqlname,backupdir)
else
backupdir=dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir")&"\"&backuptime&".sql"
varcon="--opt "&sqlname&" --user="&userid&" --password="&pwd&" -r """&backupdir&""""
ret=CreateDir(URL,FreeHostsharekey,dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir"))
ret2="MYQL备份返回信息："&Mysqldump(URL,FreeHostsharekey,varcon)&" 请您注意，您要求的备份操作会在1分钟后执行。为了能可靠地备份，建议您先停止您的网站。"
end if
dsph("backuptime"&Request("backupid"))=now
dsph.update
end if

if Request("Submit")="从备份中还原" then
if softtype="SQL2000" then
backupdir=dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir")&"\"&backuptime&".bak"
 sql="RESTORE FILELISTONLY FROM DISK = '"&backupdir&"'"
Cname="LogicalName"
recon=SqlGetName(URL,FreeHostsharekey,userid,pwd,backupdir,Cname,sql)
arrystr=split(recon,"@|freehost|@")
'arrystr(0)=Replace(Trim(arrystr(0)),vbcrlf,"")
'arrystr(1)=Replace(Trim(arrystr(1)),vbcrlf,"")
'response.write arrystr(0)&"<br>"
'response.write arrystr(1)&"<br>"
if ubound(arrystr)<>2 And InStr(recon,"611") then Response.Redirect "e.asp?e=原来的备份文件是SQL2005的文件,不能还原在SQL200中,相关信息为:："&recon
if ubound(arrystr)<>2 then Response.Redirect "e.asp?e=没有找到要还原的"&backuptime&".bak 文件，请检查这个文件是否存在，也有可能是受控端的版本不支持此项操作："&recon
ret2="还原返回信息："&SqlRESTORE(URL,FreeHostsharekey,userid,pwd,sqlname,backupdir,homedir,arrystr(0),arrystr(1))
userspacelimt=dsph("userspacelimt")
logspacelimt=dsph("logspacelimt")
ret2=ret2&SqlModify(URL,FreeHostsharekey,userid,pwd,userspacelimt,logspacelimt,sqlname)
else
userid=sqlname
pwd=sqlpassword
backupdir=dsserver("homedir")&"\backup\"&sqlname&"_"&dsph("randir")&"\"&backuptime&".sql"
varcon=" --user="&userid&" --password="&pwd&" -e ""source "&backupdir&""" "&sqlname
ret2="MYSQL还原返回信息："&MysqlRestore(URL,FreeHostsharekey,varcon)&" 请您注意，您要求的还原操作会在1分钟后执行。为了能可靠地还原，请您先停止您的网站。"
end if
end if



end if

if not ef then
%>
<!--#include virtual="/inc-freehost-function/template-sqladm-top.asp"-->
　数据库自助管理


<br>
<TABLE 
            width="100%" border=1 align=center cellPadding=0 cellSpacing=0 borderColorLight=#cccccc borderColorDark=#fbfbfb>
  <TBODY>
    <TR>
      <TD height="35" colspan="3" align="center" bgColor=#ffffff>数据库导入说明</TD>
    </TR>
    <TR>
      <TD height="91" colspan="3" align="left" bgColor=#ffffff><br>　如果您要将自己的数据库导入服务器，<br>
        　1.
      可以点击下面的“建立备份”一。<br>
      　2.
      点击建立“备份用FTP”。<br>
      　3.
      用FTP将您自己的数据库备份文件上传到服务器上，覆盖服务器上的1.bak(SQL 2000)/1.sql(Mysql)。<br>
      　4.
      再点从备份一中的“备份中还原。”<br>
      　5. 如果导入过程中发生意外，需要重新导入，您要先点清空数据库。<br>
      　　<font color="#FF0000">（在备份还原前，请您先停止您的网站一分钟后再操作，否则数据库正在使用中会造成操作失败。）</font><br><br></TD>
    </TR>
</TABLE>
<br>

<TABLE 
            width="100%" border=1 align=center cellPadding=0 cellSpacing=0 borderColorLight=#cccccc borderColorDark=#fbfbfb>
  <TBODY>
    <TR>
      <TD width="22%" align="center" vAlign=middle bgColor=#494949>&nbsp; <font color="#FFFFFF">备份数据库：</font></TD>
      <TD height="35" colspan="3" align="center" bgColor=#ffffff>数据库名：<strong><%=dsph("sqlname")%></strong>　数据库密码：<strong><%=dsph("sqlpassword")%></strong>　服务器IP：<%=dsserver("ip")%> </TD>
    </TR>
  <form action="sqlbackup.asp" method="post" name="form1">
    <TR>
      <TD width="22%" align="center" vAlign=middle bgColor=#EEEEEE>备份一：</TD>
      <TD width="24%" height="35" align="center" bgColor=#ffffff><%if isnull(dsph("backuptime1")) then%>未备份<%else%><%=dsph("backuptime1")%><%end if%>
        <input name="backupid" type="hidden" id="backupid" value="1">
        <input name="id" type="hidden" value="<%=Request("id")%>"></TD>
      <TD width="23%" align="center" bgColor=#ffffff><input name="Submit" type="submit" id="Submit" value="建立备份"></TD>
      <TD width="31%" height="35" align="center" bgColor=#ffffff><input name="Submit" type="submit" id="Submit" value="从备份中还原" <%if isnull(dsph("backuptime1")) then%>disabled<%end if%>></TD>
    </TR></form>
		<form action="sqlbackup.asp" method="post" name="form1">
    <TR>
      <TD width="22%" align="center" vAlign=middle bgColor=#EEEEEE>备份二：</TD>
      <TD width="24%" height="35" align="center" bgColor=#ffffff><%if isnull(dsph("backuptime2")) then%>未备份<%else%><%=dsph("backuptime2")%><%end if%>
        <input name="backupid" type="hidden" id="backupid" value="2">
        <input name="id" type="hidden" value="<%=Request("id")%>"></TD>
      <TD width="23%" align="center" bgColor=#ffffff><input name="Submit" type="submit" id="Submit" value="建立备份"></TD>
      <TD width="31%" height="35" align="center" bgColor=#ffffff><input name="Submit" type="submit" id="Submit" value="从备份中还原" <%if isnull(dsph("backuptime2")) then%>disabled<%end if%>></TD>
    </TR></form>
		<form action="sqlbackup.asp" method="post" name="form1">
    <TR>
      <TD width="22%" align="center" vAlign=middle bgColor=#EEEEEE>备份三：</TD>
      <TD width="24%" height="35" align="center" bgColor=#ffffff><%if isnull(dsph("backuptime3")) then%>未备份<%else%><%=dsph("backuptime3")%><%end if%>
        <input name="backupid" type="hidden" id="backupid" value="3">
        <input name="id" type="hidden" value="<%=Request("id")%>"></TD>
      <TD width="23%" align="center" bgColor=#ffffff><input name="Submit" type="submit" id="Submit" value="建立备份"></TD>
      <TD width="31%" height="35" align="center" bgColor=#ffffff><input name="Submit" type="submit" id="Submit" value="从备份中还原" <%if isnull(dsph("backuptime3")) then%>disabled<%end if%>></TD>
    </TR></form>
  </TBODY>
</TABLE>
<br>

  <form action="sqlbackup.asp" method="post" name="form1">
<TABLE 
            width="100%" border=1 align=center cellPadding=0 cellSpacing=0 borderColorLight=#cccccc borderColorDark=#fbfbfb>
  <TBODY>
    <TR>
      <TD width="22%" align="center" vAlign=middle bgColor=#494949>&nbsp; <font color="#FFFFFF">上传或下载备份：</font></TD>
      <TD height="35" colspan="3" align="center" bgColor=#ffffff>服务器IP：<%=dsserver("ip")%> </TD>
    </TR>
    <TR>
      <TD align="center" vAlign=middle bgColor=#EEEEEE>&nbsp;</TD>
      <TD width="24%" height="35" align="center" bgColor=#ffffff>临时FTP的有效期 </TD>
      <TD height="35" colspan="2" align="center" bgColor=#ffffff><%if isnull(dsph("ftpendtime")) then%>
    未设置
      <%else%>
      <%=dsph("ftpendtime")%>
      <%end if%>          </TD>
    </TR>
    <TR>
      <TD align="center" vAlign=middle bgColor=#EEEEEE>&nbsp;</TD>
      <TD height="35" align="center" bgColor=#ffffff>临时FTP的帐号</TD>
      <TD height="35" colspan="2" align="center" bgColor=#ffffff><%if isnull(dsph("ftpname")) then%>
    未设置
      <%else%>
      <%=dsph("ftpname")%><%end if%></TD>
    </TR>
    <TR>
      <TD align="center" vAlign=middle bgColor=#EEEEEE>&nbsp;</TD>
      <TD height="35" align="center" bgColor=#ffffff>临时FTP的密码</TD>
      <TD height="35" colspan="2" align="center" bgColor=#ffffff><%if isnull(dsph("ftppassword")) then%>
    未设置
      <%else%>
      <%=dsph("ftppassword")%><%end if%></TD>
    </TR>
    <TR>
      <TD width="22%" align="center" vAlign=middle bgColor=#EEEEEE>&nbsp;</TD>
      <TD height="35" colspan="3" align="center" bgColor=#ffffff><%if isnull(dsph("ftpendtime")) then%>
 <input name="Submit" type="submit" id="Submit" value="建立备份用的FTP"><%else%><input name="Submit" type="submit" id="Submit" value="删除备份用的FTP"><%end if%>
        <input name="id" type="hidden" value="<%=Request("id")%>">
        </TD>
    </TR>
</TABLE>  
  </form>
<%if softtype="SQL2000" then%>
<%end if%>
<font color=red><%=ret2%></font><br><br><br>

　<a href="selfsqlmodify.asp?id=<%=Request("id")%>"><font color="#000000">[返回上一页]</font></a><br><br>
<!--#include virtual="/inc-freehost-function/template-sqladm-end.asp"-->
<%end if
if ef then
Response.Redirect "e.asp?e="&ec
end if%>