<%
'調用方法: DBbackup.asp?dbName=資料庫名稱
'只能備份IIS所在的伺服器SQL資料庫(不能備份遠程SQL資料庫)。
'Eric 2009.12.7 14:18
const conf_dbhost = "(local)"
const conf_dbuser = "sa" '資料庫使用者名稱
const conf_dbpassword = "inchbyinch" '資料庫密碼
dim conf_dbname
Dim conf_dbSavePath
conf_dbname = trim(request.QueryString("dbName"))
conf_dbSavePath = server.MapPath(".\")&"\"&conf_dbname&getDateTimeSeries()&".bak"
If conf_dbname <>"" then
Set connObj = Server.CreateObject("ADODB.Connection")
connObj.Open "driver={SQL Server};server=" & conf_dbhost & ";uid=" & conf_dbuser & ";pwd=" & conf_dbpassword & ";database=" & conf_dbname
Set Rstmp = Server.CreateObject("adodb.recordset")
strSql = "backup database "&conf_dbname&" TO DISK='"&conf_dbSavePath&"'"
'response.write strSql
Rstmp.Open strSql,connObj,1,1
if err Then
response.Write(Err.Description)
response.End()
set Rstmp = nothing
set connObj = Nothing
Else
Call DownloadFile(conf_dbSavePath)
Call delFile(conf_dbSavePath)
End If
Else
response.write "調用方法:DBbackup.asp?dbName=資料庫名稱"
End If
'得到由時間產生的隨機數 20060101221022位隨機數
Function getDateTimeSeries()
dim yyyy,mm,dd,h,m,s,MyValue
yyyy = year(now)
mm = right("00"&cstr(month(now)),2)
dd = right("00"&cstr(day(now)),2)
h = right("00"&cstr(hour(now)),2)
m = right("00"&cstr(minute(now)),2)
s = right("00"&cstr(second(now)),2)
Randomize
MyValue = Int((1000 * Rnd) + 1)
getDateTimeSeries = yyyy&mm&dd&h&m&s
End Function
Function DownloadFile(strFilename)
'清空Buffer
Response.Buffer = True
Response.Clear
'建立Stream對象
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(strFilename) Then
' downloadFile="NoFile"
'Exit Function
'End If
'計算檔案長度
Set f = fso.GetFile(strFilename)
intFilelength = f.size
If filename="" Then
filename=f.name
End If
s.LoadFromFile(strFilename)
if err then
Response.Write("<h1>Error: </h1>" & err.Description & "<p>")
Response.End
end if
'向使用者瀏覽器發送Header
Response.AddHeader "Content-Disposition", "attachment; filename=" & filename
Response.AddHeader "Content-Length", intFilelength
Response.CharSet = "UTF-8"
Response.ContentType = "application/octet-stream"
'輸出檔案
'對於小於4096KB的檔案可以用語句
'Response.BinaryWrite s.Read
'Response.Flush
'完成,但對於大於4096KB的檔案要分段輸出,如下迴圈操作。
Do While Not s.EOS
Contents = s.Read (4096) '每次讀取4096KB
Response.BinaryWrite Contents
Response.Flush
Loop
'清理
s.Close
Set s = Nothing
End Function
'刪除檔案
Function delFile(fileName)
Set fso = Server.CreateObject("Scripting.FileSystemObject")
fso.GetFile(fileName).Delete()
Set fso = nothing
End Function
%>