ASP online Backup SQL Server database:
1. Backup SQL Server
Copy Code code as follows:
<%
sql= "BACKUP 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 success!" "
End If
%>
2. Restore SQL Server
Copy 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 success!" "
End If
%>
The same principle of access
Copy Code code as follows:
<%
'*****************************************
function CopyTo (ByVal cfile,byval tofile)
Cfile=server.mappath (CFile) ' files to be backed up
Tofile=server.mappath (ToFile) ' Back up files
Dim CFSO,CF
Set Cfso=server.createobject ("Scripting.FileSystemObject")
Cfso.fileexists (CFile)
Cfso.copyfile Cfile,tofile
End Function
'*********************************************
' ASP to implement 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>
Back up the database directory (relative path):
<input type=text size=15 name=bkfolder value=. /databackup> If the directory does not exist, the program will automatically create <BR>
Backup database name (fill in name):
<input Type=text size=15 name=bkdbname value=database.mdb> such as the backup directory has the
Files that will be overwritten, if not, will be automatically created <BR>
<input type=submit value= "Backup Data" ></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"
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> recover data successfully!" </font> "
Else
Response.Write "<font color=red> backup directory does not have your backup files! </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= "restore Data" > <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> Backup database is successful, your backup database path is" &bkfolder& \ "& bkdbname+" </font> "
Else
Response.Write "<font color=red> cannot find the file you need to back up. </font> "
End If
End Sub
'------------------Check if a directory exists-------------------
Function Checkdir (FolderPath)
Folderpath=server.mappath (".") & "\" &folderpath
Set Fso1 = CreateObject ("Scripting.FileSystemObject")
If Fso1. FolderExists (FolderPath) Then
' Existence
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
%>