Mysqldump returning empty file - php

My script is as following:
<?php
$database = 'database';
$user = 'user';
$password = 'pass';
$filename = 'database_backup_'.date( 'd-m-Y-h:i:s' ).'.sql';
$result = exec( 'mysqldump '.$database.' --password='.$password.' --user='.$user.' --single-transaction >/some-path/backups/'.$filename, $output );
?>
This script is creating a file but it is empty.

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

mysqldump returns empty file PHP

I trying to backup my database using mysqldump. The file is being generated but the file is empty. How can I fix this?
PHP Code:
$dbuser = 'demo';
$dbpass = 'demo';
$host = 'localhost';
$dbname = 'demo';
$filename = __DIR__ . '/backup/'. $dbname . '_'.$date.'.sql';
exec("mysqldump --user={$dbuser} --password={$dbpass} --host={$host} $dbname > {$filename}");

How can I export MySQL database with a PHP script?

I need one script that when I open the script.php page for example, that export me one database from MySQL to an SQL document, and I don't know how to do it in PHP, can some one help me with that?
Edited Code:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpwd = "";
$dbname = "activmanagement";
$dumpfile = $dbname . "_" . date("Y-m-d_H-i-s") . ".sql";
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
// report - disable with // if not needed
// must look like "-- Dump completed on ..."
echo "$dumpfile "; passthru("tail -1 $dumpfile");
?>
This line:
passthru("D:/wamp/bin/mysql/mysql5.6.17/bin/mysqldump --opt --host=$dbhost --user=$dbuser --password=$dbpwd $dbname > $dumpfile");
Refer PHP - exec() vs system() vs passthru(). You just need something like this
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname = 'activmanagement';
$sql_backup_file = $dbname . date("Y-m-d-H-i-s") . '.gz';
$dump = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname | gzip > $sql_backup_file";
system($dump);
?>

php file only works after reload

I use this script to create a mysqpl dump and then upload the file to dropbox. If i run the script at first nothing happens -> blank page. But when i refresh the page everything works fine
<?php
######## einstellungen #############################################
$db_name = "name";
$db_passwd = "pw";
$sql_file = "dump_" . $db_name . "_" . date('Ymd_Hi') . ".sql";
####################################################################
exec("mysqldump -u $db_name -p'$db_passwd' --allow-keywords --add-drop-table --complete-insert --quote-names $db_name > $sql_file");
exec("gzip $sql_file");
$datei = $sql_file . ".gz";
$link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$link = str_replace(basename(__FILE__),$datei,$link);
require 'DropboxUploader.php';
while (!file_exists($datei)) sleep(1);
$uploader = new DropboxUploader('email', 'pw');
$uploader->upload($datei);
?>

Backup and Restore MySQL database in PHP

I am trying to use PHP to backup and restore a MySQL database:
Backup:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/backup/test.sql";
exec("D:/xampp/mysql/bin/mysqldump --opt -h $dbhost -u $dbuser -p $dbpass $dbname > $output");
echo "Backup complete!";
Restore:
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'dbpass';
$dbname = 'test';
$output = "D:/restore/test.sql";
exec("D:/xampp/mysql/bin/mysql --opt -h $dbhost -u $dbuser -p $dbpass $dbname < $output");
echo "Restore complete!";
But both are not working. When Backup is complete then I check test.sql file that is blank. When the restore is complete, the database is still blank.
How can I fix this?
Script to backup using Php
<?php
define("BACKUP_PATH", "/home/abdul/");
$server_name = "localhost";
$username = "root";
$password = "root";
$database_name = "world_copy";
$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);
?>
Script to restore
<?php
$restore_file = "/home/abdul/20140306_world_copy.sql";
$server_name = "localhost";
$username = "root";
$password = "root";
$database_name = "test_world_copy";
$cmd = "mysql -h {$server_name} -u {$username} -p{$password} {$database_name} < $restore_file";
exec($cmd);
?>

Categories