0

I try to create a backup for my database. My database is MySQL. I am using PHP. I use a cron job to execute this code every hour.

This is my code:

$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$dbname='stock';
$backup_file = $dbname . date("Y-m-d-H-i-s") . '.sql';
$command = "mysqldump --opt -h $dbhost -u $dbuser -p $dbpass ". "stock | gzip > $backup_file";
system($command);

The problem is that my code give me an empty file How can i have my all database ?!

piet.t
  • 11,718
  • 21
  • 43
  • 52
Developer
  • 39
  • 7

1 Answers1

1

I also encounter same problem/situation years back. Refer here. Here are a few things that need to TAKE NOTE:-

  1. Compulsory to use absolute path for MySQL dump.
  2. Try using --opt (default option for MySQL dump). Can refer http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
  3. For the option, if use short form, no need to have (--). eg: --p. Use (--) when using full form. eg: --password. So use '-p' instead of '--p' (applied for others option too).
  4. try 'shell_exec' or 'system' if MySQL dump doesn't work on 'exec'.
  5. try to avoid have space between option and variable. Eg: -p$dbpass

*Also bear in mind, it involves with permission whether system command can be executed from PHP or not.

Community
  • 1
  • 1
Salehin Rafi
  • 351
  • 2
  • 6
  • 16