Export MySQL Database Using PHP

How to export and import mysql database with its data using php script?

http://www.php-mysql-tutorial.com/wikis/mysql-tutorials/using-php-to-backup-mysql-databases.aspx

either

$tableName  = 'mypet';
$backupFile = 'backup/mypet.sql';
$query = "SELECT * INTO OUTFILE '$backupFile' FROM $tableName";
$result = mysql_query($query);

or

$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);

is it possible to export MySQL database by executing a query?

Try like so and replace [user], [password] and [data-base-name]

mysqldump -u [user] -p [password] [data-base-name] > myDBDumpFileName.sql

Read here for more information Dumping Data in SQL Format with mysqldump

You can also dump single tables from the database like so:

 mysqldump -u user -p password data-base table1 table2 table5 > myDBDumpFileName.sql

Then to make it run automatically I recomend setting up a cronjob which then calls a shell script, but you can also setup the cronjob calling a PHP script. See here for more information on how to setup the cronjob

Create MySQL DB export via PHP

$dump = shell_exec('mysqldump --user=user --password=asdasd --host=localhost db_name');

alternatively you can use php implementation of mysqldump, available on https://github.com/clouddueling/mysqldump-php



Related Topics



Leave a reply



Submit