Getting 0KB of size file while backup mysql database using mysql dump [closed]

Posted on

Question :

I am using below code to backup my database using php script but I am getting 0KB size file. How can I backup my full database with all routines and functions?

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname='mydatabase';
$toDay = date('d-m-Y');
    //exec("mysqldump --user=$dbuser --password='$dbpass' --host=$dbhost --single-transaction $dbname > D:Sql_Backups/".$toDay."_DB.sql");
    //exec('mysqldump --user=$dbuser --password=$dbpass --host=$dbhost $dbname > D:Sql_Backup/file.sql');
      exec ("mysqldump --routines --h $dbhost --u $dbuser --p $dbpass --single-transaction $dbname > D:Sql_Backup/db3_backup.sql" >

Answer :

The reason you get a 0KB file for output is becuse of a syntax error in the mysqldump

You have this in the question

exec ("mysqldump --routines --h $dbhost --u $dbuser --p $dbpass --single-transaction $dbname > D:Sql_Backup/db3_backup.sql" > ...

Remove the extra dashes from -h, -u, -p

exec ("mysqldump --routines -h $dbhost -u $dbuser -p $dbpass --single-transaction $dbname > D:Sql_Backup/db3_backup.sql" > ...

The first two commands you have commented out has double dashes when you spell out the option. That part of the syntax is correct.

OTHER THINGS TO CHECK

  • Make sure the you use the same slashes on the file. Also, check which one of the following works properly in PHP
    • D:/Sql_Backup/db3_backup.sql
    • D:Sql_Backupdb3_backup.sql
    • D:\Sql_Backup\db3_backup.sql
  • Make sure mysqldump.exe is in the %PATH. If if is not, you make have to use the absolute path by doing something like this:

    exec (“D:Program FilesMySQLMySQL 5.x Serverbinmysqldump –routines –h $dbhost –u $dbuser –p $dbpass –single-transaction $dbname > D:Sql_Backup/db3_backup.sql” > …

I don’t know how to write this in php, but you should write mysqldump command like below, there is no need to write three command one is enough

mysqldump --user=root --password=root --host=localhost --single-transaction --routines db_name > db_name.sql

UPDATE

I did it here is the php script

<?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);
?>

It generate backup file named as 20140225_world_copy.sql in /home/abdul/

For reference have a look at This link.

You need to use the follwoing syntax :

Full dump with routines as well
mysqldump –routines > outputfile.sql


To dump your routines
mysqldump –routines –no-create-info –no-data –no-create-db –skip-opt > dumpfile.sql


-the option –routines is available from verion 5.0.13.
-remember that the backup user need to have the rights to read the routines.

You need find where the mysqldump.exe for backup and mysql.exe for restore is in your computer. Then copy the path to the code: Then create a backup folder to save the backup file.

For backup

$dumpfile”);
?>

Leave a Reply

Your email address will not be published. Required fields are marked *