Mysqldump error - php

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.

Related

mysqldump in .php file

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: -

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

Backup DB from php

I'm testing this script on the local server
$dump_path = "backups";
$host = "localhost";
$user = "root";
$pass = "";
$command=$dump_path.'mysqldump -h '.$host.' -u '.$user.' dental > dental_b.sql';
if (system($command)) {
echo "YES" ;
} else {
echo "Error" ;
}
But why isn't that script executed and shows an error although all data from the DB is true.
I'm pretty sure you don't need the host option if you're using mysqldump on localhost. The way you are concatenating your command looks messed up to me. Let me show you what works for me. I use this all the time:
<?php
$user = "root";
$pass = "";
$database = "dental";
// File name
$file_name = 'dental_b.' . date('mdY.Hia') . '.sql.gz';
// Storage directory
$storage_dir = __DIR__ . '/backups';
if( ! is_dir( $storage_dir ) )
mkdir( $storage_dir, 0777, TRUE );
// Absolute path to new file
$absolute_path = $storage_dir . '/' . $file_name;
// Create the backup file
exec( 'mysqldump -u ' . $user .
' -p' . $pass .
' ' . $database .
' | gzip > ' . $absolute_path );
Yes, I am gzipping the file. I think you'll find this is a good option for storage, emailing, etc.
EDIT ---
If you are not using a password, you should not use -p. You'd want to alter the command in that case.

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

backup .sql file is empty

i am trying to backup mysql database by using this code
include ("functions_cp/f_connection.php");
Sqlconnection() ;
$dbname = "Reservebox";
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "123";
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = 'mysqldump -h' . $dbhost . ' -u ' . $dbuser . '-p =' . $dbpass . ' '. $dbname . ' > ' . $backupFile ;
system($command);
the script works fine and it generates a .sql file , however this file is empty , how can i fix this problem ?
thanks
Don't put a space or equals sign between the -p and the password. Also, you are missing a space before the -p.
$command = 'mysqldump -h' . $dbhost . ' -u ' . $dbuser . ' -p' . $dbpass . ' '. $dbname . ' > ' . $backupFile ;
Probably a file permissions error. Check that whatever user PHP and MySQL are running as have the permissions to write the file. FOr troubleshooting purposes, chmod the directory to 0777 and see if that fixes the problem. If so, chown the directory to whatever user MySQL is running as.

Categories