Php+mysql Database sub-table segmented Backup program--Song Jong

Source: Internet
Author: User

<?php

Song Jong Reprint Please indicate the source

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;//This number to be large enough to be the total number of records

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

}

$limit = ($start = = ' 0 ')? ":" Limit $start, $num ";

$query = "Select * from ' {$table} ' $limit";

$result =mysql_query ($query);

$numfields = Mysql_num_fields ($result); Number of statistics fields

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

$comma = "; Storing commas

$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) {

Write to file and jump

$file = ' data/'. $table. ' -'. $part. ' SQL ';

File_put_contents ($file, $backupdata);

echo $file. ' Backup complete, the program continues! ‘;

$part + +;

Segmentation

Table name

Starting point

Jump

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. ' Backup Complete! <br/> ';

Sleep (2);

$table _index++;

if ($table _array[$table _index]) {

echo "<script>location.href="? table_index={$table _index} ';</script> ';

Exit

}else{

Echo ' Congratulations, database backup is complete! ‘;

}

function Get_tables ($db) {

$TQ = Mysql_list_tables ($db);

while ($tr = Mysql_fetch_row ($TQ)) {

$ARRTB [] = $tr [0];

}

return $ARRTB;

}

?>

Php+mysql Database sub-table segmented Backup program--Song Jong

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.