Backup mysql using PHP

Published by

Posted on December 16, 2007

Execute a database backup query from PHP file.
Run mysqldump using system() function.
Use phpMyAdmin to do the backup.

Execute a database backup query from PHP file
Below is an example of using SELECT INTO OUTFILE query for creating table backup :

To restore the backup you just need to run LOAD DATA INFILE query like this :

It’s a good idea to name the backup file as tablename.sql so you’ll know from which table the backup file is

Run mysqldump using system() function
The system() function is used to execute an external program. Because MySQL already have built in tool for creating MySQL database backup (mysqldump) let’s use it from our PHP script

$backupFile”;
system($command);

include ‘closedb.php’;
?>