3

(with PHP)如果表前缀与子字符串匹配,我想从数据库备份表。我正在尝试但没有工作

error_reporting(1);
$dbname = 'wp_dev';

if (!mysql_connect('127.0.0.1', 'root', '')) {
    echo 'Connection Error';
    exit;
}

$sql = "SHOW TABLES FROM $dbname LIKE 'wp_%'";
$result = mysql_query($sql);

if (!$result) {
    echo "DB tables could not be listed\n";
    echo 'MySQL Fehler: ' . mysql_error();
    exit;
}

while ($row = mysql_fetch_row($result)) {
    echo "<pre>Table: {$row[0]}\n</pre>";
    system( 'mysqldump $dbname $row[0] > verlag_$row[0].sql');
}

mysql_free_result($result);
4

5 回答 5

9

这是一个用于从 db 或仅一些表进行 bakups 的函数

function &backup_tables($host, $user, $pass, $name, $tables = '*'){
  $data = "\n/*---------------------------------------------------------------".
          "\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
          "\n  HOST: {$host}".
          "\n  DATABASE: {$name}".
          "\n  TABLES: {$tables}".
          "\n  ---------------------------------------------------------------*/\n";
  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  mysql_query( "SET NAMES `utf8` COLLATE `utf8_general_ci`" , $link ); // Unicode

  if($tables == '*'){ //get all of the 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);
  }

  foreach($tables as $table){
    $data.= "\n/*---------------------------------------------------------------".
            "\n  TABLE: `{$table}`".
            "\n  ---------------------------------------------------------------*/\n";           
    $data.= "DROP TABLE IF EXISTS `{$table}`;\n";
    $res = mysql_query("SHOW CREATE TABLE `{$table}`", $link);
    $row = mysql_fetch_row($res);
    $data.= $row[1].";\n";

    $result = mysql_query("SELECT * FROM `{$table}`", $link);
    $num_rows = mysql_num_rows($result);    

    if($num_rows>0){
      $vals = Array(); $z=0;
      for($i=0; $i<$num_rows; $i++){
        $items = mysql_fetch_row($result);
        $vals[$z]="(";
        for($j=0; $j<count($items); $j++){
          if (isset($items[$j])) { $vals[$z].= "'".mysql_real_escape_string( $items[$j], $link )."'"; } else { $vals[$z].= "NULL"; }
          if ($j<(count($items)-1)){ $vals[$z].= ","; }
        }
        $vals[$z].= ")"; $z++;
      }
      $data.= "INSERT INTO `{$table}` VALUES ";      
      $data .= "  ".implode(";\nINSERT INTO `{$table}` VALUES ", $vals).";\n";
    }
  }
  mysql_close( $link );
  return $data;
}

如何使用:

// create backup
//////////////////////////////////////

$backup_file = 'db-backup-'.time().'.sql';

// get backup
$mybackup = backup_tables("myhost","mydbuser","mydbpasswd","mydatabase","*");

// save to file
$handle = fopen($backup_file,'w+');
fwrite($handle,$mybackup);
fclose($handle);

您可以修改该行:

$result = mysql_query("显示表");

表前缀

于 2013-01-25T18:26:10.457 回答
0

和便便

<?php
//backup
//function &backup_tables($host, $user, $pass, $name, $tables = '*'){

