사바이캅

php 스크립트로 간단히 mysql 백업하기

phpmyadmin 들어가지않고 
php로 간단하게 sql 백업 받는 소스입니다.

backup_tables('localhost','ID','PW','DB');  의 코드를 본인의 계정에 맞게
수정하셔서 쓰시면 됩니다. 
코드를 실행하면 20121111.sql 처럼 파일이 생성됩니다.

[code]
<?php
backup_tables('localhost','ID','PW','DB');



function backup_tables($host,$user,$pass,$name,$tables = '*')
{
  
  $link = mysql_connect($host,$user,$pass);
  mysql_select_db($name,$link);
  
  //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);
  }
  

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

  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
  fwrite($handle,$return);
  fclose($handle);
}
?>
[/code]
|

댓글 5개

좋네요
유용하게 쓰겠습니다~
엌 편하네요 감사합니다!
감사합니다.
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 4,949
13년 전 조회 6,496
13년 전 조회 2,018
13년 전 조회 3,361
13년 전 조회 2,366
13년 전 조회 1,334
13년 전 조회 3,075
13년 전 조회 2,797
13년 전 조회 1,503
13년 전 조회 1,474
13년 전 조회 3,918
13년 전 조회 2,348
13년 전 조회 1,380
13년 전 조회 1,237
13년 전 조회 8,872
13년 전 조회 5,373
13년 전 조회 8,576
13년 전 조회 1,536
13년 전 조회 2,146
13년 전 조회 2,686
13년 전 조회 1,979
13년 전 조회 3,565
13년 전 조회 1,710
13년 전 조회 1,858
13년 전 조회 1,370
13년 전 조회 1,907
13년 전 조회 2,112
13년 전 조회 1.3만
13년 전 조회 1,833
13년 전 조회 5,409
13년 전 조회 2,349
13년 전 조회 1,380
13년 전 조회 1,689
13년 전 조회 4,605
13년 전 조회 1,660
13년 전 조회 5,304
13년 전 조회 1,398
13년 전 조회 1,560
13년 전 조회 1,980
13년 전 조회 9,515
13년 전 조회 2,218
13년 전 조회 3,290
13년 전 조회 2,492
13년 전 조회 2,656
13년 전 조회 2,793
13년 전 조회 1,818
13년 전 조회 2,007
13년 전 조회 1.8만
13년 전 조회 3,111
13년 전 조회 5,139
13년 전 조회 2,443
13년 전 조회 2,871
13년 전 조회 2,919
13년 전 조회 1.7만
13년 전 조회 1,810
13년 전 조회 1,677
13년 전 조회 1,471
13년 전 조회 2,480
13년 전 조회 3,936
13년 전 조회 4,166
13년 전 조회 2,971
13년 전 조회 6,418
13년 전 조회 3,600
13년 전 조회 6,803
13년 전 조회 2,419
13년 전 조회 2.2만
13년 전 조회 1,662
13년 전 조회 2,187
13년 전 조회 1.7만
13년 전 조회 3,512
13년 전 조회 1,758
13년 전 조회 4,519
13년 전 조회 1,938
13년 전 조회 2,074
13년 전 조회 1,529
13년 전 조회 3,280
13년 전 조회 2,248
13년 전 조회 1,420
13년 전 조회 1,663
13년 전 조회 8,239
13년 전 조회 3,136
13년 전 조회 1,727
13년 전 조회 2,050
13년 전 조회 3,699
13년 전 조회 1,785
13년 전 조회 2만
13년 전 조회 3,778
13년 전 조회 3,422
13년 전 조회 5,639
13년 전 조회 2,712
13년 전 조회 1,556
13년 전 조회 1,750
13년 전 조회 1.7만
13년 전 조회 1,480
13년 전 조회 2,946
13년 전 조회 2,510
13년 전 조회 2,014
13년 전 조회 1,887
13년 전 조회 2,023
13년 전 조회 2.4만