用ASP备份还原SQL Server

<HTML>
<HEAD>
<TITLE>SQL Server 数据库的备份与恢复</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<BODY>

<script language="javascript">
function inputdir()
{
if(event.srcElement.value =="wdir")
{
myform.bakfileName.disabled = true;
}
else if (event.srcElement.value == "cdir")
{
myform.bakfileName.disabled = false;
}
}
</script>
<%
if request.ServerVariables("CONTENT_LENGTH")=0 then
call fillform()
else
call backupDB()
call fillform()
end if

sub fillform()%>
<form method="post" name=myform>
<table width="433" border="1" align="center" cellpadding="0" cellspacing="0">
<caption>
SQL Server 数据库的备份与恢复
</caption>
<tr>
<td>选择操作:<label for=act_restore></label></td>
<td>
<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></td>
</tr>
<tr>
<td>帐号:</td>
<td><input name="adminName" type="text" ></td>
</tr>
<tr>
<td>密码:</td>
<td><input name="adminPassword" type="password" ></td>
</tr>
<tr>
<td>数据库名:</td>
<td><input name="databaseName" type="text" ></td>
</tr>
<tr>
<td>备份路径:</td>
<td>
<label><input type="radio" name="BackUpType" value="wdir" checked onClick="inputdir()">当前目录</label>
<label><input type="radio" name="BackUpType" value="cdir" onClick="inputdir()">自定义</label>
</td>
</tr>
<tr>
<td>文件路径:</td>
<td><input type="text" id="bakfileName" name="bakfileName" value="c:$N.bak" disabled>(备份或恢复的文件路径)</td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="submit" type="submit" value="确定">
<input name="Reset" type="reset" value="重置">
</div></td>
</tr>
</table>
</form>
<% end sub %>

<%
sub backupDB()
dim sqlserver,adminName,adminPassword,sqlLoginTimeout,databasename,bakfileName,act
sqlserver = "localhost" 'sql服务器
adminName = Trim(Request.Form("adminName")) '用户名
adminPassword = Trim(Request.Form("adminPassword")) '密码
sqlLoginTimeout = 15 '登陆超时
databaseName = trim(request.Form("databaseName"))
BackUpType = trim(request.Form("BackUpType"))
if BackUpType = "wdir" then
bakfileName = request.ServerVariables("APPL_PHYSICAL_PATH") & "" & databaseName & ".bak"
elseif BackUpType = "cdir" then
bakfileName = trim(request.Form("bakfileName"))
bakfileName = replace(bakfileName,"$N",databaseName)
else

end if
act = lcase(request("act"))

if databasename = "" then
response.write "<center><font color=red>没有输入数据库名!</font></center>"
'response.End()
elseif bakfileName = "" then
response.Write "<center><font color=red>没有输入备份文件名!</font></center>"
else
if act = "backup" then
Set srv=Server.CreateObject("SQLDMO.SQLServer")
srv.LoginTimeout = sqlLoginTimeout
srv.Connect sqlserver,adminName,adminPassword
Set bak = Server.CreateObject("SQLDMO.Backup")
bak.Database=databasename
bak.Devices=Files
bak.Files=bakfileName
bak.SQLBackup srv
if err.number<>0 then
response.write err.number & "<font color=red><br>"
response.write err.description & "</font>"
else
response.Write "<center><font color=blue>备份成功!</font></center>"
'response.End()
end if
elseif act = "restore" then
'恢复时要在没有使用数据库时进行!
Set srv=Server.CreateObject("SQLDMO.SQLServer")
srv.LoginTimeout = sqlLoginTimeout
srv.Connect sqlserver,adminName,adminPassword
Set rest=Server.CreateObject("SQLDMO.Restore")
rest.Action=0 ' full db restore
rest.Database=databasename
rest.Devices=Files
rest.Files=bakfileName
rest.ReplaceDatabase=True 'Force restore over existing database
rest.SQLRestore srv
if err.number<>0 then
response.write err.number & "<font color=red><br>"
response.write err.description & "</font>"
else
Response.write "<center><font color=green>恢复成功!</font></center>"
'response.End()
end if
else
Response.write "<center><font color=red>没有选择操作!</font></center>"
'response.End()
end if
end if
end sub
%>
</BODY>
</HTML>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据