how to properly execute mysqldump through php - php

After a database is successfully inserted with data, I want now to backup the database. Since this is a script done in codeigniter, I created this function below.
This is bugging me for a while. I want to execute a php script that contains command line.
Here is my code:
public function backupDb()
{
log_message('debug', 'Preparing to Back-up Database...');
$dbuser = "root";
$dbpass = "root#111";
$dbname = "listingdb";
$dumpfile = $dbname . "_" . date("Ymd") . ".sql";
$command = "mysqldump -u $dbuser -p$dbpass $dbname > $dumpfile";
exec($command);
}
I tried manually doing this code;
mysqldump -u root -proot#111 listingdb > listingdb_20171013.sql
and it Successfully creates a .sql file
But my function seems to not work. How can I do it right?

Related

Click Button To Export All Data In Database In To Myfile As .sql (Extension)

There is my Function to download backup is sql format
public function actionBackup()
{
// Set the database access credentials
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'fads';
// Set the backup filename and path
$backup_file = 'backup.sql';
$backup_path = "C:/Users/" . get_current_user() . "/Downloads/" . $backup_file;
// Execute the mysqldump command
$path = "C: \ xampp_7.3\mysql\bin";
$command = "mysqldump -u {$db_username} -p{$db_password} {$db_name} > {$backup_path}";
// echo $path.$command;die;
exec("$path > $command");
// Send the backup file as a download
Yii::$app->response->sendFile($backup_path);
// Delete the backup file
unlink($backup_path);
}
It's download backup.sql but its empty i don't understand that where is my code wrong and why i received blank backup.sql
First look if you use correct path for command and backup file.
Avoid spaces in paths.
Try this code:
public function actionBackup()
{
// Set the database access credentials
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'fads';
// Set the backup filename and path
$backup_file = 'backup.sql';
$backup_path = "C:\Users\" . get_current_user() . "\Downloads\" . $backup_file;
// Execute the mysqldump command
$command = "\"C:\\xampp_7\\mysql\\bin\\mysqldump.exe\""; // path to mysqldump command
// $commandParams = "-u {$db_username} -p{$db_password} {$db_name} > {$backup_path}"; // mysqldump params
$commandParams = "--host={$db_host} --user={$db_username} --password={$db_password} {$db_name} > {$backup_path}"; // mysqldump params
exec("$command $commandParams");
// Send the backup file as a download
Yii::$app->response->sendFile($backup_path);
// Delete the backup file
unlink($backup_path);
}
Updated the answer

Restore mysql dump for specific table via PHP

I've got a mysql table dump file (which can be viewed here: http://pastebin.com/raw.php?i=GQkjrDNz) that I would like to use to create a table (named php_blog_archive as per the dump file) using the contents of that sql file.
The problem is I don't have access to phpmyadmin, so I can only execute this via php command, I've looked at a few threads like this and the code I have thus far is,
<?php
// Config
$db_user = "username";
$db_pass = "password";
exec("mysql -u $db_user -p $db_pass -h localhost databasename < restoreold.sql ");
?>
But it doesn't work, it simply does nothing - doesn't even show any errors. Could someone please advise me as to how to proceed?
First of all the command must be:
<?php
// Config
$db_user = "username";
$db_pass = "password";
exec("mysql -u " . $db_user . " -p " . $db_pass . " -h localhost databasename < restoreold.sql "); // Added an space before -h
?>
And second, you've right access to create/edit/append the file and exec command?
I think you have the good old problem wit the password.
<?php
// Config
$db_user = "username";
$db_pass = "password";
exec("mysql -u " . $db_user . " -p'" . $db_pass . "' -h localhost databasename < restoreold.sql 2>&1", $output);
echo nl2br($output);
?>
This will end up in mysql -u username -p'password' -h localhost databasename < restoreold.sql 2>&1
The last thing is to echo the error masseges or something else to HTML. This is done via 2>&1 and the $output reference variable which will be convertet from nl (new lines will end up with <br />) to HTML and echoed.
Hope it helps and works.

PHP exec() only work on command line only

I am using PHP to backup MySQL database. But my code only works on command line (command: php index.php). If I execute it on browser, the dump file not generate. here is my code:
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'otoworks';
$backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "/usr/bin/mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $backup_file";
exec($command);
?>
My php run on apache 2.2.3, centos 6. php.ini (safe_mode = Off).
Thanks in advance

MysqlDump Return a Empty database backup file

I use this code for download a backup of MySql database but it download a empty file.
Please also guide for restore this backup.
<?php
//connect to database
include'connect.php';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
system($command);
?>
Replace system with echo to show the generated command, and execute it on the command line manually. Errors are usually shown on STDERR, which isn't caught by the system call, and if you get an empty output that means it couldn't output anything. Fix the error and then fix your code. I'd also use passthru instead of system.
To restore the backup afterwards use (from the commandline):
mysql -u<user> -p <database> < myfile.sql
Your variables are sticked to the options. Try to change this:
$command = "mysqldump -h$hostname -u$username -p$password $dbname > $backupFile";
To this:
$command = "mysqldump -h $hostname -u $username -p $password $dbname > $backupFile";

i want to export a single table from the database witout using phpmyadmin

i have only ftp credentials in which i am using this script
$r="mysqldump $dbuser $dbpass $dbname wp_posts > table1.sql";
system($r);
but unfortunatly m getting a blank result in table1.sql
You need to add the option switches to your command, see the mysqldump manual, like this
$r="mysqldump -u $dbuser -p $dbpass $dbname wp_posts > table1.sql";
finally i got the solution, here is my code:
<?php
$username = "abc";
$password = "xyz";
$hostname = "domain.com";
$database = "test_db";
$username =escapeshellcmd($username);
$password =escapeshellcmd($password);
$hostname =escapeshellcmd($hostname);
$database =escapeshellcmd($database);
$backupFile='url'.date("Y-m-d-H-i-s").$database.'.sql.tgz';
$command = "mysqldump -u$username -p$password -h$hostname $database wp_posts > $backupFile";
system($command, $result);
echo $result;
?>
thnx for your support, really appreciate it.
if the user has the permissions try this:
mysqldump -u$dbuser -p$dbpass $dbname wp_posts > table1.sql
to specify the parameters (which is the user and which is the password)
I think it's
mysqldump -u $dbuser -p $dbpass $dbname wp_posts
Apache user needs to have the permission to write table1.sql and also execute mysqldump command. If on shared hosting, this is doubtful.

Categories