分卷導出思路:統計sql語句變量的長度,按1個字符當成1 字節比較,如果大于設定分卷大小,則寫入一個sql文件(我也不知道這樣統計是否穩當,這也是借鑒其他的人的)。
分卷導入思路:按行讀取sql文件,將每一行當作完整的sql語句存到數組再循環執行插入數據庫就可以了,但是在創建表語句分了多行,這個需要單獨處理(就這個花了我好長時間的);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
<?php //宋正河 轉載請注明出處 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; //這個數要足夠大,可以是總記錄數 $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 ); //統計字段數 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 ){ //寫入文件并跳轉 $file = 'data/' . $table . '-' . $part . '.sql' ; file_put_contents ( $file , $backupdata ); echo $file . ' 備份完成,程序繼續進行!' ; $part ++; //分段 //表名 //起點 //跳轉 sleep(3); echo "<script>location.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 "<script>location.href='?table_index={$table_index}';</script>" ; exit ; } else { echo '恭喜你,數據庫備份完畢!' ; } function get_tables( $db ){ $tq = mysql_list_tables( $db ); while ( $tr = mysql_fetch_row( $tq )){ $arrtb [] = $tr [0]; } return $arrtb ; } ?> |
以上所述就是本文的全部內容了,希望大家能夠喜歡。