一、備份數(shù)據(jù)庫(kù)并下載到本地【db_backup.php】

成都創(chuàng)新互聯(lián)長(zhǎng)期為1000多家客戶(hù)提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為南雄企業(yè)提供專(zhuān)業(yè)的網(wǎng)站建設(shè)、網(wǎng)站制作,南雄網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。
復(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)前頁(yè)面文件路徑,SQL文件就導(dǎo)出到此文件夾內(nèi)
$tmpFile = (dirname(__FILE__))."\\".$filename;
// 用MySQLDump命令導(dǎo)出數(shù)據(jù)庫(kù)
exec("mysqldump -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname ".$tmpFile);
$file = fopen($tmpFile, "r"); // 打開(kāi)文件
echo fread($file,filesize($tmpFile));
fclose($file);
exit;
?
二、還原數(shù)據(jù)庫(kù)【db_restore.php】
復(fù)制代碼 代碼如下:
form id="form1" name="form1" method="post" action=""
【數(shù)據(jù)庫(kù)SQL文件】:input id="sqlFile" name="sqlFile" type="file" /
input id="submit" name="submit" type="submit" value="還原" /
/form
?php
// 我的數(shù)據(jù)庫(kù)信息都存放到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ù)庫(kù)主機(jī)名
$dbuser = $cfg_dbuser; //數(shù)據(jù)庫(kù)用戶(hù)名
$dbpass = $cfg_dbpwd; //數(shù)據(jù)庫(kù)密碼
$dbname = $cfg_dbname; //數(shù)據(jù)庫(kù)名
set_time_limit(0); //設(shè)置超時(shí)時(shí)間為0,表示一直執(zhí)行。當(dāng)php在safe mode模式下無(wú)效,此時(shí)可能會(huì)導(dǎo)致導(dǎo)入超時(shí),此時(shí)需要分段導(dǎo)入
$fp = @fopen($file_name, "r") or die("不能打開(kāi)SQL文件 $file_name");//打開(kāi)文件
mysql_connect($dbhost, $dbuser, $dbpass) or die("不能連接數(shù)據(jù)庫(kù) $dbhost");//連接數(shù)據(jù)庫(kù)
mysql_select_db($dbname) or die ("不能打開(kāi)數(shù)據(jù)庫(kù) $dbname");//打開(kāi)數(shù)據(jù)庫(kù)
echo "p正在清空數(shù)據(jù)庫(kù),請(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ù)庫(kù)操作br";
// 導(dǎo)入數(shù)據(jù)庫(kù)的MySQL命令
exec("mysql -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname ".$file_name);
echo "br導(dǎo)入完成!";
mysql_close();
}
?
有很多軟件可以使用,比如phpmyadmin,sqlyog等等
下載一個(gè)phpmyadmin并且配置好(網(wǎng)上有如何配置),其中就有備份還原數(shù)據(jù)庫(kù)的圖標(biāo),很簡(jiǎn)單
補(bǔ)充:----------------------
對(duì)啊,點(diǎn)導(dǎo)出,然后執(zhí)行就可以了啊
一、備份數(shù)據(jù)庫(kù)并下載到本地【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)前頁(yè)面文件路徑,SQL文件就導(dǎo)出到此文件夾內(nèi)
$tmpFile = (dirname(__FILE__))."\\".$filename;
// 用MySQLDump命令導(dǎo)出數(shù)據(jù)庫(kù)
exec("mysqldump -u$cfg_dbuser -p$cfg_dbpwd --default-character-set=utf8 $cfg_dbname ".$tmpFile);
$file = fopen($tmpFile, "r"); // 打開(kāi)文件
echo fread($file,filesize($tmpFile));
fclose($file);
exit;
?
二、還原數(shù)據(jù)庫(kù)【db_restore.php】
代碼代碼如下:
form id="form1" name="form1" method="post" action=""
【數(shù)據(jù)庫(kù)SQL文件】:input id="sqlFile" name="sqlFile" type="file" /
input id="submit" name="submit" type="submit" value="還原" /
/form
?php
// 我的數(shù)據(jù)庫(kù)信息都存放到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ù)庫(kù)主機(jī)名
$dbuser = $cfg_dbuser; //數(shù)據(jù)庫(kù)用戶(hù)名
$dbpass = $cfg_dbpwd; //數(shù)據(jù)庫(kù)密碼
$dbname = $cfg_dbname; //數(shù)據(jù)庫(kù)名
set_time_limit(0); //設(shè)置超時(shí)時(shí)間為0,表示一直執(zhí)行。當(dāng)php在safe mode模式下無(wú)效,此時(shí)可能會(huì)導(dǎo)致導(dǎo)入超時(shí),此時(shí)需要分段導(dǎo)入
$fp = @fopen($file_name, "r") or die("不能打開(kāi)SQL文件 $file_name");//打開(kāi)文件
mysql_connect($dbhost, $dbuser, $dbpass) or die("不能連接數(shù)據(jù)庫(kù) $dbhost");//連接數(shù)據(jù)庫(kù)
mysql_select_db($dbname) or die ("不能打開(kāi)數(shù)據(jù)庫(kù) $dbname");//打開(kāi)數(shù)據(jù)庫(kù)
echo "p正在清空數(shù)據(jù)庫(kù),請(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ù)庫(kù)操作br";
// 導(dǎo)入數(shù)據(jù)庫(kù)的MySQL命令
exec("mysql -u$cfg_dbuser -p$cfg_dbpwd $cfg_dbname ".$file_name);
echo "br導(dǎo)入完成!";
mysql_close();
}
?
網(wǎng)站名稱(chēng):php數(shù)據(jù)庫(kù)備份恢復(fù),mysql數(shù)據(jù)庫(kù)備份恢復(fù)
網(wǎng)頁(yè)鏈接:http://www.chinadenli.net/article28/hedgjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷(xiāo)、ChatGPT、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站導(dǎo)航、網(wǎng)站改版、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)