ASP online backup and recovery of sql server database is a temporary solution for friends who do not provide remote sqlserver remote connection or package download. It may not be effective for big data. asp online backup sql server database:
1. Back up sqlserver
Copy the code code as follows:
<%
SQL=backup database database name to disk='&Server.MapPath(backup)&/&backuptext.dat&'
set cnn=Server.createobject(adodb.connection)
cnn.open driver={SQL Server};Server=server name;uid=sa;pwd=
cnn.execute SQL
on error resume next
if err<>0 then
response.wrITe Error: &err.Descripting
else
response.wrITe Data backup successful!
end if
%>
2. Restore sqlserver
Copy the code code as follows:
<%
SQL=Restore database database name from disk='&Server.MapPath(backup)&/&backuptext.dat&'
set cnn=Server.createobject(adodb.connection)
cnn.open driver={SQL Server};Server=server name;uid=sa;pwd=
cnn.execute SQL
on error resume next
if err<>0 then
response.wrITe Error:&err.Descripting
else
response.wrITe Data recovery successful!
end if
%>
ACCESS principle is the same
Copy the code code as follows:
<%
'****************************************
function CopyTo(ByVal cFile,ByVal toFile)
cFile=Server.MapPath(cFile) 'The file to be backed up
toFile=Server.MapPath(toFile) 'Backup file
Dim cFso,cf
set cFso=Server.CreateObject(Scripting.FileSystemObject)
cFso.fileexists(cFile)
cFso.Copyfile cFile,toFile
end function
'************************************************
'ASP implements backup and restore ACCESS database operations
'This page is 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>Database backup</B>
</th>
</tr>
<form method=post action=databackup.asp?action=Backup>
<tr>
<td height=100 style=line-height:150%>
Current database path (relative path):
<input type=text size=15 name=DBpath value=../mdb/database.mdb><BR>
Backup database directory (relative path):
<input type=text size=15 name=bkfolder value=../Databackup> If the directory does not exist, the program will automatically create it<BR>
Backup database name (fill in the name):
<input type=text size=15 name=bkDBname value=database.mdb> If the backup directory has this
The file will be overwritten. If it does not exist, it will be created automatically<BR>
<input type=submIT value=Backup data><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 Please enter the full name of the database you want to restore to
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>Data recovered successfully! </font>
else
response.wrITe <font color=red>There is no backup file for you in the backup directory! </font>
end if
else
%>
<table align=center cellspacing=1 cellpadding=1 width=90%>
<tr>
<th height=25 >
<B>Restore database</B>
</th>
</tr>
<form method=post action=databackup.asp?action=Restore>
<tr>
<td height=100>
Backup database path (relative):
<input type=text size=30 name=DBpath value=../Databackup/database.mdb> <BR>
Current database path (relative):
<input type=text size=30 name=backpath value=../mdb/database.mdb><BR>
<input type=submIT value=Recover data> <hr width=90% align=center color=#999999>
<font color=#666666>·Note: All paths are relative paths</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>The database backup was successful. The database path you backed up is &bkfolder& //& bkdbname+</font>
Else
response.wrITe <font color=red>The file you need to back up cannot be found. </font>
End if
end sub
'------------------Check whether a certain directory exists------------------
Function CheckDir(FolderPath)
folderpath=Server.MapPath(.)&//&folderpath
Set fso1 = CreateObject(Scripting.FileSystemObject)
If fso1.FolderExists(FolderPath) then
'exist
CheckDir = True
Else
'does not exist
CheckDir = False
End if
Set fso1 = nothing
End Function
'-------------Generate a directory based on the specified name---------
Function MakeNewsDir(foldername)
dim f
Set fso1 = CreateObject(Scripting.FileSystemObject)
Set f = fso1.CreateFolder(foldername)
MakeNewsDir = True
Set fso1 = nothing
End Function
%>