사바이캅

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,275
13년 전 조회 5,795
13년 전 조회 1,439
13년 전 조회 2,700
13년 전 조회 1,814
13년 전 조회 774
13년 전 조회 2,482
13년 전 조회 2,230
13년 전 조회 927
13년 전 조회 875
13년 전 조회 3,363
13년 전 조회 1,770
13년 전 조회 801
13년 전 조회 648
13년 전 조회 8,297
13년 전 조회 4,788
13년 전 조회 7,983
13년 전 조회 939
13년 전 조회 1,577
13년 전 조회 1,986
13년 전 조회 1,385
13년 전 조회 2,888
13년 전 조회 1,104
13년 전 조회 1,307
13년 전 조회 807
13년 전 조회 1,321
13년 전 조회 1,518
13년 전 조회 1.3만
13년 전 조회 1,241
13년 전 조회 4,776
13년 전 조회 1,750
13년 전 조회 791
13년 전 조회 1,089
13년 전 조회 3,903
13년 전 조회 1,074
13년 전 조회 4,724
13년 전 조회 819
13년 전 조회 976
13년 전 조회 1,384
13년 전 조회 8,917
13년 전 조회 1,618
13년 전 조회 2,669
13년 전 조회 1,815
13년 전 조회 2,065
13년 전 조회 2,154
13년 전 조회 1,232
13년 전 조회 1,425
13년 전 조회 1.8만
13년 전 조회 2,472
13년 전 조회 4,506
13년 전 조회 1,833
13년 전 조회 2,253
13년 전 조회 2,313
13년 전 조회 1.6만
13년 전 조회 1,236
13년 전 조회 1,079
13년 전 조회 870
13년 전 조회 1,757
13년 전 조회 3,344
13년 전 조회 3,576
13년 전 조회 2,262
13년 전 조회 5,817
13년 전 조회 2,958
13년 전 조회 6,164
13년 전 조회 1,785
13년 전 조회 2.1만
13년 전 조회 1,064
13년 전 조회 1,549
13년 전 조회 1.7만
13년 전 조회 2,925
13년 전 조회 1,181
13년 전 조회 3,897
13년 전 조회 1,348
13년 전 조회 1,489
13년 전 조회 931
13년 전 조회 2,667
13년 전 조회 1,657
13년 전 조회 814
13년 전 조회 1,056
13년 전 조회 7,618
13년 전 조회 2,547
13년 전 조회 1,083
13년 전 조회 1,459
13년 전 조회 3,023
13년 전 조회 1,174
13년 전 조회 1.9만
13년 전 조회 3,098
13년 전 조회 2,812
13년 전 조회 4,980
13년 전 조회 2,093
13년 전 조회 971
13년 전 조회 1,147
13년 전 조회 1.6만
13년 전 조회 935
13년 전 조회 2,362
13년 전 조회 1,911
13년 전 조회 1,447
13년 전 조회 1,313
13년 전 조회 1,452
13년 전 조회 2.4만
🐛 버그신고