一、備份數(shù)據(jù)庫并下載到本地【db_backup.php】
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、阿爾山網(wǎng)絡(luò)推廣、微信小程序開發(fā)、阿爾山網(wǎng)絡(luò)營銷、阿爾山企業(yè)策劃、阿爾山品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供阿爾山建站搭建服務(wù),24小時服務(wù)熱線:18982081108,官方網(wǎng)址:muchs.cn
代碼代碼如下:
?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è)置超時時間為0,表示一直執(zhí)行。當(dāng)php在safe mode模式下無效,此時可能會導(dǎo)致導(dǎo)入超時,此時需要分段導(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ù)庫,請稍等....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();
}
?
使用mysqldump函數(shù)
mysqldump -u username -p dbname table1 table2 ... ? BackupName.sql
dbname參數(shù)表示數(shù)據(jù)庫的名稱
table1和table2參數(shù)表示需要備份的表的名稱,為空則整個數(shù)據(jù)庫備份;
BackupName.sql參數(shù)表設(shè)計備份文件的名稱,文件名前面可以加上一個絕對路徑。通常將數(shù)據(jù)庫被分成一個后綴名為sql的文件。
備份數(shù)據(jù)庫的主要過程:
切換到對應(yīng)的數(shù)據(jù)庫;
使用show create table ?tableName,獲得表結(jié)構(gòu),寫到文件中;
然后查詢所有的表數(shù)據(jù),循環(huán)生成相對應(yīng)sql語句,寫到文件中;
試運行生成的sql文件。
分卷導(dǎo)出思路:統(tǒng)計sql語句變量的長度,按1個字符當(dāng)成1
字節(jié)比較,如果大于設(shè)定分卷大小,則寫入一個sql文件(我也不知道這樣統(tǒng)計是否穩(wěn)當(dāng),這也是借鑒其他的人的)。
分卷導(dǎo)入思路:按行讀取sql文件,將每一行當(dāng)作完整的sql語句存到數(shù)組再循環(huán)執(zhí)行插入數(shù)據(jù)庫就可以了,但是在創(chuàng)建表語句分了多行,這個需要單獨處理(就這個花了我好長時間的);
?php
//宋正河
轉(zhuǎn)載請注明出處
set_time_limit(0);
header('content-type:text/html;charset=utf-8');
mysql_connect('localhost','root','root');
mysql_select_db('test');
$table_array=get_tables('test');
mysql_query('set
names
utf8');
$filesize=1024*1024*4;
$start=$_GET['start']?$_GET['start']:0;
$part=$_GET['part']?$_GET['part']:'1';
$table_index=$_GET['table_index']?$_GET['table_index']:'0';
$table=$table_array[$table_index];
$num=200000000;//這個數(shù)要足夠大,可以是總記錄數(shù)
$backupdata='';
if($start=='0'){
$query="SHOW
CREATE
TABLE
`{$table}`";
$result
=
mysql_query($query);
$row
=
mysql_fetch_row($result);
$backupdata
.=
"DROP
TABLE
IF
EXISTS
`{$table}`;\n"
.
$row[1]
.
";\n\n";
}
$limit=($start=='0')?'':"
limit
$start,$num
";
$query="select
*
from
`{$table}`
$limit
";
$result=mysql_query($query);
$numfields
=
mysql_num_fields($result);
//統(tǒng)計字段數(shù)
while($row=mysql_fetch_row($result)){
$comma
=
'';
//存儲逗號
$backupdata_tmp
=
"INSERT
INTO
`{$table}`
VALUES
(";
for($i=0;
$i$numfields;
$i++){
$backupdata_tmp
.=
$comma
.
"'"
.
mysql_escape_string($row[$i])
.
"'";
$comma
=
',';
}
$backupdata_tmp
.=
");\n";
if(strlen($backupdata)+strlen($backupdata_tmp)
$filesize){
//寫入文件并跳轉(zhuǎn)
$file='data/'.$table.'-'.$part.'.sql';
file_put_contents($file,$backupdata);
echo
$file.'
備份完成,程序繼續(xù)進(jìn)行!';
$part++;
//分段
//表名
//起點
//跳轉(zhuǎn)
sleep(3);
echo
"scriptlocation.href='?start={$start}table_index={$table_index}part={$part}';/script";
exit;
}
$backupdata.=$backupdata_tmp;
$start++;
}
if($backupdata){
$file='data/'.$table.'-'.$part.'.sql';
file_put_contents($file,$backupdata);
}
echo
$table.'備份完成!br
/';
sleep(2);
$table_index++;
if($table_array[$table_index]){
echo
"scriptlocation.href='?table_index={$table_index}';/script";
exit;
}else{
echo
'恭喜你,數(shù)據(jù)庫備份完畢!';
}
function
get_tables($db){
$tq
=
mysql_list_tables($db);
while($tr
=
mysql_fetch_row($tq)){
$arrtb[]
=
$tr[0];
}
return
$arrtb;
}
?
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
$msgs[]="服務(wù)器備份目錄為backup";
$msgs[]="對于較大的數(shù)據(jù)表,強(qiáng)烈建議使用分卷備份";
$msgs[]="只有選擇備份到服務(wù)器,才能使用分卷備份功能";
show_msg($msgs);
文章名稱:php數(shù)據(jù)庫備份表 php 備份數(shù)據(jù)庫
URL鏈接:http://muchs.cn/article32/hjsipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、服務(wù)器托管、移動網(wǎng)站建設(shè)、面包屑導(dǎo)航、網(wǎng)站內(nèi)鏈、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容