COMING SOON 🚀

사바이캅

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년 전 조회 5,380
13년 전 조회 6,931
13년 전 조회 2,559
13년 전 조회 3,811
13년 전 조회 2,918
13년 전 조회 1,892
13년 전 조회 3,592
13년 전 조회 3,287
13년 전 조회 2,059
13년 전 조회 1,946
13년 전 조회 4,502
13년 전 조회 2,887
13년 전 조회 1,877
13년 전 조회 1,790
13년 전 조회 9,413
13년 전 조회 5,938
13년 전 조회 9,091
13년 전 조회 2,024
13년 전 조회 2,676
13년 전 조회 3,116
13년 전 조회 2,519
13년 전 조회 4,036
13년 전 조회 2,276
13년 전 조회 2,420
13년 전 조회 1,873
13년 전 조회 2,477
13년 전 조회 2,617
13년 전 조회 1.4만
13년 전 조회 2,397
13년 전 조회 5,923
13년 전 조회 2,875
13년 전 조회 1,953
13년 전 조회 2,282
13년 전 조회 5,059
13년 전 조회 2,215
13년 전 조회 5,855
13년 전 조회 1,940
13년 전 조회 2,129
13년 전 조회 2,485
13년 전 조회 1만
13년 전 조회 2,748
13년 전 조회 3,853
13년 전 조회 2,975
13년 전 조회 3,183
13년 전 조회 3,317
13년 전 조회 2,308
13년 전 조회 2,513
13년 전 조회 1.9만
13년 전 조회 3,661
13년 전 조회 5,657
13년 전 조회 2,944
13년 전 조회 3,407
13년 전 조회 3,389
13년 전 조회 1.7만
13년 전 조회 2,302
13년 전 조회 2,212
13년 전 조회 1,972
13년 전 조회 2,924
13년 전 조회 4,493
13년 전 조회 4,635
13년 전 조회 3,455
13년 전 조회 6,905
13년 전 조회 4,110
13년 전 조회 7,272
13년 전 조회 2,931
13년 전 조회 2.2만
13년 전 조회 2,193
13년 전 조회 2,700
13년 전 조회 1.8만
13년 전 조회 3,980
13년 전 조회 2,300
13년 전 조회 4,999
13년 전 조회 2,475
13년 전 조회 2,630
13년 전 조회 2,063
13년 전 조회 3,824
13년 전 조회 2,815
13년 전 조회 1,979
13년 전 조회 2,144
13년 전 조회 8,725
13년 전 조회 3,618
13년 전 조회 2,241
13년 전 조회 2,521
13년 전 조회 4,135
13년 전 조회 2,311
13년 전 조회 2.1만
13년 전 조회 4,185
13년 전 조회 3,981
13년 전 조회 6,069
13년 전 조회 3,234
13년 전 조회 2,078
13년 전 조회 2,262
13년 전 조회 1.7만
13년 전 조회 2,062
13년 전 조회 3,405
13년 전 조회 2,959
13년 전 조회 2,544
13년 전 조회 2,412
13년 전 조회 2,565
13년 전 조회 2.5만