always there for you

善于生活,善于学习,这是我和你的目标.

« 网站生成静态页面攻略 兼容IE,FF的背景暗一点突出效果 »

asp-开机-自动备份数据库

今天试着做了自动备份数据库到管理员的主机上,功能大致实现了.

当管理员运行客户端脚本时,会自动连接服务器启用相应的asp脚本,备份数据库到服务器上,然后再自动下载到管理员的主机上.

不过程序刚做出来,现在也不知道有没有什么遗漏,如果大家想到希望指点下!

先看服务器端的代码:.

   autobackupdb.asp

 '*******************************************************
 '目的:自动备份数据库到服务器上和网站管理员的主机
 '作者:always there
 '时间:2008-04-08
 '主页:http://www.alwaysthere.com.cn
 '*******************************************************
 
 
 const verifystring="alwaysthere"    '--定义验证字符
 const SQL_injdata = "'|;|-"               '--定义过滤字符
 
 Dim strVerify,clientIP                       '--接收验证字符,发送请求的IP
 Dim objXML,x                                    '--xmlDOM对象,x为集合
 Dim SQL_inj                                     
 Dim dbBackup,dbSource                
 dbSource = server.mapPath("#test.mdb")                    '--源数据库路径
 dbBackup = server.mapPath("#backup" & date() & ".mdb")    '--备份数据库路径

 

SQL_inj = split(SQL_injdata,"|")                   '--过滤字符的数组
 
'--验证QueryString,Form,Cookies里是否有非法字符

 If Request.QueryString<>"" Then
  For Each SQL_Get In Request.QueryString
   For SQL_Data=0 To Ubound(SQL_inj)
    if instr(Lcase(Request.QueryString(SQL_Get)),Sql_Inj(Sql_DATA))>0 Then
     Response.Write "网址中包含非法字符"&Sql_Inj(Sql_DATA)
     Response.End
    End If
   Next
  Next
 End If
 
 If Request.Form<>"" Then
  For Each Sql_Post In Request.Form
   For SQL_Data=0 To Ubound(SQL_inj)
    if instr(Lcase(Request.Form(Sql_Post)),Sql_Inj(Sql_DATA))>0 Then
     Response.Write "表单中包含非法字符"&Sql_Inj(Sql_DATA)
     Response.end
    End If
   Next
  Next
 End If
 
 If Request.Cookies<>"" Then
  For Each Sql_Cookies In Request.Cookies
   For SQL_Data=0 To Ubound(SQL_inj)
    if instr(Lcase(Request.Cookies(Sql_Cookies)),Sql_Inj(Sql_DATA))>0 Then
     Response.Write "Cookies中包含非法字符"&Sql_Inj(Sql_DATA)&",请在 IE选项-常规 里删除Cookiess再执行操作!"
     Response.end
    End If
   Next
  Next
 End If

 

strVerify = trim(request.querystring("verifystring"))
 clientIP = Request.ServerVariables("Remote_Addr") 
 
 '--检验发送请求的用户IP是否存在黑名单里
 set objXML = server.createObject("Microsoft.XMLDOM")
 objXML.load(server.mapPath("hbtIP.xml"))
 set x = objXML.getElementsByTagName("hbtIP")
 For i = 0 to x.length-1
  If x(i).text = clientIP Then
   Response.Write "做人不能太CNN"
   Response.End
  End If
 Next
 set objXML = nothing

 

 '--当传递过来的验证字符串不符合,即把发送请求的ip拖入黑名单
 If strVerify<>verifystring Then
    ''打开xml添加ip
    set objXML=server.createObject("Microsoft.XMLDOM")
    objXML.Load(server.mapPath("hbtIP.xml"))
    set newNode=objXML.createElement("hbtIP")
    newNode.text=clientIP
    objXML.documentElement.appendChild newNode
    objXML.save(server.mapPath("hbtIP.xml"))
    set objXML=nothing
    Response.End
 End IF    

 

 '--备份数据库到服务器
 set fso = server.createobject("scripting.filesystemObject")
 if Not fso.FileExists(dbSource) Then
  Response.Write "数据库不存在,请检查"
  Response.End
 End if
 fso.CopyFile dbSource,dbBackUp
 If not Err then
  Response.Write "服务器端备份成功"
 End If   
 set fso = nothing

 

'--下载备份数据库到管理员主机   以下部分可能考虑不周全,请谨慎使用
 downloadFile(dbBackup)
 
 Function  downloadFile(strFile)    
 Dim fileName
 fileName=split(strFile,"\")(Ubound(split(strFIle,"\")))
 Response.Buffer  =  True   
 Response.Clear    
 Set  s  =  Server.CreateObject("ADODB.Stream")   
 s.Open     
 s.Type  =  1     
 on  error  resume  next   
 Set  fso  =  Server.CreateObject("Scripting.FileSystemObject")   
 if  not  fso.FileExists(strFile)  then   
  Response.Write("<h1>Error:</h1>"&strFile&" does not exists!<p>")      
  Response.End   
 end  if
 Set  f  =  fso.GetFile(strFile)   
  intFilelength  =  f.size   
  s.LoadFromFile(strFile)   
 if  err  then   
  Response.Write("<h1>Error: </h1>Unknown Error!<p>")   
  Response.End
 end  if 

 Response.AddHeader  "Content-Disposition","attachment;  filename="+fileName
 Response.AddHeader  "Content-Length",intFilelength   
 Response.CharSet  =  "UTF-8"   
 Response.ContentType  =  "application/octet-stream"      
 Response.BinaryWrite  s.Read   
 Response.Flush   
  
 s.Close  
 Set  s  =  Nothing  
 Set f = nothing
 Set fso = nothing
 End  Function   

    这种办法备份数据库到服务器没什么问题,但是之后下载数据库到管理员主机上有风险,所以还是得改进,大家有什么好的看法,也可以提出来!.一起研究学习.

下面这个是管理员电脑上的客户端脚本,参考了爬坡者的<定时运行你的asp文件>这篇文章.

Dim IE,verifystring

verifystring =""          '--验证字符串,第一次使用请填写
If verifystring="" Then
 verifystring = inputbox("请输入","请输入验证字符串")
End if

Set IE = CreateObject("InternetExplorer.Application")
IE.navigate("http://www.alwaysthere.com.cn/Test/backupdb/autobackupdb.asp?verifystring="&verifystring)
IE.visible = 1
Set IE = nothing

 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Copyright www.alwaysthere.com.cn . Some Rights Reserved.

              

Powered By Z-Blog 1.8 Devo Build 80201