사바이캅

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,349
13년 전 조회 5,866
13년 전 조회 1,520
13년 전 조회 2,771
13년 전 조회 1,871
13년 전 조회 836
13년 전 조회 2,561
13년 전 조회 2,293
13년 전 조회 990
13년 전 조회 944
13년 전 조회 3,431
13년 전 조회 1,850
13년 전 조회 860
13년 전 조회 717
13년 전 조회 8,366
13년 전 조회 4,857
13년 전 조회 8,064
13년 전 조회 1,000
13년 전 조회 1,645
13년 전 조회 2,062
13년 전 조회 1,453
13년 전 조회 2,959
13년 전 조회 1,176
13년 전 조회 1,371
13년 전 조회 864
13년 전 조회 1,402
13년 전 조회 1,587
13년 전 조회 1.3만
13년 전 조회 1,319
13년 전 조회 4,864
13년 전 조회 1,826
13년 전 조회 871
13년 전 조회 1,159
13년 전 조회 3,973
13년 전 조회 1,140
13년 전 조회 4,795
13년 전 조회 886
13년 전 조회 1,045
13년 전 조회 1,467
13년 전 조회 8,981
13년 전 조회 1,702
13년 전 조회 2,743
13년 전 조회 1,882
13년 전 조회 2,133
13년 전 조회 2,234
13년 전 조회 1,303
13년 전 조회 1,499
13년 전 조회 1.8만
13년 전 조회 2,557
13년 전 조회 4,567
13년 전 조회 1,907
13년 전 조회 2,316
13년 전 조회 2,396
13년 전 조회 1.6만
13년 전 조회 1,303
13년 전 조회 1,166
13년 전 조회 957
13년 전 조회 1,845
13년 전 조회 3,423
13년 전 조회 3,647
13년 전 조회 2,353
13년 전 조회 5,896
13년 전 조회 3,032
13년 전 조회 6,252
13년 전 조회 1,865
13년 전 조회 2.1만
13년 전 조회 1,141
13년 전 조회 1,629
13년 전 조회 1.7만
13년 전 조회 3,003
13년 전 조회 1,254
13년 전 조회 3,973
13년 전 조회 1,422
13년 전 조회 1,562
13년 전 조회 1,012
13년 전 조회 2,755
13년 전 조회 1,728
13년 전 조회 885
13년 전 조회 1,130
13년 전 조회 7,702
13년 전 조회 2,631
13년 전 조회 1,165
13년 전 조회 1,526
13년 전 조회 3,105
13년 전 조회 1,257
13년 전 조회 2만
13년 전 조회 3,172
13년 전 조회 2,897
13년 전 조회 5,051
13년 전 조회 2,174
13년 전 조회 1,049
13년 전 조회 1,224
13년 전 조회 1.6만
13년 전 조회 1,001
13년 전 조회 2,432
13년 전 조회 1,986
13년 전 조회 1,527
13년 전 조회 1,404
13년 전 조회 1,531
13년 전 조회 2.4만