php語句備份數(shù)據(jù)庫,php備份數(shù)據(jù)庫代碼

php實(shí)現(xiàn)mysql數(shù)據(jù)庫分表分段備份

分卷導(dǎo)出思路:統(tǒng)計(jì)sql語句變量的長度,按1個(gè)字符當(dāng)成1

同江網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián)公司,同江網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為同江超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的同江做網(wǎng)站的公司定做!

字節(jié)比較,如果大于設(shè)定分卷大小,則寫入一個(gè)sql文件(我也不知道這樣統(tǒng)計(jì)是否穩(wěn)當(dāng),這也是借鑒其他的人的)。

分卷導(dǎo)入思路:按行讀取sql文件,將每一行當(dāng)作完整的sql語句存到數(shù)組再循環(huán)執(zhí)行插入數(shù)據(jù)庫就可以了,但是在創(chuàng)建表語句分了多行,這個(gè)需要單獨(dú)處理(就這個(gè)花了我好長時(shí)間的);

?php

//宋正河

轉(zhuǎn)載請(qǐng)注明出處

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;//這個(gè)數(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)計(jì)字段數(shù)

while($row=mysql_fetch_row($result)){

$comma

=

'';

//存儲(chǔ)逗號(hào)

$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++;

//分段

//表名

//起點(diǎn)

//跳轉(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)容了,希望大家能夠喜歡。

如何用php進(jìn)行數(shù)據(jù)備份

1、如果服務(wù)器允許mysqldump 并且沒有禁止PHP的shell_exec()這個(gè)函數(shù)的話

直接在PHP里面執(zhí)行mysqldump就可以了。

2、通過mysql_query('show tables')的返回值遍歷每個(gè)表,循環(huán)對(duì)每個(gè)表使用查詢語句

select * into outfile '路徑/文件名' from 表名

缺點(diǎn)是這樣得到的是純數(shù)據(jù),恢復(fù)數(shù)據(jù)的時(shí)候你需要額外再寫腳本

3、同樣通過show tables返回表名,遍歷每個(gè)表,通過select 語句查詢,然后逐條結(jié)果處理,比如手工添加drop table 和create table 以及insert into等等,然后再寫入文件。這樣得到的備份數(shù)據(jù)是比較接近mysqldump的結(jié)果的,各種工具都可以用來恢復(fù)數(shù)據(jù)。缺點(diǎn)是如果數(shù)據(jù)庫太大的話,效率不好說

怎樣使用PHP自動(dòng)備份數(shù)據(jù)庫

mysql數(shù)據(jù)庫的備份方式有很多;

例如:

1、使用mysqldump函數(shù)

mysqldump -u username -p dbname table1 table2 ... ? BackupName.sql

dbname參數(shù)表示數(shù)據(jù)庫的名稱

table1和table2參數(shù)表示需要備份的表的名稱,為空則整個(gè)數(shù)據(jù)庫備份;

BackupName.sql參數(shù)表設(shè)計(jì)備份文件的名稱,文件名前面可以加上一個(gè)絕對(duì)路徑。通常將數(shù)據(jù)庫被分成一個(gè)后綴名為sql的文件;

基本使用:

2、管理工具

php中數(shù)據(jù)庫怎樣備份數(shù)據(jù)庫文件

$msgs[]="服務(wù)器備份目錄為backup";

$msgs[]="對(duì)于較大的數(shù)據(jù)表,強(qiáng)烈建議使用分卷備份";

$msgs[]="只有選擇備份到服務(wù)器,才能使用分卷備份功能";

show_msg($msgs);

php 代碼備份數(shù)據(jù)庫

?php

/*

**************function?db_bak()********************

*??功能:備份指定的數(shù)據(jù)庫中的指定表

*??輸入(按傳入?yún)?shù)解釋):$db_name?要備份的數(shù)據(jù)庫,$tb_array要備份的表,數(shù)組形式的,如果不是數(shù)組,將備份整個(gè)庫。

$dir輸出備份文件的目錄,$filename備份文件名,$block_num指定分塊大?。ㄖ敢粭linsert語句包含幾天數(shù)據(jù))

*??輸出:備份的數(shù)據(jù)表的sql語句文件,可用于還原

*/

function?db_bak($mylink,$dbname,$tab_array="",$dir,$filename,$block_num=20){

mysql_select_db($dbname,$mylink)?or?db_error(mysql_error());

is_array($tab_array)?or?$tab_array=db_showtb($mylink,$dbname,true);?//如果$tab_array不是數(shù)組,則取得整個(gè)庫的表

if($dir){

if(!file_exists($dir)){

$flag=mkdir($dir);

if(!$flag)

return?2;

}

}

//檢查數(shù)據(jù)表是否存在

foreach($tab_array?as?$tab)

db_e_tab($mylink,$dbname,$tab)?or?db_error($tab."?is?not?exist!");

$fp=fopen($dir.$filename,"w");

foreach($tab_array?as?$tab){??

fputs($fp,"DROP?TABLE?IF?EXISTS?".$tab.";\r\n");

$tabdef_array=mysql_fetch_array(mysql_query("SHOW?CREATE?TABLE?".$tab,$mylink));

fputs($fp,str_replace("\n","",$tabdef_array["Create?Table"]).";\r\n");?

fputs($fp,"LOCK?TABLES?".$tab."?WRITE;\r\n");?

$getvalue_result=mysql_query("select?*?from?".$tab,$mylink);

if(mysql_num_rows($getvalue_result)){

$i=1;??

$p="";

$start_flag=true;

while($getvalue_array=mysql_fetch_row($getvalue_result)){

$values=join("','",array_map('db_data_check',$getvalue_array));

$start_flag??fputs($fp,"insert?into?".$tab."?values");

if($i$block_num){

fputs($fp,",('".$values."');\r\n");

$start_flag=true;

$i=0;

}else{

fputs($fp,$p."('".$values."')");

$start_flag=false;

$i++;

}

$p=$start_flag?"":",";?

}

$start_flag?or?fputs($fp,";\r\n");

}

fputs($fp,"UNLOCK?TABLES;\r\n");??

}

fclose($fp);??

}

/*

**************function?db_in()********************

*??功能:還原備份函數(shù)db_bak生成的數(shù)據(jù)庫文件

*??輸入(按傳入?yún)?shù)解釋):$db_name?要備份的數(shù)據(jù)庫,$filename要還原的數(shù)據(jù)庫文件

*??輸出:將數(shù)據(jù)還原到數(shù)據(jù)庫

*??原理:將文件分割成一個(gè)數(shù)組,然后逐條將數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫

*??警告:執(zhí)行該函數(shù)會(huì)刪除原來的數(shù)據(jù)表

*/

function?db_in($mylink,$dbname,$filename){

if(!file_exists($filename)){

return?0;

}

db_e_db($mylink,$dbname)?or?(mysql_query("create?database?".$dbname,$mylink)or?db_error(mysql_error()));

mysql_select_db($dbname,$mylink);

$sql_array=file($filename);

foreach($sql_array?as?$sql){

mysql_query(trim($sql),$mylink)?or?db_error(mysql_error());

}

}

??

//我自己寫的??梢詤⒖脊?/p>

網(wǎng)頁題目:php語句備份數(shù)據(jù)庫,php備份數(shù)據(jù)庫代碼
標(biāo)題來源:http://www.muchs.cn/article4/hcppoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)服務(wù)器托管、App開發(fā)、自適應(yīng)網(wǎng)站建站公司、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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í)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)