사바이캅

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,276
13년 전 조회 5,799
13년 전 조회 1,441
13년 전 조회 2,702
13년 전 조회 1,816
13년 전 조회 775
13년 전 조회 2,485
13년 전 조회 2,231
13년 전 조회 931
13년 전 조회 877
13년 전 조회 3,366
13년 전 조회 1,772
13년 전 조회 803
13년 전 조회 649
13년 전 조회 8,303
13년 전 조회 4,793
13년 전 조회 7,984
13년 전 조회 945
13년 전 조회 1,581
13년 전 조회 1,987
13년 전 조회 1,387
13년 전 조회 2,890
13년 전 조회 1,107
13년 전 조회 1,308
13년 전 조회 807
13년 전 조회 1,324
13년 전 조회 1,522
13년 전 조회 1.3만
13년 전 조회 1,243
13년 전 조회 4,781
13년 전 조회 1,752
13년 전 조회 792
13년 전 조회 1,091
13년 전 조회 3,907
13년 전 조회 1,075
13년 전 조회 4,725
13년 전 조회 822
13년 전 조회 977
13년 전 조회 1,387
13년 전 조회 8,919
13년 전 조회 1,620
13년 전 조회 2,671
13년 전 조회 1,818
13년 전 조회 2,069
13년 전 조회 2,157
13년 전 조회 1,235
13년 전 조회 1,427
13년 전 조회 1.8만
13년 전 조회 2,473
13년 전 조회 4,510
13년 전 조회 1,836
13년 전 조회 2,254
13년 전 조회 2,313
13년 전 조회 1.6만
13년 전 조회 1,239
13년 전 조회 1,082
13년 전 조회 871
13년 전 조회 1,759
13년 전 조회 3,345
13년 전 조회 3,577
13년 전 조회 2,266
13년 전 조회 5,817
13년 전 조회 2,961
13년 전 조회 6,169
13년 전 조회 1,787
13년 전 조회 2.1만
13년 전 조회 1,066
13년 전 조회 1,551
13년 전 조회 1.7만
13년 전 조회 2,926
13년 전 조회 1,182
13년 전 조회 3,899
13년 전 조회 1,350
13년 전 조회 1,491
13년 전 조회 934
13년 전 조회 2,671
13년 전 조회 1,660
13년 전 조회 816
13년 전 조회 1,059
13년 전 조회 7,621
13년 전 조회 2,550
13년 전 조회 1,084
13년 전 조회 1,461
13년 전 조회 3,027
13년 전 조회 1,179
13년 전 조회 1.9만
13년 전 조회 3,098
13년 전 조회 2,814
13년 전 조회 4,982
13년 전 조회 2,098
13년 전 조회 973
13년 전 조회 1,151
13년 전 조회 1.6만
13년 전 조회 936
13년 전 조회 2,362
13년 전 조회 1,912
13년 전 조회 1,449
13년 전 조회 1,317
13년 전 조회 1,454
13년 전 조회 2.4만
🐛 버그신고