mysqldump in .php file - php

What's wrong with the exec() function
<?php
exec('mysqldump --host=localhost --user=root --password="" testing > C:/Users/deleo/Desktop/newfile.sql');
?>
when I run this code it's nothing happen but only white screen
I expected the output .sql file with my database(testing) table structure and data itself
When I did this
`
<?php
define("BACKUP_PATH", "C:/Users/deleo/Desktop/");
$server_name = "localhost";
$username = "root";
$password = "";
$database_name = "testing";
$date_string = date("Ymd");
$cmd = "mysqldump --hex-blob --routines --skip-lock-tables --log-error=mysqldump_error.log -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
$arr_out = array();
unset($return);
exec($cmd, $arr_out, $return);
if($return !== 0) {
echo "mysqldump for {$server_name} : {$database_name} failed with a return code of {$return}\n\n";
echo "Error message was:\n";
$file = escapeshellarg("mysqldump_error.log");
$message = `tail -n 1 $file`;
echo "- $message\n\n";
}
?>
`
I got this error
mysqldump for localhost : tapsihannibhey failed with a return code of 1 Error message was: -

Related

Enter password: mysqldump: Got error: 1045: Access denied for user 'XXX'#'localhost' (using password: NO) when trying to connect

I'm trying to write a PHP code that will backup my SQL database daily. However, running it using a Cron Job spits out the above error in the title. My server is hosted on hostgator and its a shared server.
The dbuser has all privileges granted.
Here's my code:
$dbhost = 'localhost';
$dbuser = 'XXXX';
$dbpass = 'XXXX';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.gz';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $backupFile";
system($command);
Any help would be much appreciated!
The following code worked for me:
$DBUSER="XXX";
$DBPASSWD="XXXX";
$DATABASE="XXXXX";
$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD $DATABASE | gzip --best > $filename";
passthru( $cmd );
exit(0);
your problem is the space between "-p" and $dbpass. The command mysqldump hopes "-ppassword" together (the response is "using password: no" for this).
Try:
$command = "mysqldump --opt -h $dbhost -u $dbuser -p$dbpass $dbname | gzip > $backupFile";

getting 0 kb after backup the sql

Why when i backup my sql the size is 0 kb what should i do ? or did i miss something? . and what is the code for recovering the sql. Thank you in advance
define("BACKUP_PATH", "D:\xampp/patient.sql");
$server_name = "localhost";
$username = "root";
$password = "7831365";
$database_name = "patient";
$date_string = date("Ymd");
$cmd = "mysqldump --routines -h {$server_name} -u {$username} -p{$password} {$database_name} > " . BACKUP_PATH . "{$date_string}_{$database_name}.sql";
exec($cmd);
I think you should define your BACKUP_PATH like this:
define("BACKUP_PATH", "D:\xampp\patient_");
try the following:escape characters
define("BACKUP_PATH", "D:\\xampp\\patient.sql");
check the response:
exec($cmd,$response);
print_r($response); //<-- might see the problem

Mysqldump error

if($_POST['submit'])
{
$dbhost = 'localhost';
$DBUSER = 'dbuser';
$DBPASSWD = 'dbpass';
$DATABASE = 'db';
$backupPath = '/home/*****/public_html/admin/sql';
$filename = "backup-" . date("d-m-Y");
$cmd = "mysqldump --complete-insert --create-options --add-locks --disable-keys --extended-insert --quick --quote-names -u $user --password=$DBPASSWD $DATABASE|gzip --fast -c >{$backupPath}/$filename.sql.gz";
exec($cmd);
echo $cmd;
}
This is the code i want to use for dumping database. But i never get any backup to specified directory, it always show the error which i have mentioned on my code. Ca anyone tell me whats the problem? Thank you.
N.B
Update this one -u $user with this --user $DBUSER $user but nothing happened.
Here is the sol-
$filename = $backupPath . '/DB_' . time(). '.gz';
if (file_exists($filename))
{
unlink($filename);
}
$cmd = 'mysqldump --opt -h '.$dbhost.' -u ' . $DBUSER . ' -p\'' . $DBPASSWD. '\' ' . $DATABASE . ' | gzip > ' . $filename;
exec($cmd);
echo $cmd;
Thank you all for your help.

mysqldump php command easyphp

when i run the following command from the cmd box, it works fine; creates the file with my database data inside it:
"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root -p databasename > D:\path\dumpedfile.sql
but then when i run this one from a php script, it makes the file, but its empty:
shell_exec('"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root -p databasename > D:\path\dumpedfile.sql');
(note: i have no password setup)
what am i doing wrong?
please help
thanks
If you don't have any password on your user account, you should try without the -p argument.
shell_exec('"\Program Files\EasyPHP-5.3.8.1\mysql\bin\mysqldump" -u root databasename > dumpedfile.sql');
Try something like this:
<?php
$User = "root";
$Password = "mypass";
$DatabaseName = "bcms6"
$File = "mydump.sql";
shell_exec("fullpath\mysqldump --allow-keywords --opt -u $User -p $Password $DatabaseName > $File");
//Or this way
shell_exec('"C:\Program Files (x86)\EasyPHP-5.3.9\mysql\bin\mysqldump.exe" -u root -p database > database_dump.sql'); //second way with full path.
?>
You can try this way too:
$command = 'mysqldump -u root -ppassword database > /path/database.sql';
system($command, $output);
if($output != 0) {
echo 'Error during backup';
else {
echo 'Database dumped';
}
EDITED..
The password has to be hardcoded.

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";

Categories