php 로 sql 백업후 db 복구할때 에러 메세지

wire101의 이미지

dbback.php 소스 입니다.

백업 잘됩니다. 2003년도 네xㅂ 지식인에 답변된 소스 파일 입니다.

백업한 파일을
mysql -utest -ptest testdb < backup.sql 했을때 .

ERROR 1062 (23000) at line 31: Duplicate entry '1' for key 1

-----------------------------------------------------------------
mysqldump -utest -ptest testdb > backup.sql 를
mysql -utest -ptest testdb < backup.sql 했을때는 에러가 없습니다.
-----------------------------------------------------------------

mysql은 5.0.27 사용 합니다.

dbback.php소스 내용 입니다.
-----------------------------------------------------------------
<?
$mysql_host = "localhost"; // 호스트정보
$mysql_db = ""; // 백업할 DB명
$mysql_user = ""; // 사용자 아이디
$mysql_pass = ""; // 사용자 비번

$fileName = date("Y_m_d_H_i"); //백업받을 파일명
$extention = "sql"; // 백업 받을 파일 확장자

$all = false; //완전한 Insert 문을 작성하려면 true
$drop = false; //drop table 구문을 포함하려면 true

mysql_connect( $mysql_host, $mysql_user, $mysql_pass ) || die("데이타 베이스 접속에 실패하였습니다.");
mysql_select_db( $mysql_db ) || die("DB 접속 실패");

function bak_getTableNames($db) {
$result[0] = mysql_list_tables($db);
$result[1] = mysql_num_rows($result[0]);
return $result;
}

function bak_getFields($table) {
global $all;
$result = mysql_query("show fields from $table");
$i = 0;
while($keys = mysql_fetch_array($result)) {
if(!$i) $defaultOrder = $keys[Field];
if($keys[Key] == 'PRI') {
$orderby = $keys[Field];
break;
}
$i++;
}
if(!$orderby) $orderby = $defaultOrder;
$result = mysql_query("select * from $table order by $orderby");
$nums = mysql_num_fields($result);
if($all == true) {
for($i=0;$i<$nums;$i++) {
$fields[] = mysql_field_name($result,$i);
}
$fields = "(".implode(",",$fields).") ";
}
while($rows = mysql_fetch_row($result)) {
for($i=0;$i<$nums;$i++) {
$insertValues[$i] = $rows[$i];
}
$return .= "INSERT INTO $table ".$fields."values ('".implode("','",$insertValues)."');\n";
}
return $return;
}

$tables = bak_getTableNames($mysql_db);
if($tables[1] > 0) {
$backText = "
# MysqlDump
# 호스트: ".$mysql_host."
# 처리한 시간: ".date('Y년 n월 j일 H시 i분')."
# 서버 버전: ".mysql_get_server_info()."
# 데이터베이스 : `".$mysql_db."`
# --------------------------------------------------------\n\n\n\n\n";
while($rows = mysql_fetch_row($tables[0])) {
$backText .= "
#
# 테이블 구조 `".$rows[0]."`
#\n\n";
$skima = mysql_fetch_array(mysql_query("SHOW CREATE TABLE ".$mysql_db.".".$rows[0]));
if($drop == true) $backText .= "DROP TABLE IF EXISTS ".$skima[0].";\n\n";
$backText .= "CREATE TABLE if not exists ".$skima[0]." ";
$pos = strpos($skima[1]," (");
$backText .= str_replace("`","",substr($skima[1],($pos+1))).";";

$insert = bak_getFields($rows[0]);
$backText .= "
#
# 테이블 '".$rows[0]."' insert 문
#";
if($insert) $backText .= "\n\n\n".$insert;
$backText .= "\n\n\n\n\n";
}

header("Content-Type: application/octetstream");
header("Content-Disposition: attachment; filename=".$fileName.".".$extention);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo $backText;
}
?>

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.