繁体   English   中英

PHP多数据库备份

[英]php multi database backup

我正在使用以下代码,正在寻找的代码工作正常,其他代码则无法正常工作。

我将函数调用5次以下,但仅工作1次。 我不知道他为什么要这么做;;;;;;;;;;;;;;;;;;;; ;;;;;;

<?php

//MySQL server and database
$dbhost = 'localhost';
$dbuser = 'my_user';
$dbpass = 'my_pwd';
$dbname = 'database_name';
$tables = '*';

//Call the core function
backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);

//Core function
function backup_tables($host, $user, $pass, $dbname, $tables = '*') {
    $link = mysqli_connect($host,$user,$pass, $dbname);

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    exit;
}

mysqli_query($link, "SET NAMES 'utf8'");

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

$return = '';
//cycle through
foreach($tables as $table)
{
    $result = mysqli_query($link, 'SELECT * FROM '.$table);
    $num_fields = mysqli_num_fields($result);
    $num_rows = mysqli_num_rows($result);

    $return.= 'DROP TABLE IF EXISTS '.$table.';';
    $row2 = mysqli_fetch_row(mysqli_query($link, 'SHOW CREATE TABLE '.$table));
    $return.= "\n\n".$row2[1].";\n\n";
    $counter = 1;

    //Over tables
    for ($i = 0; $i < $num_fields; $i++) 
    {   //Over rows
        while($row = mysqli_fetch_row($result))
        {   
            if($counter == 1){
                $return.= 'INSERT INTO '.$table.' VALUES(';
            } else{
                $return.= '(';
            }

            //Over fields
            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.= ','; }
            }

            if($num_rows == $counter){
                $return.= ");\n";
            } else{
                $return.= "),\n";
            }
            ++$counter;
        }
    }
    $return.="\n\n\n";
}

//save file
$fileName = 'db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql';
$handle = fopen($fileName,'w+');
fwrite($handle,$return);
if(fclose($handle)){
    echo "Done, the file name is: ".$fileName;
    exit; 
}
}

我打了5次

backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);
backup_tables($dbhost, $dbuser1, $dbpass1, $dbname1, $tables);
backup_tables($dbhost, $dbuser2, $dbpass2, $dbname2, $tables);
backup_tables($dbhost, $dbuser3, $dbpass3, $dbname3, $tables);
backup_tables($dbhost, $dbuser4, $dbpass4, $dbname4, $tables);

但只有一个被备份。 其他4个都不备份,为什么? 谢谢

只需移开出口即可;

if(fclose($handle)){
echo "Done, the file name is: ".$fileName;
//removed this exit
//exit; 
}

删除它后,我进行了测试,代码按预期工作: 结果

暂无
暂无

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

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