사바이캅

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,316
13년 전 조회 5,847
13년 전 조회 1,495
13년 전 조회 2,750
13년 전 조회 1,853
13년 전 조회 809
13년 전 조회 2,536
13년 전 조회 2,271
13년 전 조회 968
13년 전 조회 924
13년 전 조회 3,408
13년 전 조회 1,828
13년 전 조회 843
13년 전 조회 693
13년 전 조회 8,346
13년 전 조회 4,838
13년 전 조회 8,036
13년 전 조회 982
13년 전 조회 1,619
13년 전 조회 2,031
13년 전 조회 1,429
13년 전 조회 2,932
13년 전 조회 1,149
13년 전 조회 1,348
13년 전 조회 845
13년 전 조회 1,374
13년 전 조회 1,572
13년 전 조회 1.3만
13년 전 조회 1,297
13년 전 조회 4,832
13년 전 조회 1,800
13년 전 조회 838
13년 전 조회 1,136
13년 전 조회 3,945
13년 전 조회 1,116
13년 전 조회 4,770
13년 전 조회 861
13년 전 조회 1,016
13년 전 조회 1,435
13년 전 조회 8,962
13년 전 조회 1,673
13년 전 조회 2,711
13년 전 조회 1,858
13년 전 조회 2,107
13년 전 조회 2,209
13년 전 조회 1,278
13년 전 조회 1,473
13년 전 조회 1.8만
13년 전 조회 2,524
13년 전 조회 4,545
13년 전 조회 1,885
13년 전 조회 2,299
13년 전 조회 2,357
13년 전 조회 1.6만
13년 전 조회 1,275
13년 전 조회 1,125
13년 전 조회 924
13년 전 조회 1,815
13년 전 조회 3,394
13년 전 조회 3,619
13년 전 조회 2,323
13년 전 조회 5,863
13년 전 조회 3,006
13년 전 조회 6,224
13년 전 조회 1,839
13년 전 조회 2.1만
13년 전 조회 1,114
13년 전 조회 1,608
13년 전 조회 1.7만
13년 전 조회 2,976
13년 전 조회 1,222
13년 전 조회 3,939
13년 전 조회 1,392
13년 전 조회 1,537
13년 전 조회 987
13년 전 조회 2,723
13년 전 조회 1,699
13년 전 조회 855
13년 전 조회 1,102
13년 전 조회 7,670
13년 전 조회 2,602
13년 전 조회 1,129
13년 전 조회 1,500
13년 전 조회 3,071
13년 전 조회 1,230
13년 전 조회 2만
13년 전 조회 3,136
13년 전 조회 2,861
13년 전 조회 5,022
13년 전 조회 2,144
13년 전 조회 1,016
13년 전 조회 1,189
13년 전 조회 1.6만
13년 전 조회 982
13년 전 조회 2,407
13년 전 조회 1,952
13년 전 조회 1,493
13년 전 조회 1,368
13년 전 조회 1,503
13년 전 조회 2.4만