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 ?!

5
  • 3
    Possible duplicate of Automated or regular backup of mysql data Commented Feb 17, 2017 at 7:04
  • @e4c5 sorry it didn't give me an answer :( Commented Feb 17, 2017 at 7:07
  • It does. you shouldn't be doing this Commented Feb 17, 2017 at 7:09
  • @e4c5 can you please give me more explain ??! Commented Feb 17, 2017 at 7:10
  • Read that QA please then you will get your explaination Commented Feb 17, 2017 at 7:13

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.