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

时间:2014-11-25 10:21:28

标签: php cron

我试图每隔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>

任何人都可以帮我这个???

先谢谢。

1 个答案:

答案 0 :(得分:0)

第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**