2013년 8월 8일 목요일

mysql db schema dump php script

<?php
// Delete the tables if they exists?
$drop = true;
$dumpdata = false;

$user = 'user';
$passwd = 'password';
$host = '127.0.0.1';
$port = 3306;
$db = 'dbname';
db($host, $user, $passwd, $db, $port, $drop, $dumpdata);

function db($host, $user, $passwd, $db, $port, $drop, $dumpdata) {
    // DB Connection
    $mysqli = new Mysqli($host, $user, $passwd, $db, $port);
    if($mysqli->connect_error) die("Can't connect to MySql server: " . $mysqli->connect_errno .' => '. $mysqli->connect_error);
    $mysqli->set_charset('utf8');
    // Search the table in the database
    $tables = array();
    $query = "SHOW TABLES FROM $db;";
    $rs = $mysqli->query($query, MYSQLI_STORE_RESULT) or die("Can't execute the query: ".$mysqli->error);
    while ($row = $rs->fetch_array(MYSQLI_NUM)){
            $tables[] = $row[0];
    }
    // Create the header archive
    $info['date'] = date("d-m-Y");
    $info['time'] = date("h:m:s A");
    $info['mysqlver'] = $mysqli->server_info;
    $info['phpver'] = phpversion();
    print_r($info);
    // Dump variable
    $dump= "";
    foreach ($tables as $table){
        $drop_table_query = "";
        $create_table_query = "";
        $insert_into_query = "";
        // Start the query that create the db.
        if ($drop){
                $drop_table_query .= "DROP TABLE IF EXISTS `$table`;" . PHP_EOL;
        } else {
                $drop_table_query .= "# No specified." . PHP_EOL;
        }
        $query = "SHOW CREATE TABLE $table;";
        $rs = $mysqli->query($query, MYSQLI_STORE_RESULT) or die("Can't execute the query: ".$mysqli->error);
        while ($row = $rs->fetch_array(MYSQLI_NUM)){
                $create_table_query .= $row[1].";";
        }
        // This query insert the datas.
        $query = "SELECT * FROM $table;";
        $rs = $mysqli->query($query, MYSQLI_STORE_RESULT) or die("Can't execute the query: ".$mysqli->error);
        while ($row = $rs->fetch_array(MYSQLI_NUM)){
                $columns = array_keys($row);
                foreach ($columns as $column){
                        if ( gettype($row[$column]) == "NULL" ){
                                $values[] = "NULL";
                        } else {
                                $values[] = "'".$mysqli->real_escape_string($row[$column])."'";
                        }
                }
                $insert_into_query .= "INSERT INTO `$table` VALUES (".implode(", ", $values).");" . PHP_EOL;
                unset($values);
        }
    $dump .= "# | Empty Table '$table'" . PHP_EOL;
    $dump .= "# +------------------------------------->" . PHP_EOL;
    $dump .= $drop_table_query . PHP_EOL;
    $dump .= PHP_EOL;
    $dump .= "# | Structure of table '$table'" . PHP_EOL;
    $dump .= "# +------------------------------------->" . PHP_EOL;
    $dump .= $create_table_query . PHP_EOL;
    $dump .= PHP_EOL;
    if($dumpdata) {
        $dump .= "# | Data loading of table '$table'" . PHP_EOL;
        $dump .= "# +------------------------------------->" . PHP_EOL;
        $dump .= $insert_into_query . PHP_EOL;
        $dump .= PHP_EOL;
    }
    }
    createdumpfile($dump, $db);
    $mysqli->close();
}
function createdumpfile($dump, $db) {
    $myFile = $db.'.sql';
    // if the backup exists we delete and rewrite it
    if (file_exists($myFile)){
    unlink($myFile);
    }
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $dump);
    fclose($fh);
}

댓글 없음:

댓글 쓰기