perform-mysql-backup-php

perform-mysql-backup-php

There are at least three ways to backup your MySQL Database :

1. Execute a database backup query from PHP file.
2. Run mysqldump using system() function.
3. 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’;
?>

Use phpMyAdmin to do the backup

This option as you may guessed doesn’t involve any programming on your part. However I think i mention it anyway so you know more options to backup your database.

To backup your MySQL database using phpMyAdmin click on the “export” link on phpMyAdmin main page. Choose the database you wish to backup, check the appropriate SQL options and enter the name for the backup file