使用 PHP 备份整个网站和数据库

问题描述 投票:0回答:1

我已经从事此工作一段时间了,这是我最接近使用 PHP 备份整个站点和数据库的方法。问题是我不明白为什么我继续在第 145 行和第 154 行收到错误。

错误:注意:未定义变量:arr_zip 在 C:\xampp\htdocs\wordpress ackup.php 第 145 行

警告:在 C:\xampp\htdocs\wordpress ackup.php 第 145 行为 foreach() 提供的参数无效

注意:未定义的变量:delete_zip 在 C:\xampp\htdocs\wordpress ackup.php 第 154 行

    <?php

     ini_set ("max_execution_time", 0);
     $dir = "site-backup-stark";
     if(!(file_exists($dir))) 
     {
        mkdir($dir, 0777);
     }

     $host = "localhost"; //host name
     $username = "wordpress_user"; //username
     $password = "pasword99"; // your password
     $dbname = "wordpress_db"; // database name

     $zip = new ZipArchive();

     backup_tables($host, $username, $password, $dbname);

     /* backup the db OR just a table */
    function backup_tables($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 = "stark-".$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("../$folder_name/"));
  // iterate over the directory
  // add each file found to the archive

  foreach ($iterator as $key=>$value) 
  {
if( strstr(realpath($key), "stark") == FALSE) 
{
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

    }
    // close and save archive
    $zip->close();
    echo "Archive created successfully.";

    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-stark folder
    foreach ($arr_zip as $key => $value) //error here
    {
if (strstr($value, "stark"))
{
    $delete_zip[] = $value;
    copy("$value", "$dir/$value");
}
    }

    for ($i=0; $i < count($delete_zip); $i++) //error here
    {
unlink($delete_zip[$i]);
    }

    ?>
php database backup
1个回答
0
投票

在这段代码中:

//get the array of zip files
if(glob("*.zip") != false) 
{
   $arr_zip = glob("*.zip");
}
//copy the backup zip file to site-backup-stark folder
foreach ($arr_zip as $key => $value) //error here

如果您对

glob("*.zip")
的调用返回“falsey”值,则您的变量
$arr_zip
将不会被初始化,并且您将在其后面的
foreach
中收到错误。使用
 显式检查 
false

    if(glob("*.zip") !== false) 

如果继续失败,您需要调查

glob()
失败的原因。我对此没有什么建议。

后来,你根本就没有初始化

$delete_zip
,在你需要的地方

$delete_zip = array();
© www.soinside.com 2019 - 2024. All rights reserved.