Export and Import Mysql Database using shell script in php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
ini_set('max_execution_time', '-1');
error_reporting(E_ALL);
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$db = 'alldatabackup';
$dir = 'dump.sql';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "CREATE DATABASE alldatabackup";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$mySQLDir='/Applications/MAMP/Library/bin/mysqldump';
$cmd = $mySQLDir .' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' alldata > dump.sql';
exec($cmd);
echo "Import Done";
$mySQLDirec='/Applications/MAMP/Library/bin/mysql';
$cmd = $mySQLDirec.' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' alldatabackup < "'.$dir.'"';
exec($cmd);
echo "Export Done";