MySQL [MySql] DB 백업
페이지 정보

본문
<?php
/**
* Updated: IT HUB. heesung byeon
* Website: program1472.com
*/
//MySQL server and database
$dbhost = 'localhost';
$dbuser = '로그인아이디';
$dbpass = '로그인비번';
$dbname = isset($_GET['db_nm'])?$_GET['db_nm']:'*';
$tables = isset($_GET['tb_nm'])?$_GET['tb_nm']:'*';
//Call the core function
backup_tables($dbhost, $dbuser, $dbpass, $dbname, $tables);
//Core function
function backup_tables($host, $user, $pass, $databases = '*', $tables = '*') {
    $con = mysqli_connect($host,$user,$pass);
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        exit;
    }
    if($databases == '*'){
        $databases = array();
        $result = mysqli_query($con, 'SHOW DATABASES');
        while($row = mysqli_fetch_row($result))
        {
            $databases[] = $row[0];
        }
    } else {
        $databases = is_array($databases) ? $databases : explode(',',$databases);        
    }
    foreach($databases as $database)
    {
        mysqli_select_db($con, $database);
        mysqli_query($con, "SET NAMES 'utf8'");
        $table_array = array();
        //get all of the tables
        if($tables == '*')
        {            
            $result = mysqli_query($con, 'SHOW TABLES');
            while($row = mysqli_fetch_row($result))
            {
                $table_array[] = $row[0];
            }
        }
        else
        {
            $table_array = is_array($tables) ? $tables : explode(',',$tables);
        }
        $return = '';
        //cycle through
        foreach($table_array as $table)
        {
            $result = mysqli_query($con, 'SELECT * FROM '.$table);
            if ($result){
                $num_fields = mysqli_num_fields($result);
                $num_rows = mysqli_num_rows($result);
                $return.= 'DROP TABLE IF EXISTS '.$table.';';
                $row2 = mysqli_fetch_row(mysqli_query($con, '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-'.$database.'-'.time().'-'.(md5(implode(',',$tables))).'.sql';
        $fileName = 'db/backup-'.$database.'-'.time().'.sql';
        $handle = fopen($fileName,'w+');
        fwrite($handle,$return);
        if(fclose($handle)){
            echo "Done, the file name is: ".$fileName.'\r\n<br>';
            // exit; 
        }
    }
}
- 이전글[MySQL] SELECT 문 특정 컬럼의 MIN값이 0보다 클경우 0을 반환하고 그렇지 않으면 MIN값을 반환 23.07.12
 - 다음글[MySQL] A테이블에 있고, B테이블에 없는 데이터 조회 및 삭제 23.02.21
 
댓글목록
등록된 댓글이 없습니다.



