-1
我試圖通過cron每4天運行一個php文件。 它會嘗試備份文件和數據庫。運行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和157 – 2014-11-25 10:23:31
能不能請你和縮進的代碼?我發現它很難閱讀它... – 2014-11-25 10:24:04
在第一個網站,它看起來像一個路徑問題。由於CLI從另一個「工作目錄」運行,然後是web服務器。您應該手動從CLI運行腳本,並在正確的位置放置一些調試語句,例如。 '的print_r($迭代器);'。 – giorgio 2014-11-25 10:24:46