运行 cron 作业时 PHP 文件中的错误


Errors in PHP file while running a cron job

我正在尝试每 4 天通过 cron 运行一个 php 文件。它尝试备份文件和数据库。

#!/usr/bin/php -q
<?php
if($filecount<=4)
{
$host = "internal-db.host.gridserver.com"; //host name
$username = "user"; //username
$password = "Password"; // your password
$dbname = "Dataabse"; // database name
$dir = "/backup-all";
if(!(file_exists($dir))) {
}
$zip = new ZipArchive();
//backup_tables($host, $username, $password, $dbname);

/* backup the db OR just a table */
function backup_tables1($host,$user,$pass,$name,$tables = '*')
{
$con = mysql_connect($host,$user,$pass);
mysql_select_db($name,$con);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
$return = "";
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "nn".$row2[1].";nn";
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = preg_replace("#n#","n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");n";
}
$return.="nnn";
}
//save file
$handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
fwrite($handle,$return);
fclose($handle);
}

if (glob("*.sql") != false)
{
$filecount = count(glob("*.sql"));
$arr_file = glob("*.sql");
for($j=0;$j<$filecount;$j++)
{
$res = $zip->open($arr_file[$j].".zip", ZipArchive::CREATE);
if ($res === TRUE)
{
$zip->addFile($arr_file[$j]);
$zip->close();
unlink($arr_file[$j]);
}
}
}

//get the current folder name-start
$path = dirname($_SERVER['PHP_SELF']);
$position = strrpos($path,'/') + 1;
$folder_name = substr($path,$position);
//get the current folder name-end

$zipname = date('Y/m/d');
$str = "figata-".$zipname.".zip";
$str = str_replace("/", "-", $str);

// open archive
if ($zip->open($str, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("/home/161441/users/.home/domains/growing-your-business.org/html/"));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
if( strstr(realpath($key), "figata") == FALSE) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
}
// close and save archive
$zip->close();

if(glob("*.sql.zip") != false) {
$filecount = count(glob("*.sql.zip"));
$arr_file = glob("*.sql.zip");
for($j=0;$j<$filecount;$j++)
{
unlink($arr_file[$j]);
}
}

//get the array of zip files
if(glob("*.zip") != false) {
$arr_zip = glob("*.zip");
}
//copy the backup zip file to site-backup-figata folder
foreach ($arr_zip as $key => $value) {   // **Line 142**
if (strstr($value, "figata")) {
$delete_zip[] = $value;
copy("$value", "$dir/$value");
}
}

for ($i=0; $i < count($delete_zip); $i++) {
unlink($delete_zip[$i]);
}
$dir ="home/domains/growing-your-business.org/backup-growing/";//dir absolute path
$interval = strtotime('-96 hours');//files older than 24hours
foreach (glob($dir."*.zip") as $file) // **Line 157**
    //delete if older
    if (filemtime($file) <= $interval ) unlink($file);
echo '<script>alert("Archive created successfully.")</script>';
}
else
{
    echo '<script>alert("Archive Not created successfully.")</script>';
}
?>

当它通过 http 运行时,该功能可以正常工作。但是当它通过 Cron运行时,我收到以下错误:

    Warning: Invalid argument supplied for foreach() in /nfs/c05/h05/mnt/161441/domains/growing-your-business.org/html/backup-growing.php on line 142
Warning: Invalid argument supplied for foreach() in /nfs/c05/h05/mnt/161441/domains/growing-your-business.org/html/backup-growing.php on line 157
<script>alert("Archive created successfully.")</script>

谁能帮我解决这个问题???

提前谢谢。

对于第 142 行

您要么先检查$arr_zip是否设置,要么定义它。

$arr_zip = array( ); //create an empty array
//get the array of zip files
if(glob("*.zip") != false) {
    $arr_zip = glob("*.zip");
}
//copy the backup zip file to site-backup-figata folder
foreach ($arr_zip as $key => $value) {   // **Line 142**
    if (strstr($value, "figata")) {
        $delete_zip[] = $value;
        copy("$value", "$dir/$value");
    }
}

if( isset( $arr_zip ) )
{
    foreach ($arr_zip as $key => $value) {   // **Line 142**
        if (strstr($value, "figata")) {
            $delete_zip[] = $value;
            copy("$value", "$dir/$value");
        }
    }
}

我宁愿定义它而不是检查它是否设置。

对于 157 行

几乎复制了你做第 142 行的方式:

$files = array( );
if( glob($dir."*.zip") != false ) {
    $files = glob($dir."*.zip");
}
foreach ($files as $file) // **Line 157**