PHP exports MySQL database and compresses

Source: Internet
Author: User
Tags ziparchive mysql backup

PHP can be a button to export MySQL backup files, and compressed storage, although phpMyAdmin has this feature, but if you develop your own website or write a CMS for others, you should not ask others to use your program to use the other phpMyAdmin backup MySQL, This should be a function of your CMS, then php How to implement a key backup MySQL data, here refers to the MySQL content is exported to SQL files, and then compressed SQL, two methods are described as follows:

The first type:

<?PHP$username= "Root";//your MySQL user name$password= "";//Password$hostname= "localhost";//MySQL server address$dbname= "Cars";//Database name$dumpfname=$dbname. "_" .Date("Y-m-d_h-i-s"). ". sql;$command= "C:\\xampp\\mysql\\bin\\mysqldump--add-drop-table--host=$hostname--user=$username";if($password)$command. = "--password=".$password." ";$command.=$dbname;$command. = ">".$dumpfname;system($command);//compress into a zip file$zipfname=$dbname. "_" .Date("Y-m-d_h-i-s"). ". Zip;$zip=Newziparchive ();if($zip->open ($zipfname, Ziparchive::CREATE)) {   $zip->addfile ($dumpfname,$dumpfname); $zip-close ();}if(file_exists($zipfname)) {    Header(' Content-description:file Transfer '); Header(' Content-type:application/octet-stream '); Header(' Content-disposition:attachment; Filename= '.basename($zipfname)); Flush(); ReadFile($zipfname); Exit;}?>

The above code can be saved as a PHP file, such as mysqlbak.php, note that this file must have write permissions. For ease of use, you can give this file a link in the background, you need to export MySQL, you just click on the backup export operation.

The second method: Do not require write permission, but do not compress the SQL file, the code is as follows:

<?PHPOb_start();$username= "Root";$password= "";$hostname= "localhost";$dbname= "Test";$command= "C:\\xampp\\mysql\\bin\\mysqldump--add-drop-table--host=$hostname--user=$username";if($password)$command. = "--password=".$password." ";$command.=$dbname;system($command);$dump=ob_get_contents();Ob_end_clean();Header(' Content-description:file Transfer ');Header(' Content-type:application/octet-stream ');Header(' Content-disposition:attachment; Filename= '.basename($dbname. "_" .Date("Y-m-d_h-i-s"). ". sql));Flush();Echo $dump;Exit();?>

Two methods you can choose one, the same can be saved to the second method of mysqlbak.php file, in the background to give a link, with convenience.

PHP exports MySQL database and compresses

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.