我在Ubuntu上并且有一个我一直试图运行的小备份脚本。不幸的是,它没有执行备份。我已经在这里包含了两个PHP脚本,以防有些东西我不知道。
首先,这是我的crontab如何看起来像
*/30 * * * * /usr/bin/php /var/www/mybackup.php
上面的cron应该调用这个脚本:mybackup.php
<?php
include('myfunctions.php');
theBackup();
?>
主要脚本是这样的。虽然我手动运行它时效果很好,但它不能与cron一起运行。
<?php
/*
* Script to back up the database
*
*
*/
function getAllFiles($directory, $recursive = false) {
$result = array();
$handle = opendir($directory);
while ($datei = readdir($handle))
{
if (($datei != '.') && ($datei != '..'))
{
$file = $directory.$datei;
if (is_dir($file)) {
if ($recursive) {
$result = array_merge($result, getAllFiles($file.'/'));
}
} else {
$result[] = $file;
}
}
}
closedir($handle);
return $result;
}
function getOldestTimestamp($directory, $recursive = true, $display ='file') {
$allFiles = getAllFiles($directory, $recursive);
$highestKnown = time();
$highestFile = '';
foreach ($allFiles as $val) {
$currentValue = filemtime($val);
$currentFile = $val;
if ($currentValue < $highestKnown){
$highestKnown = $currentValue;
$highestFile = $currentFile;
}
}
if($display=='file'){
return $highestFile;
} else {
return $highestKnown;
}
}
function theBackup(){
$sendfrom = "System Backup <admin@domain.com>";
$headers = 'Admin <admin@domain.com>' . "\n";
$headers .= 'MIME-Version: 1.0' . "\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\n";
$filename = getOldestTimestamp('./app/db/',true,'file');
$filename = str_replace("./app/db/", "", $filename );
$backupfile = '/var/www/app/db/'.$filename;
$handle = fopen($backupfile, 'w') or die('Cannot open file: '.$backupfile);
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "password";
$dbname = "db";
if(system("mysqldump -h $dbhost -u $dbuser -p$dbpass $dbname > $backupfile") == false){
mail('email@yahoo.com','My Backup','Back Up successfully completed',$headers );
}else {
mail('email@yahoo.com','My Backup','Back Up did NOT complete successfully please check the file/folder
permission',$headers );
}
}
?>
上面的代码中是否有任何遗漏?就像我说的,当我从浏览器运行mybackup.php时,它运行得很好,但不是通过cron。
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
您正在使用绝对路径在cron作业中运行php
*/30 * * * * /usr/bin/php /var/www/mybackup.php
包含URL是相对的
include('myfunctions.php');
尝试使用包含绝对URL。
答案 1 :(得分:1)
我认为你需要完整的途径来包含你所说的:
include('myfunctions.php');
应该像
include('/var/www/myfunctions.php');
或者无论在哪里。 还要检查日志以查看您收到的错误消息