PHP 模仿 phpmyadmin 備份和還原(PHP imitate phpmyadmin backup and restore)

PHP 模仿 phpmyadmin 備份和還原(PHP imitate phpmyadmin backup and restore)

PHP 模仿 phpmyadmin 備份和還原(PHP imitate phpmyadmin backup and restore)

資料來源:https://davidwalsh.name/backup-mysql-database-php
資料來源:http://stackoverflow.com/questions/19751354/how-to-import-sql-file-in-mysql-database-using-php

程式碼和相關檔案以備份到GITHUB:https://github.com/jash-git/PHP-imitate-phpmyadmin-backup-and-restore

 

backup_tables.php

<?php
//資料來源:https://davidwalsh.name/backup-mysql-database-php
function backup_tables($host,$user,$pass,$name,$tables = ‘*’)
{
    
    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);

      mysql_query(“SET NAMES utf8;”);
      mysql_query(“SET CHARACTER_SET_CLIENT=utf8;”);
      mysql_query(“SET CHARACTER_SET_RESULTS=utf8;”);      
      
    //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 ‘.$table.’;’;
        $row2 = mysql_fetch_row(mysql_query(‘SHOW CREATE TABLE ‘.$table));
        $return.= “\n\n”.$row2[1].”;\n\n”;
        
        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] = ereg_replace(“\n”,”\\n”,$row[$j]);
                    if (isset($row[$j])) { $return.= ‘”‘.$row[$j].'”‘ ; } else { $return.= ‘””‘; }
                    if ($j < ($num_fields-1)) { $return.= ‘,’; }
                }
                $return.= “);\n”;
            }
        }
        $return.=”\n\n\n”;
    }
    
    //save file
    $handle = fopen(‘db-backup-‘.time().’-‘.(md5(implode(‘,’,$tables))).’.sql’,’w+’);
    fwrite($handle,$return);
    fclose($handle);
}
    header(‘content-type:text/html;charset=utf-8’);
    set_time_limit(0);    
    backup_tables(‘localhost’,’root’,’usbw’,’CSV_Import_Export’);
?>

import-sql.php

<?php
//資料來源:http://stackoverflow.com/questions/19751354/how-to-import-sql-file-in-mysql-database-using-php
header(‘content-type:text/html;charset=utf-8’);
set_time_limit(0);    
// Name of the file
$filename = ‘db-backup.sql’;
// MySQL host
$mysql_host = ‘localhost’;
// MySQL username
$mysql_username = ‘root’;
// MySQL password
$mysql_password = ‘usbw’;
// Database name
$mysql_database = ‘CSV_Import_Export’;

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die(‘Error connecting to MySQL server: ‘ . mysql_error());
// Select database
mysql_select_db($mysql_database) or die(‘Error selecting MySQL database: ‘ . mysql_error());

mysql_query(“SET NAMES utf8;”);
mysql_query(“SET CHARACTER_SET_CLIENT=utf8;”);
mysql_query(“SET CHARACTER_SET_RESULTS=utf8;”);

// Temporary variable, used to store current query
$templine = ”;
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
// Skip it if it’s a comment
if (substr($line, 0, 2) == ‘–‘ || $line == ”)
    continue;

// Add this line to the current segment
$templine .= $line;
// If it has a semicolon at the end, it’s the end of the query
if (substr(trim($line), -1, 1) == ‘;’)
{
    // Perform the query
    mysql_query($templine) or print(‘Error performing query \'<strong>’ . $templine . ‘\’: ‘ . mysql_error() . ‘<br /><br />’);
    // Reset temp variable to empty
    $templine = ”;
}
}
 echo “Tables imported successfully”;
?>

 

 

 

 

 

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *