<!-- #include file="conn.asp" -->
<!-- #include file="admin.asp" -->
<%'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
'//  '数据库备份工具 ASP V1.0

'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&%>
<HTML>
<HEAD>
<TITLE>SQL Server 数据库的备份与恢复</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>
<BR><font color=green><a href="index.asp?sid=<%=sid%>">网站后台管理</a></font><hr/>
<%

bak_file = trim(Request.Form("bak_file"))
        
            dim fso,Files
            Set fso = CreateObject("Scripting.FileSystemObject")
            If fso.FileExists(bak_file) Then
%>
               备份目标数据库<font color=#85456><%=bak_file%></font>已经存在，请删除或更换备份文件名进行备份

<hr><br><font color=green><a href="sqlbak.asp?sid=<%=sid%>">数据备份</a></font>
<%
response.end

 end if
%>

<%



dim sqlserver,sqlname,sqlpassword,sqlLoginTimeout,databasename,bak_file,act,mypath
sqlserver = ""&YD_SQL_SERVERIP&"" 'sql服务器
sqlname = ""&YD_SQL_UserName&""   '用户名
sqlpassword = ""&YD_SQL_PassWord&""  '密码
sqlLoginTimeout = 15 '登陆超时
databasename = trim(request("databasename"))
bak_file = trim(request("bak_file"))
bak_file = replace(bak_file,"$1",databasename)
if databasename="" then databasename=""&YD_Main_DatabaseName&""
act = lcase(request("act"))
mypath=server.mappath("\others\mssql\")
mypath=replace(mypath,"","")



if databasename <>""&YD_Main_DatabaseName&"" then%>
 <font color=red>找不到数据库，可能你输入出错</font>
<%else

 if act = "backup" then
  Set srv=Server.CreateObject("SQLDMO.SQLServer")
  srv.LoginTimeout = sqlLoginTimeout
  srv.Connect sqlserver,sqlname, sqlpassword
  Set bak = Server.CreateObject("SQLDMO.Backup")
  bak.Database=databasename
  bak.Devices=Files
  bak.Files=bak_file
  bak.SQLBackup srv


 SQL="backup database "&YD_Main_DatabaseName&" to disk='"&bak_file&"'" 

 conn.execute SQL
 on error resume next 
 if err<>0 then 
 response.write "错误："&err.Descripting 
 else 


%>
  ok,数据库<font color=red><%=YD_Main_DatabaseName%></font>备份成功!
目标：<font color="#85456"><%=bak_file%></font>
<%end if
 elseif act = "restore" then
  '恢复时要在没有使用数据库时进行！
  Set srv=Server.CreateObject("SQLDMO.SQLServer")
  srv.LoginTimeout = sqlLoginTimeout
  srv.Connect sqlserver,sqlname, sqlpassword
  Set rest=Server.CreateObject("SQLDMO.Restore")
  rest.Action=0 ' full db restore
  rest.Database=databasename
  rest.Devices=Files
  rest.Files=bak_file
  rest.ReplaceDatabase=True 'Force restore over existing database
  if err.number>0 then
    response.write err.number&"<font color=red>1111<br>"
    response.write err.description&"</font>"
  end if
  rest.SQLRestore srv
  %>
<font color=red>OK,数据库<%=YD_Main_DatabaseName%>恢复成功!</font>
 <%else%>

  <font color=red>请选择备份或恢复数据库</font>
<%
 end if
end if
%>

<form method="post" name="myform">
<INPUT TYPE="radio" NAME="act" id="act_backup"  value="backup"><label for="act_backup">备份</label>　
<INPUT TYPE="radio" NAME="act" id="act_restore" value="restore"><label for="act_restore">恢复</label>
<br>默认数据库(不要更改)：<br/><INPUT TYPE="text" NAME="databasename" value="<%=YD_Main_DatabaseName%>">
<br>(备份或恢复的文件路径)<br/><INPUT TYPE="text" NAME="bak_file" value="<%=mypath%>\<%=date%>.bak"><br><hr>
<input type="submit" value="确定提交数据"><br><hr>
MSSQL数据库在线备份 V1.0
</form><br/>
</BODY>
</HTML>
