I have configured a cronjob to backup a mysql database and upload the file to Amazon S3. Here's the job:
57 14 * * * . $HOME/.profile; php /root/backup_scripts/mysql-backup/index.php
And here are the contents of index.php
:
<?php
require 'vendor/autoload.php';
$s3 = new Aws\S3\S3Client([
'version' => 'latest',
'region' => 'us-east-1',
'profile' => 'my-bucket',
]);
$file = tmpfile();
$filename = stream_get_meta_data($file)['uri'];
$username = getenv('MYSQL_USERNAME');
$password = getenv('MYSQL_PASSWORD');
shell_exec("mysqldump -u {$username} -p{$password} --events --all-databases --extended-insert=FALSE --complete-insert=TRUE > {$filename}");
$result = $s3->putObject([
'Bucket' => 'my-bucket',
'Key' => 'mysql.backup.' . time(),
'Body' => $file,
]);
fclose($file);
?>
The upload works correctly, but I am receiving an email from Cron with the output of the command. The output is:
stdin: is not a tty
I've read some related questions on SO and understand that this is caused by a command expecting to be run with stdin as a terminal. When cron runs the command, there is no terminal, so the output occurs.
However, I haven't been able to find a solution for how to get rid of this. Is there any way to ensure this output does not occur? I'm also not clear on which command is actually generating the output. Is it the cron command itself, or the mysqldump
being executed from within the PHP script?