求thinkphp 数据库的备份、还原的脚本

求thinkphp 数据库的备份、还原的脚本,第1张

一、备份数据库并下载到本地db_backupphp

代码代码如下:

<php

// 设置SQL文件保存文件名

$filename=date("Y-m-d_H-i-s")"-"$cfg_dbname"sql";

// 所保存的文件名

header("Content-disposition:filename="$filename);

header("Content-type:application/octetstream");

header("Pragma:no-cache");

header("Expires:0");

// 获取当前页面文件路径,SQL文件就导出到此文件夹内

$tmpFile = (dirname(__FILE__))"\\"$filename;

// 用MySQLDump命令导出数据库

exec("mysqldump -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname > "$tmpFile);

$file = fopen($tmpFile, "r"); // 打开文件

echo fread($file,filesize($tmpFile));

fclose($file);

exit;

>

二、还原数据库db_restorephp

代码代码如下:

<form id="form1" name="form1" method="post" action="">

数据库SQL文件:<input id="sqlFile" name="sqlFile" type="file" />

<input id="submit" name="submit" type="submit" value="还原" />

</form>

<php

// 我的数据库信息都存放到configphp文件中,所以加载此文件,如果你的不是存放到该文件中,注释此行即可;

require_once((dirname(__FILE__)'///include/configphp'));

if ( isset ( $_POST['sqlFile'] ) )

{

$file_name = $_POST['sqlFile']; //要导入的SQL文件名

$dbhost = $cfg_dbhost; //数据库主机名

$dbuser = $cfg_dbuser; //数据库用户名

$dbpass = $cfg_dbpwd; //数据库密码

$dbname = $cfg_dbname; //数据库名

set_time_limit(0); //设置超时时间为0,表示一直执行。当php在safe mode模式下无效,此时可能会导致导入超时,此时需要分段导入

$fp = @fopen($file_name, "r") or die("不能打开SQL文件 $file_name");//打开文件

mysql_connect($dbhost, $dbuser, $dbpass) or die("不能连接数据库 $dbhost");//连接数据库

mysql_select_db($dbname) or die ("不能打开数据库 $dbname");//打开数据库

echo "<p>正在清空数据库,请稍等<br>";

$result = mysql_query("SHOW tables");

while ($currow=mysql_fetch_array($result))

{

mysql_query("drop TABLE IF EXISTS $currow[0]");

echo "清空数据表"$currow[0]"成功!<br>";

}

echo "<br>恭喜你清理MYSQL成功<br>";

echo "正在执行导入数据库 *** 作<br>";

// 导入数据库的MySQL命令

exec("mysql -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname < "$file_name);

echo "<br>导入完成!";

mysql_close();

}

>

备份数据库

使用mysqldump命令备份数据库

复制代码代码如下:

# 如果要将game数据库进行备份:

mysqldump -u root -p game > game_backupsql

# 如果希望备份所有的数据库:

mysqldump -u root -p --all-databases > all_backupsql

还原数据库

1、使用mysql命令还原数据库

将game_backupsql 还原至 game 数据库:

复制代码代码如下:

mysql -u root -p game < game_backupsql

2、使用source命令还原数据库

如果数据库过大,建议可以使用source命令

复制代码代码如下:

mysql> source game_backupsql

<form name="form1" method="post" action="action=bf">

<table width="90%" border="0" align=center cellpadding="5" cellspacing="1" bgcolor="#336699" class="tableBorder">

<tr>

<th width="593" height=25 bgcolor="#FFFFFF" >   <B>备份数据</B>( 需要FSO支持,FSO相关帮助请看微软网站 ) </th>

</tr>

<tr>

<td height=100 bgcolor="#FFFFFF" class="forumrow">   

<span class="STYLE1">

<%

if requestQueryString("action")="bf" then

Dbpath=requestform("Dbpath")

backpath=requestform("backpath")&"mdb"

if dbpath="" then

responsewrite "请输入您要您要备份的数据库"

else

Dbpath=servermappath(Dbpath)

end if

backpath=servermappath(backpath)

Set Fso=servercreateobject("scriptingfilesystemobject")

if fsofileexists(dbpath) then

fsocopyfile Dbpath,Backpath

responsewrite "数据备份成功!"

else

responsewrite "数据库地址不存在,请检查CONNASP中的DB=这一项是否是绝对地址!"

end if

end if%>

</span> <div align="center">    

<p>备份数据库路径(相对):

<input name=backpath type=text id="backpath" value="bak\jester_bak" size=30>

<br>

目标数据库路径(相对):

<input name=DBpath type=text id="DBpath" value="<%=db%>" size=30>

<BR>

  <BR>

  

<input name="submit" type=submit value="备份数据">

</p>

<p>必须是绝对的数据库地址<br>

</p>

</div></td>

</tr>

</table>

<p>  </p>

</form>

<form name="form1" method="post" action="action=hy">

<font color=red class="STYLE1">

<%

if requestQueryString("action")="hy" then

Dbpath=requestform("Dbpath")

backpath=requestform("backpath")

if dbpath="" then

responsewrite "请输入您要恢复成的数据库全名"

else

Dbpath=servermappath(Dbpath)

end if

backpath=servermappath(backpath)

Set Fso=servercreateobject("scriptingfilesystemobject")

if fsofileexists(dbpath) then

fsocopyfile Dbpath,Backpath

responsewrite "成功恢复数据!"

else

responsewrite "备份目录下并无您的备份文件!"

end if

end if%>

</font> <table width="80%" height="1" border="0" align=center cellpadding="5" cellspacing="1" bgcolor="#336699" class="tableBorder">