function &backup_tables($host, $user, $pass, $name, $tables){   
$data = "\n/*---------------------------------------------------------------".
      "\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
      "\n  HOST: {$host}".
      "\n  DATABASE: {$name}".
      "\n  TABLES: {$tables}".
      "\n  ---------------------------------------------------------------*/\n";
 include "connexion.php";

$myquery="SET NAMES `utf8` COLLATE `utf8_general_ci`";
$result = $mylink->query($myquery);

if($tables == '*'){ //get all of the tables
$tables = array();
$myquery="SHOW TABLES";
$result = $mylink->query($myquery);

while($row = mysqli_fetch_row($result)){
  $tables[] = $row[0];
}
}else{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}

foreach($tables as $table){
$data.= "\n/*---------------------------------------------------------------".
        "\n  TABLE: `{$table}`".
        "\n  ---------------------------------------------------------------*/\n";           
$data.= "DROP TABLE IF EXISTS `{$table}`;\n";

$myquery="SHOW CREATE TABLE `{$table}`";
$result = $mylink->query($myquery);

$row = $result->fetch_row();
$data.= $row[1].";\n";

$myquery="SELECT * FROM `{$table}`";
$result = $mylink->query($myquery);
$num_rows = $result->num_rows;


if($num_rows>0){
  $vals = Array(); $z=0;
  for($i=0; $i<$num_rows; $i++){
    $items = mysqli_fetch_row($result);
    $vals[$z]="(";
    for($j=0; $j<count($items); $j++){
      if (isset($items[$j])) { $vals[$z].= "'".$mylink->real_escape_string($items[$j]) ."'"; } else { $vals[$z].= "NULL"; }
      if ($j<(count($items)-1)){ $vals[$z].= ","; }
    }
    $vals[$z].= ")"; $z++;
  }
  $data.= "INSERT INTO `{$table}` VALUES ";      
  $data .= "  ".implode(";\nINSERT INTO `{$table}` VALUES ", $vals).";\n";
  }
  }
 $mylink->close();
 return $data;
 }


//***********
// called by :

$backup_file = 'db-backup-'.time().'.sql';

// get backup

$host = "yourhost";
$user = "youruser";
$pass = "your_pw";
$name = "your base";
$tables= 'your_table';
 $mybackup = backup_tables($host,$user,$pass,$name,$tables);

// save to file
$handle = fopen($backup_file,'w+');
fwrite($handle,$mybackup);
fclose($handle);

//affichage des résultats, pour savoir si la modification a marchée:
 echo("La sauvegarde à été correctement effectuée") ;

?>

where connection.php is :
<?php
 $mylink = new mysqli("host", "user", "pw", "base");
if ($mylink->connect_errno) {
echo "Echec lors de la connexion à MySQL : " . $mylink->connect_error;
exit();
} 
于 2017-02-08T16:43:22.257 回答
0

我设置了简单的函数 'openDb()' 和 get($SQL) 来打开数据库并执行 PHP mysql 查询。可能存在逐行复制更有用的情况,但您也可以使用,如果它有效(使用我上面的简单函数):

 openDb($db,$user,$pw);
 get("CREATE TABLE `TABLE1_backup` LIKE `TABLE1`");
 get("INSERT INTO `TABLE1_backup` (SELECT * FROM `TABLE1`)");

(注意这是一个基本描述。根据需要添加 DROP 等。“get”根据成功返回 T/F,因此对于调试,可能需要测试 false 并提供 MySQLi_ERROR)

