私はUbuntuを使用しており、実行しようとしている小さなバックアップスクリプトを持っています。残念ながら、バックアップは実行されていません。足りないものがある場合に備えて、ここに 2 つの 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 では機能しません。
どんな助けでも大歓迎です。