<tr>

<th width="593" height=25 bgcolor="#FFFFFF" >   <B>恢复论坛数据</B>( 需要FSO支持,FSO相关帮助请看微软网站 ) </th>

</tr>

<tr>

<td height=100 bgcolor="#FFFFFF" class="forumrow">   备份数据库路径(相对):

<input type=text size=30 name=DBpath value="bak\jester_bakMdb">

  <BR>

  目标数据库路径(相对):

<input name=backpath type=text id="backpath" value="<%=db%>" size=30>

<BR>

  填写您当前使用的数据库路径,如不想覆盖当前文件,可自行命名(注意路径是否正确),然后修改connasp文件, 如果目标文件名和当前使用数据库名一致的话,不需修改connasp文件<BR>

  

<input name="submit" type=submit value="恢复数据">

<br>

-----------------------------------------------------------------------------------------<br>

  在上面填写本程序的数据库路径全名,本程序的默认备份数据库文件为,请按照您的备份文件自行修改。<br>

  您可以用这个功能来备份您的法规数据,以保证您的数据安全!<br>

  注意:所有路径都是相对与程序空间根目录的相对路径 </td>

</tr>

</table>

<p> </p>

</form>

<span class="STYLE1">

<%

if requestQueryString("action")="ys" then

dim dbpath,boolIs97

if request("Dbpath")<>"" then Dbpath=request("Dbpath") end if

if request("Dbpath")="" then

if request("bkfolder")<>"" then bkfolder=request("bkfolder") else bkfolder="spubbsbak" end if

if request("bkdbname")<>"" then bkdbname=request("bkdbname") else bkdbname="spubbs" end if

bkdbname=bkdbname&"#asp"

Dbpath=bkfolder&"\"&bkdbname

end if

'dbpath = request("dbpath")

if request("boolIs97")<>"" then boolIs97=request("boolIs97") else boolIs97=true end if

'boolIs97 = request("boolIs97")

If dbpath <> "" Then

dbpath = servermappath(dbpath)

responsewrite(CompactDB(dbpath,boolIs97))

End If

'=====================压缩参数=========================

Function CompactDB(dbPath, boolIs97)

Dim fso, Engine, strDBPath,JET_3X

strDBPath = left(dbPath,instrrev(DBPath,"\"))

Set fso = CreateObject("ScriptingFileSystemObject")

If fsoFileExists(dbPath) Then

fsoCopyFile dbpath,strDBPath & "tempmdb"

Set Engine = CreateObject("JROJetEngine")

If boolIs97 = "True" Then

EngineCompactDatabase "Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "tempmdb", _

"Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "temp1mdb;" _

& "Jet OLEDB:Engine Type=" & JET_3X

Else

EngineCompactDatabase "Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "tempmdb", _

"Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "temp1mdb"

End If

fsoCopyFile strDBPath & "temp1mdb",dbpath

fsoDeleteFile(strDBPath & "tempmdb")

fsoDeleteFile(strDBPath & "temp1mdb")

Set fso = nothing

Set Engine = nothing

CompactDB = "你的数据库, " & dbpath & ", 已经压缩成功!" & vbCrLf

Else

CompactDB = "数据库名称或路径不正确 请重试!" & vbCrLf

End If

End Function

end if%>

</span>

<form action="action=ys" method="post">

<table width="80%" height="1" border="0" align=center cellpadding="5" cellspacing="1" bgcolor="#336699" class="tableBorder">

<tr>

<td width="581" height=25 bgcolor="#FFFFFF" class="forumrow"><b>注意:</b><br>

输入数据库所在相对路径,并且输入数据库名称(正在使用中数据库不能压缩,请选择备份数据库进行压缩 *** 作) </td>

</tr>

<tr>

<td bgcolor="#FFFFFF" class="forumrow">压缩数据库:

<input type="text" name="dbpath" value=<%=db%>>

 

<input name="submit3" type="submit" value="开始压缩"></td>

</tr>

<tr>

<td bgcolor="#FFFFFF" class="forumrow"><input type="checkbox" name="boolIs97" value="True">

如果使用 Access 97 数据库请选择 (默认为 Access 2000 数据库)<br>

<br></td>

</tr>

</table> </form>

' 名称:BackupDatabase

' 功能:备份数据库

' 控件:一个文本框和两个按钮(备份到和确定)

'

Public Sub BackupDatabase()

Dim cn As New ADODBConnection

Dim s_path, s_dataexport As String

s_path = AppPath

MeMousePointer = 11 '设置鼠标指针形状

'student1是需要备份的数据库名称

s_dataexport = "backup database student1 to disk='" + CommonDialog1FileName + "'"

cnOpen "driver={sql server};server=" & d1 & ";database=student1;persist security info=false; userid=sa" '数据库连接字符串

'这里不需要连接master数据库,即可完成备份

cnBeginTrans

cnExecute s_dataexport

ErrNumber = 0

If ErrNumber = 0 Then

cnCommitTrans

MsgBox "数据备份成功!", vbInformation, "提示"

MsgBox "数据备份文件存放路径:" & CommonDialog1FileName, vbOKOnly, "提示"

Unload Me

Else

cnRollbackTrans

MsgBox "数据备份失败!请检查数据库是否正在打开!", vbCritical, "提示"

End If

cnClose

Set cn = Nothing

MeMousePointer = 1

End Sub

以上就是关于求thinkphp 数据库的备份、还原的脚本全部的内容,包括:求thinkphp 数据库的备份、还原的脚本、如何使用命令来备份和还原MySQL数据库、如何用asp代码实现数据备份和恢复等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/10188387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存