Trying to run a simple backup script and it’s only printing error message. Is there something I doing wrong? Here’s the complete code. I’m on linux and have full access to the database.
It only prints ‘There was an issue with your system that prevented the backup from completing’
<?php
$dbhost = "localhost"; // usually localhost
$dbuser = "myuser";
$dbpass = "mypass";
$dbname = "mydb";
$sendto = "Webmaster <webmaster@mydomain.com>";
$sendfrom = "Automated Backup <info@mydomain.com>";
$sendsubject = "Daily Mysql Backup";
$bodyofemail = "Here is the daily backup.";
$headers = 'My Shop <myname@mydomain.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$backupfile = $dbname . date("Y-m-d") . '.sql';
if(system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile")){
mail('receiver@yahoo.com','Back Up Service','Back Up successfully completed',$headers );
echo'Back up saved successfully.';
}else {
echo'There was an issue with your system that prevented the backup from completing';
}
?>
Thanks for your help.
There will be something wrong with your command:
This looks fine but it’s worth printing it out so that you can check it in the command line to see what result you get.
Edit:
Looking at the system docs available here: system it looks like this returns the last line of the commands output. If mysqldump runs sucessfully but doesn’t create any output your
ifwill return false. Either use:or use the return_var argument to system to check the status code (it should be 0 on success).