如何将数据库备份文件转储到计算机的另一个磁盘和网络电脑?

时间:2014-11-12 03:40:38

标签: php

我使用此代码将数据库备份文件转储到名为 db_backup 的htdocs文件夹中,并选择下载备份文件。

<?php
function export_tables($host,$user,$pass,$name,  $tables=false, $backup_name=false )
{
    $link = mysqli_connect($host,$user,$pass,$name);
    // Check connection
    if (mysqli_connect_errno())   {   echo "Failed to connect to MySQL: " . mysqli_connect_error();   }

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

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

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

        for ($i = 0; $i < $num_fields; $i++) 
        {
            $st_counter= 0;
            while($row = mysqli_fetch_row($result))
            {
                //create new command if when starts and after 100 command cycle
                if ($st_counter%100 == 0 || $st_counter == 0 )  {
                    $return.= "\nINSERT INTO `".$table."` VALUES";
                }


                $return.="\n(";
                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.=")";


                //create new command if when starts and after 100 command cycle (but detect this 1 cycle earlier !)
                if ( ($st_counter+1)%100 == 0  && $st_counter != 0 )    {   $return.= ";";  }
                else                                                {   $return.= ",";  }
                //+++++++
                $st_counter = $st_counter +1 ;
            }
            //as we cant detect WHILE loop end, so, just detect, if last command ends with comma(,) then replace it with semicolon(;)
            if (substr($return, -1) == ',') {$return = substr($return, 0, -1). ';'; }
        }
        $return.="\n\n\n";
    }

    //save file
    $backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').').sql';
    file_put_contents('db_backup/'.$backup_name,$return);
    echo 'SUCCESS. Download BACKUP file: <a target="_blank" href="'.'db_backup/'.$backup_name.'">'.$backup_name.'</a>';

}

if (!empty($_GET['delete_filee'])){ chdir(dirname(__file__));       
    if  (unlink($_GET['delete_filee'])) {die('file_deleted');} 
    else                                {die("file doesnt exist");}
}
?>

执行人:

<?php
                include("../dbbackup_function.php");
                export_tables("localhost","root","password","isys");
            ?>

我想知道如何将它保存在我的PC / Server和网络PC中的另一个磁盘中。

对于磁盘,我试过了:

file_put_contents('D:/db_backup/'.$backup_name,$return);
        echo 'SUCCESS. Download BACKUP file: <a target="_blank" href="'.'D:/db_backup/'.$backup_name.'">'.$backup_name.'</a>';

转储工作正常,但下载并不适用。它错误:

enter image description here

那么如何解决此问题的下载错误?

对于网络电脑,我试过:

file_put_contents('//DevServer/Users/Administrator/Downloads/test/'.$backup_name,$return);
            echo 'SUCCESS. Download BACKUP file: <a target="_blank" href="'.'//DevServer/Users/Administrator/Downloads/test/'.$backup_name.'">'.$backup_name.'</a>';

转储错误:

  

警告:file_put_contents(// DevServer / Users / Administrator / Downloads / test / test ___(03-25-01_12-11-2014).sql)[function.file-put-contents]:无法打开流:权限在第71行的E:\ xampp \ htdocs \ sample \ test2.php中被拒绝

下载错误找不到对象

那么如何访问网络PC?我有凭据,我只是不知道如何通过代码来完成。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

当您链接到存储在本地(或联网)驱动器上的文件时,请确保在路径前加file://

How to open a pdf file located in a local c drive

处的类似问题

答案 1 :(得分:0)

对于MySQL转储,您可以:

function export_tables($host, $user, $pass, $name, $tables=false, $backup_name=false )
{
    $tables = is_array($tables) ? implode(" ",$tables) : "";
    $backup_name = $backup_name ? $backup_name : $name."___(".date('H-i-s')."_".date('d-m-Y').').sql';
    $backup_path = dirname(__FILE__).DIRECTORY_SEPARATOR."db_backup".DIRECTORY_SEPARATOR.$backup_name;

    //****this part can be omited if mysqldump is a system variable
    $link = mysqli_connect($host,$user,$pass,$name);
    if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
    mysqli_select_db($link,$name);
    $mysql_home = mysqli_fetch_assoc(mysqli_query($link, "SELECT @@basedir as mysql_home"));
    $mysql_path = str_replace("/", DIRECTORY_SEPARATOR, $mysql_home["mysql_home"]."/bin/mysqldump");

    //****so instead of {$mysql_path} --> mysqldump
    exec("{$mysql_path} --add-drop-table --host={$host} --user={$user} --password={$pass} {$name} {$tables} > {$backup_path}");

    //this part is consider a security vulnerability so limited in some web browsers
    //here you check some answers for this (by @Spiral57):  http://stackoverflow.com/questions/23759756/how-to-open-href-to-local-file
    echo 'SUCCESS. Download BACKUP file: <a target="_blank" href="file:///'.$backup_path.'">'.$backup_name.'</a>';
}

并使用它:

export_tables("localhost","root","password","isys",["table1","table2"]);