PHP備份/還原MySQL數(shù)據(jù)庫的代碼
更新時(shí)間:2011年01月06日 21:51:50 作者:
之前是采用PHP讀取數(shù)據(jù)庫結(jié)構(gòu)和內(nèi)容,然后寫文件,這樣可能會(huì)導(dǎo)致導(dǎo)出的文件不一定能百分百導(dǎo)入到MySQL中去,想想之后采用MySQL自帶的導(dǎo)入導(dǎo)出命令最保險(xiǎn)
以下是代碼:
一、備份數(shù)據(jù)庫并下載到本地【db_backup.php】
<?php
// 設(shè)置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");
// 獲取當(dāng)前頁面文件路徑,SQL文件就導(dǎo)出到此文件夾內(nèi)
$tmpFile = (dirname(__FILE__))."\\".$filename;
// 用MySQLDump命令導(dǎo)出數(shù)據(jù)庫
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;
?>
二、還原數(shù)據(jù)庫【db_restore.php】
<form id="form1" name="form1" method="post" action="">
【數(shù)據(jù)庫SQL文件】:<input id="sqlFile" name="sqlFile" type="file" />
<input id="submit" name="submit" type="submit" value="還原" />
</form>
<?php
// 我的數(shù)據(jù)庫信息都存放到config.php文件中,所以加載此文件,如果你的不是存放到該文件中,注釋此行即可;
require_once((dirname(__FILE__).'/../../include/config.php'));
if ( isset ( $_POST['sqlFile'] ) )
{
$file_name = $_POST['sqlFile']; //要導(dǎo)入的SQL文件名
$dbhost = $cfg_dbhost; //數(shù)據(jù)庫主機(jī)名
$dbuser = $cfg_dbuser; //數(shù)據(jù)庫用戶名
$dbpass = $cfg_dbpwd; //數(shù)據(jù)庫密碼
$dbname = $cfg_dbname; //數(shù)據(jù)庫名
set_time_limit(0); //設(shè)置超時(shí)時(shí)間為0,表示一直執(zhí)行。當(dāng)php在safe mode模式下無效,此時(shí)可能會(huì)導(dǎo)致導(dǎo)入超時(shí),此時(shí)需要分段導(dǎo)入
$fp = @fopen($file_name, "r") or die("不能打開SQL文件 $file_name");//打開文件
mysql_connect($dbhost, $dbuser, $dbpass) or die("不能連接數(shù)據(jù)庫 $dbhost");//連接數(shù)據(jù)庫
mysql_select_db($dbname) or die ("不能打開數(shù)據(jù)庫 $dbname");//打開數(shù)據(jù)庫
echo "<p>正在清空數(shù)據(jù)庫,請(qǐng)稍等....<br>";
$result = mysql_query("SHOW tables");
while ($currow=mysql_fetch_array($result))
{
mysql_query("drop TABLE IF EXISTS $currow[0]");
echo "清空數(shù)據(jù)表【".$currow[0]."】成功!<br>";
}
echo "<br>恭喜你清理MYSQL成功<br>";
echo "正在執(zhí)行導(dǎo)入數(shù)據(jù)庫操作<br>";
// 導(dǎo)入數(shù)據(jù)庫的MySQL命令
exec("mysql -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname < ".$file_name);
echo "<br>導(dǎo)入完成!";
mysql_close();
}
?>
一、備份數(shù)據(jù)庫并下載到本地【db_backup.php】
復(fù)制代碼 代碼如下:
<?php
// 設(shè)置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");
// 獲取當(dāng)前頁面文件路徑,SQL文件就導(dǎo)出到此文件夾內(nèi)
$tmpFile = (dirname(__FILE__))."\\".$filename;
// 用MySQLDump命令導(dǎo)出數(shù)據(jù)庫
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;
?>
二、還原數(shù)據(jù)庫【db_restore.php】
復(fù)制代碼 代碼如下:
<form id="form1" name="form1" method="post" action="">
【數(shù)據(jù)庫SQL文件】:<input id="sqlFile" name="sqlFile" type="file" />
<input id="submit" name="submit" type="submit" value="還原" />
</form>
<?php
// 我的數(shù)據(jù)庫信息都存放到config.php文件中,所以加載此文件,如果你的不是存放到該文件中,注釋此行即可;
require_once((dirname(__FILE__).'/../../include/config.php'));
if ( isset ( $_POST['sqlFile'] ) )
{
$file_name = $_POST['sqlFile']; //要導(dǎo)入的SQL文件名
$dbhost = $cfg_dbhost; //數(shù)據(jù)庫主機(jī)名
$dbuser = $cfg_dbuser; //數(shù)據(jù)庫用戶名
$dbpass = $cfg_dbpwd; //數(shù)據(jù)庫密碼
$dbname = $cfg_dbname; //數(shù)據(jù)庫名
set_time_limit(0); //設(shè)置超時(shí)時(shí)間為0,表示一直執(zhí)行。當(dāng)php在safe mode模式下無效,此時(shí)可能會(huì)導(dǎo)致導(dǎo)入超時(shí),此時(shí)需要分段導(dǎo)入
$fp = @fopen($file_name, "r") or die("不能打開SQL文件 $file_name");//打開文件
mysql_connect($dbhost, $dbuser, $dbpass) or die("不能連接數(shù)據(jù)庫 $dbhost");//連接數(shù)據(jù)庫
mysql_select_db($dbname) or die ("不能打開數(shù)據(jù)庫 $dbname");//打開數(shù)據(jù)庫
echo "<p>正在清空數(shù)據(jù)庫,請(qǐng)稍等....<br>";
$result = mysql_query("SHOW tables");
while ($currow=mysql_fetch_array($result))
{
mysql_query("drop TABLE IF EXISTS $currow[0]");
echo "清空數(shù)據(jù)表【".$currow[0]."】成功!<br>";
}
echo "<br>恭喜你清理MYSQL成功<br>";
echo "正在執(zhí)行導(dǎo)入數(shù)據(jù)庫操作<br>";
// 導(dǎo)入數(shù)據(jù)庫的MySQL命令
exec("mysql -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname < ".$file_name);
echo "<br>導(dǎo)入完成!";
mysql_close();
}
?>
您可能感興趣的文章:
- MySQL數(shù)據(jù)庫備份與恢復(fù)方法
- mysql備份的三種方式詳解
- mysqldump備份還原和mysqldump導(dǎo)入導(dǎo)出語句大全詳解
- 用mysqldump備份和恢復(fù)指定表的方法
- 史上最全的MySQL備份方法
- Navicat異地自動(dòng)備份MySQL方法詳解(圖文)
- 詳解Mysql自動(dòng)備份與恢復(fù)的幾種方法(圖文教程)
- Linux下實(shí)現(xiàn)MySQL數(shù)據(jù)備份和恢復(fù)的命令使用全攻略
- 批處理命令 BAT備份MySQL數(shù)據(jù)庫
- 如何優(yōu)雅安全的備份MySQL數(shù)據(jù)
相關(guān)文章
php實(shí)現(xiàn)指定字符串中查找子字符串的方法
這篇文章主要介紹了php實(shí)現(xiàn)指定字符串中查找子字符串的方法,涉及php中strpos()函數(shù)查找字符串的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03PHP的array_diff()函數(shù)在處理大數(shù)組時(shí)的效率問題
PHP 5.2.6 以上版本的 array_diff() 函數(shù)在處理大數(shù)組時(shí),需要花費(fèi)超長時(shí)間,這個(gè) bug 已經(jīng)被官方確認(rèn);在這個(gè)問題被修復(fù)之前或者在我們不能控制 PHP 版本的時(shí)候,可以使用本文提供的方法2011-11-11PHP警告Cannot use a scalar value as an array的解決方法
PHP警告Cannot use a scalar value as an array的解決方法,需要的朋友可以參考下。2012-01-01