자료실

부자는 돈을 써서 시간을 아끼지만 가난한 사람은 시간을 써서 돈을 아낀다

MySQL

IT HUB를 찾아주셔서 감사합니다.

MySQL [MySql] DB 백업

페이지 정보

profile_image
작성자 하나를하더라도최선을
댓글 0건 조회 3,042회 작성일 23-06-07 17:04

본문

<?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; 
        }
    }
}

댓글목록

등록된 댓글이 없습니다.