Implementation of PHP database volume-based export and backup

Source: Internet
Author: User
Tags php database

Many systems, such as dedeCMS and phpCMS, have been used for reference when writing this function. Unfortunately, they all have bugs and they cannot control the volume size, there is always a thought:

// If you want to publish this article, please indicate the exit, no outgoing copyright notice, I do not like to see this kind of website. My work website does not indicate the person who is serving Seven {See7di # Gmail.com}
$ Work = StrToLower (_ r ($ _ GET ["work"]);
Switch ($ work ){
Case "export ":
_ Export ();
Break;
Case "exportloop ":
_ ExportLoop ();
Break;
Case "tozip ":
_ Tozip ();
Break;
}
/*---------------------------------*/
Function _ Export (){
Global $ Mysql, $ work;
$ F1 = _ r ($ _ GET ["f1"]);

// If it is the first loop
IF ($ f1 = "1 "){
// First obtain whether the backup table is specified
$ Tables = $ _ REQUEST ["tables"];
Echo ($ tables );
IF (Empty ($ tables )){
// Obtain the data table
$ Result = $ Mysql-> View ("Show Tables From". BLK_SysDbname ."");
Foreach ($ Result as $ val ){
$ Tables [] = $ val [0];
}
} Else {
$ Tables = Explode (",", $ tables );
$ Tables = Array_Filter ($ tables, '_ nul ');
}
$ _ SESSION ['tab'] = $ tables;

// Initialize the sequence number of the data table
$ NList = 0;
// Initialization file No.
$ NFile = 1;
// Used for comparison to check whether the table creation information has been written.
$ _ SESSION ['nlist'] = ($ nList-1 );
} Else {
$ NList = _ r ($ _ GET ["nList"]);
$ NFile = _ r ($ _ GET ["nFile"]);
}
$ ISql = ''; // captured content
$ FPath = "Cache/"; // Save the Directory
$ CName = "DB_Export. SQL"; // cache file
$ _ Tab = $ _ SESSION ['tab'] [$ nList]; // the table currently operated
$ FName = "DB". Date ('_ md _'). $ nFile. ". SQL ";

// Determine the directory status
IF (! Is_writable ($ fPath) {Exit ("data cannot be backed up to the server! Check whether the ". $ fPath." directory is writable. ");}

// Determine whether the backup is complete
IF (Count ($ _ SESSION ['tab']) <= $ nList ){
@ Unlink ($ fPath. $ cName );
For ($ I = 1; $ I <= $ nFile; $ I ++ ){
$ FileArr [] = $ fPath. "DB". Date ('_ md _'). $ I. ". SQL ";
}
$ _ FileName = "DB". Date ('_ md'). ". zip ";
Tozip ($ FileArr, $ _ FileName, 2, 2 );

Alert ("","? Work = backdb & rback = 1 ");
Die ();
}

// Read a table to obtain the header information and table creation information.
IF ($ _ SESSION ['nlist'] <$ nList ){
IF ($ nFile = 1 And $ nList = 0 ){
// Delete an object
For ($ I = 1; I I <1000; $ I ++ ){
IF (File_exists ($ fPath. "DB". Date ('_ md _'). $ I. ". SQL ")){
@ Unlink ($ fPath. "DB". Date ('_ md _'). $ I. ". SQL ");
} Else {
Break;
}
}
// Create a file
File_put_contents ($ fPath. $ fName ,"");
File_put_contents ($ fPath. $ cName ,"");
$ ISql = "# Create ". BLK_SysName. "\ n # Create time ". date ('Y-m-d '). "\ n # Mysql Version ". $ Mysql-> Version (). "\ n # PHP Version ". @ phpversion (). "\ n # ------------------------------------------------------ \ n \ nSET SQL _MODE = \" NO_AUTO_VALUE_ON_ZERO \ "; \ n ";
}
$ ISql. = "drop table if exists '". $ _ Tab. "'; \ n ";
$ _ ISql = Mysql_fetch_row (mysql_query ("show create table". $ _ Tab .""));
$ ISql. = $ _ iSql [1]. "; \ n ";
}

// Write header information
$ ISql = Trim (File_get_contents ($ fPath. $ fName). "\ n". $ iSql );
File_put_contents ($ fPath. $ fName, $ iSql );

// Obtain the cached content and determine whether it is null. If it is null, capture all the content of the current table and put it into the cache.
$ _ Cache = Trim (File_get_contents ($ fPath. $ cName ));
IF (Empty ($ _ Cache )){
$ _ Cache = DropInfo ($ _ Tab );
File_put_contents ($ fPath. $ cName, $ _ Cache );
}

// Capture the file cyclically until the backup file is full (because the file size cannot be obtained in a timely manner)
Alert ("","? Work = exportloop & nList = ". $ nList." & nFile = ". $ nFile." & Session = ". $ Session." "," ", 10 );
}
/*---------------------------------*/
Function _ ExportLoop (){
Global $ work;
$ NList = _ r ($ _ GET ["nList"]);
$ NFile = _ r ($ _ GET ["nFile"]);
$ ISql = ''; // captured content
$ FPath = "Cache/"; // Save the Directory
$ FSize = (BLK_SysDbsplit * 1024); // file size, 2048 K = 2 M
$ FName = "DB". Date ('_ md _'). $ nFile. ". SQL ";
$ CName = "DB_Export. SQL ";

// Display progress
Echo "<br> <strong> the backup is in progress. Please wait. <span style = 'color: red; '>" is being generated ". $ fName. "</span> </strong> <br> ";
Echo "<br>". (FileSize ($ fPath. $ fName). "<br>". $ fSize. "<br> ";
Echo Loading (FileSize ($ fPath. $ fName), $ fSize );

// If the backup file is full
IF (FileSize ($ fPath. $ fName) + 9000)> $ fSize ){
$ NFile ++; // backup file serial number plus one
$ _ SESSION ['nlist'] = $ nList; // prevents the table creation information from being written again.
Alert ("","? Work = export & nList = ". $ nList." & nFile = ". $ nFile." & Session = ". $ Session." "," ", 30 );
} Else {
// Obtain the cached content and compile it as an array
$ _ Cache = (File ($ fPath. $ cName ));

// Capture the required data for the backup file, 4 entries each time
$ _ Info = Array ();
For ($ I = 0; $ I <2; $ I ++ ){
$ _ Info [] = $ _ Cache [$ I];
}

// Get new cache data
$ _ Cache = array_diff_key ($ _ Cache, $ _ Info );
File_put_contents ($ fPath. $ cName, Trim (Implode ("", $ _ Cache )));

// Refresh and write the backup file
$ ISql = Trim (File_get_contents ($ fPath. $ fName). "\ n". Trim (Implode ("", $ _ Info ));
File_put_contents ($ fPath. $ fName, $ iSql );

// If the backup file is not full and the cache is empty, enter the next table
IF (Count ($ _ Cache) <1 ){
$ NList ++;
$ _ SESSION ['nlist'] = ($ nList-1 );
Alert ("","? Work = export & nList = ". $ nList." & nFile = ". $ nFile." & Session = ". $ Session." "," ", 10 );
// Loop this function if the backup file is not full and the cache is not empty
} Else {
Alert ("","? Work = exportloop & nList = ". $ nList." & nFile = ". $ nFile." & Session = ". $ Session." "," ", 10 );
}
}
}

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.