简体   繁体   English

使用PHP / MYSQL备份整个数据库

[英]Backup an entire database with PHP/MYSQL

I am trying to perform php/mysql backups 我正在尝试执行php / mysql备份

I receive values from a form page and then with the command "select tables", i save those values in a array. 我从表单页面接收值,然后使用“选择表”命令将这些值保存在数组中。

After that i do a "for" loop to backup each table: 之后,我做一个“ for”循环来备份每个表:

<?php

$dbname = $_POST['txt_db_name'];
$tbname = $_POST['txt_tb_name'];

$ligacao=mysql_connect('localhost','root','')
or die ('Problemas na ligação ao Servidor de MySQL');

$res = mysql_query("SHOW TABLES FROM pessoal");

$tables = array();

mysql_select_db($dbname,$ligacao);

while($row = mysql_fetch_array($res, MYSQL_NUM)) {
$tables[] = "$row[0]";
}

$length = count($tables);


for ($i = 0; $i < $length; $i++) {
$query=
"SELECT * INTO OUTFILE 'pessoa_Out.txt'".
"FIELDS TERMINATED BY ',' ".
"ENCLOSED BY '\"'".
"LINES TERMINATED BY '#'".
"FROM $tables[$i]";


 $resultado = mysql_query($query,$ligacao);

 }



 mysql_close();

 if ($resultado) 
 $msg ='Sucesso na Exportaçao da Database '.$dbname.' ';
 else
 $msg ='Erro Impossivel Exportar a Database '.$tbname.' ';

 ?>

Don't do this — you are re-inventing existing tools! 不要这样做-您正在重新发明现有工具!

Invoke mysqldump instead, which is expressly designed for the purpose. 而是调用mysqldump ,它是专门为此目的而设计的。

With the proper permissions, you can use PHP's system or exec to invoke this. 拥有适当的权限,您可以使用PHP的systemexec来调用它。

First, I agree with the ones saying that mysqldump should be used for this. 首先,我同意那些人说应该为此使用mysqldump的说法。 But basing on your comment that you just need it to be done with php/mysql for educational (or whatever) purposes, here is the script (yep, it re-invents the wheel). 但是根据您的评论,您只需要出于教育(或其他目的)目的而使用php / mysql完成此操作,以下是脚本(是的,它重新发明了轮子)。 Note that you should create a backup directory in the folder where you upload this and allow web server to write to it : 请注意,您应该在上传目录的文件夹中创建一个备份目录,并允许Web服务器对其进行写入:

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
ini_set('memory_limit','1500M');
set_time_limit(0);

backup_tables('localhost','user','xxxxxxx','xxxxxxxxxx');

/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
  //save file
  $handle = gzopen(getcwd() . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'db-backup-'.time().'.sql.gz','w9');

  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);

  //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);
  }

  //cycle through
  foreach($tables as $table)
  {
    $result = mysql_query('SELECT * FROM '.$table);
    $num_fields = mysql_num_fields($result);

    $return = 'DROP TABLE IF EXISTS '.$table.';';
    $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
    $return .= "\n\n".$row2[1].";\n\n";
    gzwrite($handle,$return);
    for ($i = 0; $i < $num_fields; $i++) 
    {
      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] = str_replace("\n","\\n",$row[$j]);
          if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
          if ($j<($num_fields-1)) { $return.= ','; }
        }

        $return.= ");\n";
        gzwrite($handle,$return);
      }
    }
    $return ="\n\n\n";
    gzwrite($handle,$return);
  }


  gzclose($handle);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM