PHP backup MySQL Database

Source: Internet
Author: User

1 Set_time_limit(0);2 3 4 //Library name5 $db= ' dbname ';6 7 8 9 /********** connecting MySQL database **********/Ten  One $dsn= ' mysql:dbname= '.$db.‘; host=127.0.0.1 '; A  - $user= ' Root '; -  the $password= ' Root '; -  -  - Try{ +  -     $pdo=NewPDO ($dsn,$user,$password); +  A     $pdo-exec(' SET NAMES UTF8 '); at  -     //Catching exceptions -     $pdo->setattribute (pdo::attr_errmode,2); -  -}Catch(pdoexception$e){ -  in     Echo' Error: '.$e-getMessage (); -  to } +  - //querying more than one piece of data the functionQuery$sql){ *  $     Global $pdo;Panax Notoginseng  -     $res=$pdo->prepare ($sql); the     $res-execute (); +     $data=$res->fetchall (PDO::FETCH_ASSOC); A  the     return $data; +  - } $  $  - /********** Backup Code **********/ -  the  - //all table names under query libraryWuyi $sql=sprintf(' SHOW TABLES from%s ',$db); the $tablesData= Query ($sql); -  Wu //is there a table in the library - if(Count($tablesData) < 1){ About  $     Exit($db. ' No data table in library '); - } -  - //Backup Storage Path A $dirPath= ' Backup '; +  the //determine if a directory exists - if(!file_exists($dirPath)){ $  the     $res=mkdir($dirPath); the     if($res!==true){ the  the         Exit(' Directory Creation failed 1 '); -     } in } the  the //Current Date About $toDir=Date("Ymd", Time()); the  the //backup directory for the current date the $toDir=$dirPath.‘ /‘.$toDir; +  - //whether the current date directory exists the if(!file_exists($toDir)){Bayi  the     //Create a directory with the current date name the     $res=mkdir($toDir); -     if($res!==true){ -  the         Exit(' Directory Creation failed 2 '); the     } the } the  - //If the directory has a file name that generates a backup the if(file_exists($toDir)){ the  the     $fileName=$toDir.‘ /‘.Date("Y-m-d_h-i-s", Time()).‘. sql;94  the}Else{ the  the     Exit(' Directory Creation failed 3 ');98 } About  - //Cyclic data101 foreach($tablesData  as $key=$val){102 103     //Table Key Name104     $tableKey= ' Tables_in_ '.$db; the 106     //Get Table Structure107     $sql=sprintf(' SHOW CREATE TABLE%s ',$val[$tableKey]);108     $tableBody[$key] = Query ($sql);109  the     //write SQL file to local (write table name)111     Error_log("\n\n\n#".$tableBody[$key][0][' Table '],3,$fileName); the 113     //if present, delete the table first the     $delTab= ' DROP TABLE IF EXISTS '.$tableBody[$key][0][' Table ']. ' ;; the     Error_log("\ n".)$delTab, 3,$fileName); the 117     //write the table structure at the end of multiple output one; (Semicolon-terminator of SQL)118     Error_log("\ n".)$tableBody[$key][0][' Create Table ']. "; N\n ", 3,$fileName);119  -     //Querying table Data121     $sql=sprintf(' SELECT * from%s ',$val[$tableKey]);122     $tableData= Query ($sql);123 124     //each piece of data in the looping table the     foreach($tableData  as $k=$v){126 127         $sqlHeadStr= ' INSERT into '.$val[$tableKey]; -         $keyStr= ' ';129         $valStr= ' '; the 131         //the key and value of the loop insert the         foreach($v  as $kk=$VV){133 134             //splicing Key135             $keyStr. = ".$kk.‘ ,;136             //Stitching Value137             $valStr. = ' \ '.$VV.‘ \‘,‘;138         }139  $         //Remove the right extra comma141         $keyStr=Trim($keyStr,",");142         $valStr=Trim($valStr,",");143 144         //add parentheses to key values145         $keyStr= ' ('.$keyStr.‘)‘;146         $valStr= ' ('.$valStr.‘)‘;147 148         //stitching Full SQL149         $sqlStr=$sqlHeadStr.$keyStr.‘ VALUES '.$valStr.‘;‘; Max 151         //Write insert statement to file the         Error_log($sqlStr." \ n ", 3,$fileName);153     }154 }155 156 Echo' OK ';157 Exit;

PHP backup MySQL Database

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.