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
Using X-Sendfile with Apache/Php
Rotate Array Elements to the Left (Move First Element to Last and Re-Index)
Memcached VS Apc Which One Should I Choose
Yosemite/El Capitan PHP-Gd + Mcrypt Installation
$_Server["Remote_Addr"] Gives Server Ip Rather Than Visitor Ip
Convert PDF to Jpeg with PHP and Imagemagick
Advantages of MySQLi Over MySQL
How to Display a MySQL Error in PHP for a Long Query That Depends on the User Input
In PHP, Is There a Short Way to Compare a Variable to Multiple Values
Differencebetween Self::$Bar and Static::$Bar in PHP
Generating Random Numbers Without Repeats
Find Common Values in Multiple Arrays with PHP
Best Practice to Generate Random Token for Forgot Password
Laravel Eloquent Sum of Relation's Column
How to Run PHP Files on My Computer
Convert All Types of Smart Quotes with PHP
How to Schedule the Execution of a PHP Script on the Server Side