如何将数据库备份至我的硬盘呀,从我的硬盘恢复大概是不太好实现吧?并且代码已有没听简单的,大家帮下
修改红色部分的吧?
从我的硬盘恢复大概是不太好实现吧?如果能实现的话给出下思路和方向,有代码最好?
<%
'*****************************************
function CopyTo(ByVal cFile,ByVal toFile)
cFile=Server.MapPath(cFile) '所要备份的文件
toFile=Server.MapPath(toFile) '备份文件
Dim cFso,cf
set cFso=Server.CreateObject("Scripting.FileSystemObject")
cFso.fileexists(cFile)
cFso.Copyfile cFile,toFile
end function
'*********************************************
'ASP实现备份及恢复ACCESS数据库操作
'本页面为 databackup.asp
dim dbpath,bkfolder,bkdbname,fso,fso1
call main()
call main2()
'conn.close
' set conn=nothing
sub main()
if request("action")="Backup" then
call backupdata()
else
%>
<table cellspacing=1 cellpadding=1 align=center width="90%">
<tr>
<th height=25 >
<B>数据库备份</B>
</th>
</tr>
<form method="post" action="databackup.asp?action=Backup">
<tr>
<td height=100 style="line-height:150%">
当前数据库路径(相对路径):
<input type=text size=15 name=DBpath value="../data/account.mdb"><BR>
备份数据库目录(相对路径):
<input type=text size=15 name=bkfolder value=../Databackup> 如目录不存在,程序将自动创建<BR>
备份数据库名称(填写名称):
<input type=text size=15 name=bkDBname value=database.mdb> 如备份目录有该文件,将覆盖,如没有,将自动创建<BR>
<input type=submit value="备份数据"><hr align="center" width="90%" color="#999999"></td>
</tr>
</form>
</table>
<%
end if
end sub
sub main2()
if request("action")="Restore" then
Dbpath=request.form("Dbpath")
backpath=request.form("backpath")
if dbpath="" then
response.write "请输入您要恢复成的数据库全名"
else
Dbpath=server.mappath(Dbpath)
end if
backpath=server.mappath(backpath)
Response.write Backpath
Set Fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(dbpath) then
fso.copyfile Dbpath,Backpath
response.write "<font color=red>成功恢复数据!</font>"
else
response.write "<font color=red>备份目录下并无您的备份文件!</font>"
end if
else
%>
<table align=center cellspacing=1 cellpadding=1 width="90%">
<tr>
<th height=25 >
<B>恢复数据库</B> </th>
</tr>
<form method="post" action="databackup.asp?action=Restore">
<tr>
<td height=100 >
备份数据库路径(相对):
<input type=text size=30 name=DBpath value="../Databackup/database.mdb"> <BR>
当前数据库路径(相对):
<input type=text size=30 name=backpath value="../data/account.mdb" />
<BR>
<input type=submit value="恢复数据"> <hr width="90%" align="center" color="#999999">
<font color="#666666">·注意:所有路径都是相对路径 </font></td>
</tr>
</form>
</table>
<%
end if
end sub
sub backupdata()
Dbpath=request.form("Dbpath")
Dbpath=server.mappath(Dbpath)
bkfolder=request.form("bkfolder")
bkdbname=request.form("bkdbname")
Set Fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(dbpath) then
If CheckDir(bkfolder) = True Then
fso.copyfile dbpath,bkfolder& "\\"& bkdbname
else
MakeNewsDir bkfolder
fso.copyfile dbpath,bkfolder& "\\"& bkdbname
end if
response.write "<font color=red>备份数据库成功,您备份的数据库路径为" &bkfolder& "\\"& bkdbname+"</font>"
Else
response.write "<font color=red>找不到您所需要备份的文件。</font>"
End if
end sub
'------------------检查某一目录是否存在-------------------
Function CheckDir(FolderPath)
folderpath=Server.MapPath(".")&"\\"&folderpath
Set fso1 = CreateObject("Scripting.FileSystemObject")
If fso1.FolderExists(FolderPath) then
'存在
CheckDir = True
Else
'不存在
CheckDir = False
End if
Set fso1 = nothing
End Function
'-------------根据指定名称生成目录---------
Function MakeNewsDir(foldername)
dim f
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set f = fso1.CreateFolder(foldername)
MakeNewsDir = True
Set fso1 = nothing
End Function
%>
[解决办法]
<!--#include file="../include/connection_backup.asp" -->
<!--上面的是数据库连接语句-->
<!--另一个页面见下一帖-->
<%
asp_name="database_backup.asp" '本页页面名称
'统计备份的文件数
Set FileRs_1= Server.CreateObject ("Adodb.Recordset")
sqlstr_1 ="Select * from savetimes"
FileRs_1.Open sqlstr_1,strconn_bk,1,1
if not FileRs_1.eof then'若今天已经备份过
jilu=FileRs_1.recordcount
else
jilu=0
end if
FileRs_1.close
set FileRs_1=nothing
action=request("action")
If action="backup" then
if request("act")="bk" then
call updata()
'更新数据库
Set FileRs1 = Server.CreateObject("ADODB.connection")
FileRs1.Open strconn_bk
sqlStr1 = "update savetimes set save_times='"&now()&"'"
FileRs1.Execute sqlStr1
FileRs1.Close
Set FileRs1 = Nothing
end if
elseif action="restore" then
if request("act")="hf" then
call restore()
end if
End if
%>
<%
sub updata()
Dbpath=request.form("Dbpath")
Dbpath=server.mappath(Dbpath)
bkfolder=request.form("bkfolder")
bkfolder2=request.form("bkfolder")
bkdbname=request.form("bkdbname")
'判断今天是否已经备份过
Set FileRs_1= Server.CreateObject ("Adodb.Recordset")
sqlstr_1 ="Select bkdbname from savetimes where bkdbname='"&bkdbname&"'"
FileRs_1.Open sqlstr_1,strconn_bk,1,1
if not FileRs_1.eof then'若今天已经备份过
if_beifen=1
action_result=2
else
if_beifen=0
action_result=1
end if
FileRs_1.Close()
Set FileRs_1 = Nothing
if if_beifen=0 then
'数据库备份行为的记录添加 这一段数据库添加语句你可以不用
Set FileRs_add_ts = Server.CreateObject("ADODB.connection")
sqlStr_add_ts = "insert into savetimes (bkdbname,backup_date,backup_man) values ('"&bkdbname&"','"&scrq&"-"&time()&"','"&session("true_name")&"')"
FileRs_add_ts.Open strconn_bk
FileRs_add_ts.Execute sqlStr_add_ts
FileRs_add_ts.Close
Set FileRs_add_ts = Nothing
end if
Set Fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(dbpath) then
If CheckDir(bkfolder) = True Then
fso.copyfile dbpath,bkfolder& "\"& bkdbname
else
MakeNewsDir bkfolder
fso.copyfile dbpath,bkfolder& "\"& bkdbname
end if
Else
action_result=5
End if
%>
<%
Select Case action_result
Case 1:tishi=scrq&"-"&time()&"数据库备份成功!点击后返回!\n\r您备份的数据库路径为服务器路径下:\n\r"&bkfolder2&"/"&bkdbname&"\n\r并且已经将此次备份行为记录下来"
Case 2:tishi=scrq&"-"&time()&"数据库备份成功!点击后返回!\n\r您备份的数据库路径为服务器路径下:\n\r"&bkfolder2&"/"&bkdbname&"\n\r今天已经有过备份,故不作记录"
Case 5:tishi="数据库备份失败!找不到您所需要备份的路径!点击后返回!"
End Select
%>
<%if action_result<>"" then%>
<script language="javascript">
alert("\<%=tishi%>")
window.location='<%=asp_name%>'
</script>
<%end if%>
<%end sub
sub restore()
date1=request.form("date1")
Dbpath=request.form("Dbpath")
Dbpath=Dbpath&date1&".mdb"
Dbpath=server.mappath(Dbpath)
bkfolder=request.form("bkfolder")
bkfolder2=request.form("bkfolder")
bkdbname=request.form("bkdbname")
Set Fso=server.createobject("scripting.filesystemobject")
if fso.fileexists(dbpath) then
If CheckDir(bkfolder) = True Then
fso.copyfile dbpath,bkfolder& "\"& bkdbname
else
MakeNewsDir bkfolder
fso.copyfile dbpath,bkfolder& "\"& bkdbname
end if
action_result=3
Else
action_result=4
End if
%>
<%
Select Case action_result
Case 3:tishi="数据库恢复成功!点击后返回!您恢复的数据库路径为服务器路径下:"&bkfolder2&"\"&bkdbname
Case 4:tishi="数据库恢复失败!找不到所需要的备份档案!点击后返回!"
End Select
%>
<%if action_result<>"" then%>
<script language="javascript">
alert("\<%=tishi%>")
window.location='<%=asp_name%>'
</script>
<%end if%>
<%
end sub
'------------------检查某一目录是否存在-------------------
Function CheckDir(FolderPath)
folderpath=Server.MapPath(".")&"\"&folderpath
Set fso1 = CreateObject("Scripting.FileSystemObject")
If fso1.FolderExists(FolderPath) then
'存在
CheckDir = True
Else
'不存在
CheckDir = False
End if
Set fso1 = nothing
End Function
'-------------根据指定名称生成目录-----------------------
Function MakeNewsDir(foldername)
dim f
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set f = fso1.CreateFolder(foldername)
MakeNewsDir = True
Set fso1 = nothing
End Function
%>
[解决办法]
<!--以下是本页面HTML语句和界面-->
<script language="javascript" src="../include/script.js"></script>
<script language="javascript" src="../include/date.js"></script>
<script language="javascript">
function check(){
if (document.form.date1.value==""){
alert("\请选择备份恢复的日期!")
document.form.date1.focus()
return false
}
}
</script>
<html>
<head>
<title>数据库备份</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="../CSS/style.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style7 {color: #BC1600;
font-weight: bold;
}
.style8 {color: #FFFFFF}
body {
background-image: url(../image/shangren_<%Response.Write week_color%>.gif);
}
.style9 {color: #A45E3A}
.style10 {color: #339933}
.style11 {color: #FF66FF}
.style12 {color: #666666}
.STYLE13 {color: #FF0000}
-->
</style>
</head>
<body bgcolor="#FFFFFF" onselectstart="return false" onpaste="return false" oncontextmenu=return(false)>
<table width="100%" height="100%" border="0" align="right" cellpadding="0" cellspacing="0">
<tr>
<td><table width="800" height="500" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF" background="../image/yinmei_index.jpg">
<tr>
<td><p align="center"> </p>
<table width="60%" border="0" align="right" cellpadding="0" cellspacing="6">
<tr>
<td><div align="center"></div>
<table width="100%" height="" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height=25 class="forumrow"><font color=red><b><%=txt%></b></font></td>
</tr>
<tr>
<td height=100 class="forumrow">
<form method="post" action="database_backup.asp">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<%
db="../yinmei.mdb"
db_bk="../database-copy"
dbname="../database-copy/yinmei_bk_"&scrq&".mdb"
%>
<span class="style10">当前数据库名(相对路径)</span>:
<input type=text size=20 name=DBpath value="<%=db%>" readonly>
<BR>
<span class="style11">备份数据库目录(相对路径)</span>:
<input type=text size=20 name=bkfolder value="<%=db_bk%>" readonly>
<BR>
<span class="style12">备份数据库名(填写名称)</span>:
<br>
<input type=text size=45 name=bkDBname value="<%Response.Write dbname%>" readonly>
自动覆盖/创建
<br></td>
</tr>
<tr>
<td height="50"><div align="center">
<input name="submit" type=submit class="cn_button01" onClick="javascript:return confirm('备份后数据库只能按天恢复,并且将覆盖当天的备份文件,确定吗?')" value="开始备份">
<input name="action" value="backup" type="hidden">
<input name="act" value="bk" type="hidden">
</div></td>
</tr>
<tr>
<td><div align="center">
<a href="load_beifens.asp">管理全部的备份记录</a>(有<%=jilu%>个,<span class="STYLE13">请记得将相隔多天的备份删除</span>)</div></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</td>
</tr>
<tr valign="bottom">
<td><div align="center">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><div align="center">
<form method="post" action="database_backup.asp" name="form" onSubmit="return check()">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<%
db="../yinmei.mdb"
db_bk="../database-copy"
dbname="../database-copy/yinmei_bk_"
%>
<span class="style10">源数据库名(相对路径)</span>:
<br><input name=DBpath type=text id="DBpath" value="<%=dbname%>" size=30 readonly>
+<input name="date1" type="text" id="date1" size="10" readonly="true">
<a href="#"><img src="../image/button.gif" name="imageCalendar1" width="24" height="18" border="0" align="absmiddle" id="imageCalendar1" onClick="setday(this,date1)"></a>.mdb
<BR>
<span class="style11">目标数据库目录(相对路径)</span>:
<input name=bkfolder type=text id="bkfolder" value="<%=db_bk%>" size=20 readonly>
<BR>
<span class="style12">目标数据库名(填写名称)</span>:
<input name=bkDBname type=text id="bkDBname" value="<%=db%>" size=20 readonly>
自动覆盖/创建 <br>
</td>
</tr>
<tr>
<td><div align="center">
<input name="submit" type=submit class="cn_button03" onClick="javascript:return confirm('将恢复指定的数据库备份,确定吗?')" value="开始恢复">
<input name="action" value="restore" type="hidden">
<input name="act" value="hf" type="hidden">
</div></td>
</tr>
<tr>
<td>---------------------------------------<br>
<span class="style9">注意:所有路径都是相对与程序空间根目录的相对路径</span></td>
</tr>
</table>
</form>
</div></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><div align="center"><%
Set FileRs_bktimes = Server.CreateObject("ADODB.recordset")
sqlStr_bktimes= "Select save_times from savetimes"
FileRs_bktimes.Open sqlStr_bktimes,strconn_bk,1,1
if not FileRs_bktimes.eof then
save_times=FileRs_bktimes("save_times")
else
save_times="上次更新时间:未知"
end if
%><%Response.Write "上次更新时间:"&save_times%></div></td>
</tr>
<tr>
<td><div align="center"><br>
<input name="btn22" type="button" class="cn_button02" onClick="location.href='../index.asp?action_type=3'" value="回到主菜单">
</div></td>
</tr>
</table>
</div></td>
</tr>
</table>
<p align="center"> </p>
<p align="center"> </p>
<div align="center"><br>
<br>
</div></td>
</tr>
</table></td>
</tr>
</table>
<p> </p>
</body>
</html>