于 2016-09-14T19:13:07.093 回答
-1
function backup_tables($tables = '*', $filepath) {
$data = "\n/*---------------------------------------------------------------" .
        "\n  SQL DB BACKUP " . date("d.m.Y H:i") . " " .
        "\n  HOST: {$host}" .
        "\n  DATABASE: {$name}" .
        "\n  TABLES: {$tables}" .
        "\n  ---------------------------------------------------------------*/\n";
mysql_query("SET NAMES `utf8` COLLATE `utf8_general_ci`"); // Unicode

if ($tables == '*') { //get all of the tables
    $tables = array();
    $result = mysql_query("SHOW TABLES");
    while ($row = mysql_FetchRecordsFromCSV_row($result)) {
        $tables[] = $row[0];
    }
} else {
    $tables = is_array($tables) ? $tables : explode(',', $tables);
}

foreach ($tables as $table) {
    $data.= "\n/*---------------------------------------------------------------" .
            "\n  TABLE: `{$table}`" .
            "\n  ---------------------------------------------------------------*/\n";
    $data.= "DROP TABLE IF EXISTS `{$table}`;\n";
    $res = mysql_query("SHOW CREATE TABLE `{$table}`");
    $row = mysql_fetch_row($res);
    $data.= $row[1] . ";\n";

    $result = mysql_query("SELECT * FROM `{$table}`");
    $num_rows = mysql_num_rows($result);

    if ($num_rows > 0) {
        $vals = Array();
        $z = 0;
        for ($i = 0; $i < $num_rows; $i++) {
            $items = mysql_fetch_row($result);
            $vals[$z] = "(";
            for ($j = 0; $j < count($items); $j++) {
                if (isset($items[$j])) {
                    $vals[$z].= "'" . mysql_real_escape_string($items[$j]) . "'";
                } else {
                    $vals[$z].= "NULL";
                }
                if ($j < (count($items) - 1)) {
                    $vals[$z].= ",";
                }
            }
            $vals[$z].= ")";
            $z++;
        }
        $data.= "INSERT INTO `{$table}` VALUES ";
        $data .= "  " . implode(";\nINSERT INTO `{$table}` VALUES ", $vals) . ";\n";
    }
}
//mysql_close( $link );
$handle = fopen($filepath, 'w+');
fwrite($handle, $data);
fclose($handle);

}

于 2018-04-11T07:33:12.023 回答
-3

与 user3365179 相同,但使用 PDO。

将 ISO-8859-1 更改为 utf8

$mylink 在 $GLOBALS['db_pdo']

function backup_tables_pdo($host, $user, $pass, $name, $tables) {   
    $data = "\n/*---------------------------------------------------------------".
          "\n  SQL DB BACKUP ".date("d.m.Y H:i")." ".
          "\n  HOST: {$_SERVER['SERVER_NAME']}".
          "\n  DATABASE: {$name}".
          "\n  TABLES: {$tables}".
          "\n  ---------------------------------------------------------------*/\n";
    // include "connexion.php";

    $myquery="SET NAMES `ISO-8859-1` COLLATE `latin1_spanish_ci`";
    $result = $GLOBALS['db_pdo']->query($myquery);

    if($tables == '*'){ //get all of the tables
        $tables = array();
        $myquery="SHOW TABLES";
        $result = $GLOBALS['db_pdo']->query($myquery);

        while($row = mysqli_fetch_row($result)){
          $tables[] = $row[0];
        }
    } else {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }

    foreach($tables as $table) {
        $data.= "\n/*---------------------------------------------------------------".
                "\n  TABLE: `{$table}`".
                "\n  ---------------------------------------------------------------*/\n";           
        $data.= "DROP TABLE IF EXISTS `{$table}`;\n";

        $myquery="SHOW CREATE TABLE `{$table}`";
        $result = $GLOBALS['db_pdo']->query($myquery);

        $row = $result->fetch(); // fetch_row()
        $data.= $row[1].";\n";

        $myquery="SELECT * FROM `{$table}`";
        $result = $GLOBALS['db_pdo']->query($myquery);
        $num_rows = $result->rowCount();  // num_rows

        if($num_rows>0) {
          $vals = Array(); $z=0;
          for($i=0; $i<$num_rows; $i++) {
            $items = $result->fetch(PDO::FETCH_NUM);  // mysqli_fetch_row($result)
            //var_dump($items); exit;
            $vals[$z]="(";
            for($j=0; $j<count($items); $j++) {
                if (isset($items[$j])) { 
                    $vals[$z].= $GLOBALS['db_pdo']->quote($items[$j]); //real_escape_string($items
                } else { 
                    $vals[$z].= "NULL"; 
                } 
              if ($j<(count($items)-1)){ $vals[$z].= ","; }
            }
            $vals[$z].= ")"; $z++;
          }
          $data.= "INSERT INTO `{$table}` VALUES ";      
          $data .= "  ".implode(";\nINSERT INTO `{$table}` VALUES ", $vals).";\n";
        }
    }
    return $data;
}
于 2020-03-12T15:27:40.650 回答