mysqldump로 백업시에 where조건에 맞는 데이터만 백업하기

이번에는 좀 특이한 백업방법을 소개해 드리겠습니다.   

즉, 지금까지의 백업방법은 대부분 로컬서버나 또는 원격서버의 MYSQL의 특정 데이터베이스를 백업대상으로 하거나 또는
특정 데이터베이스의 테이블들, 또는 데이터베이스 스키마등만을 그 대상으로 하여 백업하였습니다.   

하지만 이번 예에서는 특정 데이터베이스 내에 실제 데이터들 가운데 특정조건에 맞는 데이터만을 백업하는 방법을 소개할까합니다. 

즉 특정 데이터베이스 전체를 대상으로 한 것도 아니고 특정 데이터베이스의 테이블 전체를 대상으로하는 백업도 아닌 
특정 테이블의 데이터(레코드값)들 가운데 조건에 해당하는 데이터(레코드값)만을 골라서 백업하는 방법을 의미합니다.

이와 같은 백업을 하려면 --where옵션을 사용하여 조건문을 지정해 주시면 됩니다.

사용하는 형식은 다음과 같습니다.   

사용형식 : mysqldump -u root -p --where=“WHERE조건문” DB명 테이블명 > 파일명
(위의 형식에서 --where대신에 -w를 사용할 수도 있습니다.)

아래의 예는 temp_db2데이터베이스 내의 Demo_DomainAdmin이라는 테이블의 실제 데이터들 가운데
admin_id필드값이 “admin”인 레코드만을 백업하여 temp_db2.sql파일에 저장하는 예입니다.

즉, --where=“admin_id=‘admin’”이라는 조건문 때문에 이와 같은 백업이 가능한 것입니다.

[root@file bin]# ./mysqldump -u root -p --where="admin_id='admin'" temp_db2  Demo_DomainAdmin > ./temp_db2.sql

Enter password: ********

[root@file bin]#

[root@file bin]# ls -l temp_db2.sql

-rw-r--r--    1 root     root          506  2월 14 16:46 temp_db2.sql

[root@file bin]#

위와 같이 백업된 temp_db2.sql파일의 내용을 보시면 다음과 같습니다.   

아래의 결과를 보시면 데이터베이스 스키마와 --where조건에 맞는 데이터만이 저장되어 있다는 것을 알 수 있습니다.

[root@file bin]# cat temp_db2.sql

-- MySQL dump 9.08
--
-- Host: localhost    Database: temp_db2

---------------------------------------------------------

-- Server version       4.0.14
--
-- Table structure for table 'Demo_DomainAdmin'
--

CREATE TABLE Demo_DomainAdmin (

  admin_id varchar(12) NOT NULL default '',

  admin_pass varchar(40) default NULL,

  PRIMARY KEY  (admin_id)

) TYPE=MyISAM;

--
-- Dumping data for table 'Demo_DomainAdmin'
--
-- WHERE:  admin_id='admin'

INSERT INTO Demo_DomainAdmin VALUES ('admin','6ffffffffffff996d4');

[root@file bin]#

이제 실무적인 관점에서 한가지만 더 알려드리겠습니다.

위의 방법에서 -t옵션을 추가하여 백업하신 다면 데이터베이스 스키마를 제외한 실질적으로 --where조건에 맞는 순수한 데이터만을 저장할 수 있습니다. 

아래의 예는 바로 앞의 mysqldump명령어에 -t옵션만을 추가한 것입니다.   

즉 위의 결과에서 생성된 데이터베이스 스키마를 제외한 순수한 --where조건에 맞는 데이터만을 저장하기 위한 백업입니다.

[root@file bin]# ./mysqldump -u root -p -t --where="admin_id='admin'" temp_db2  Demo_DomainAdmin > ./temp_db2.sql

Enter password: ********

[root@file bin]#

아래는 그 결과를 나타낸 것입니다.   

확인해 보시면 아시겠지만 앞의 결과에서 데이터베이스 스키마를 생성하는 “CREATE TABLE”문이 생략되고
순수한 --where조건에 맞는 결과만이 저장되었다는 것을 확인 할 수 있습니다.   

[root@file bin]# cat temp_db2.sql

-- MySQL dump 9.08
--
-- Host: localhost    Database: temp_db2

---------------------------------------------------------

-- Server version       4.0.14
--
-- Dumping data for table 'Demo_DomainAdmin'
--
-- WHERE:  admin_id='admin'

INSERT INTO Demo_DomainAdmin VALUES ('admin','6ffffffffffff996d4');

[root@file bin]#

실제로 이와 같은 백업방법은 좀 까다로와 보이긴 하지만 잘 활용하신다면 실무에서 매무 강력한 힘을 발휘합니다.   

즉 수많은 데이터들이 저장되어 있는 특정 테이블의 값들 가운데 특정 조건에 맞는 데이터만을 뽑아내려고 할 때에 가장 현실적이고 현명한 방법이기 때문입니다.     

출처 : https://www.linux.co.kr/bbs/board.php?bo_table=lecture&wr_id=3060

mysqldump명령어로 백업할 때에 -d옵션을 사용하면 백업대상 데이터베이스의 스키마(테이블구조)만을 백업 결과파일에 저장해준다.

즉 데이터값(레코드값)을 생성하는 SQL문은 결과파일에 저장되지 않는다는 의미이다. 사용하는 형식은 다음과 같다.

사용형식 : mysqldump -u DB사용자명 -p -d  DB명 > 파일명 (DB명의 전체 테이블의 스키마 백업할 때)
사용형식 : mysqldump -u DB사용자명 -p -d DB명 테이블명 > 파일명 (DB명의 특정테이블만 스키마 백업할 때)

위의 형식에서 -d옵션 대신에 --no-data를 사용할 수도 있다.

 

Rockylinux8 에서의 PHP 8.3 컴파일 시 나올 수 있는 에러와 이에 대한 대응방법 정리.

1. mbstring 관련

/usr/bin/ld: dynamic STT_GNU_IFUNC symbol `mb_utf16be_to_wchar' with pointer 
equality in `ext/mbstring/libmbfl/filters/mbfilter_utf16.o' can not be used 
when making an executable; recompile with -fPIE and relink with -pie
collect2: error: ld returned 1 exit status
make: *** [Makefile:307: sapi/cli/php] Error 1

해결책 (컴파일 옵션에 CFLAGS="-fPIE" LDFLAGS="-pie"  추가)

./configure CFLAGS="-fPIE" LDFLAGS="-pie"

원문 : http://www.ischo.net -- 조인상 // 시스템 엔지니어

Writer : http://www.ischo.net -- ischo // System Engineer in Replubic Of Korea

+++++++++++++++++++++++++++++++++++++++++++++++++++++++

본문 : http://www.ischo.net -- 조인상 //시스템 엔지니어

+++++++++++++++++++++++++++++++++++++++++++++++++++++++



아래와 같은 오류가 발생하면, 99.9% /etc/mail/local-host-names 설정이 잘못되어 있기 때문이다.

  ----- The following addresses had permanent fatal errors -----
<center@center.co.kr>
  (reason: 553 5.3.5 system config error)

  ----- Transcript of session follows -----
553 5.3.5 217.129.242.1. config error: mail loops back to me (MX problem?)
554 5.3.5 Local configuration error


local-host-names 파일에 center.co.kr 도메인 이름을 추가하고,

sendmail 데몬을 다시 시작하면, 메일을 받을 수 있다.

물론, center.co.kr 도메인에 대한 MX 레코드가 해당 메일 서버로 지정되어 있어야만 한다.

 

출처 : https://ischo.net/bd_sun/757

출처 : http://coffeenix.net/board_view.php?bd_code=1678

제  목 : find명령에서 특정 디렉토리 제외하고 찾기(exclude)
작성자 : 좋은진호(truefeel, http://coffeenix.net/ )
작성일 : 2009.7.3(금)

find 명령을 할 때, 특정 디렉토리 제외하고 find하는 방법은 없느냐고 주위분이 물어오셨다. 
개인적으로는 find에서 특정 디렉토리를 exclude하는 것 대신에 디렉토리를 나열하는 방법을 사용했다. 그러나 디렉토리 갯수가 많고, 같은 depth의 디렉토리가 아니고 하위 특정 디렉토리 몇개를 exclude해야한다면? 옵션을 활용할 수 밖에 없을 것이다.

man페이지를 확인해보니 -prune옵션이 있다. -prune 옵션은 찾아낸 것이 디렉토리이면 그 디렉토리내에는 find하지 않는다.

 
-name pattern
       ... 생략 ...
       To ignore a directory and the files under it, use -prune; see an example in the description of -wholename.

-prune If -depth is not given, true; if the file is a directory, do not descend into it.
       If -depth is given, false; no effect.
 



1. 예제로 살펴보기

[예제 1] 2009가 포함된 파일을 찾는다. 단 파일명이 디렉토리에 해당되면 해당 디렉토리 이하는 찾지 않는다.

find (전체 보기)
./
./check/check_list_2008_12.php
./check/check_list_2009_05.php
./check/check_list_2009_06.php
./2007_10_mobile
./2008_01_highlight
./2008_01_highlight/view.pl
./2008_01_highlight/view.sh
./2009_03_MSIE8
./2009_03_MSIE8/get_MSIE8.sh
./2009_05_apache
./2009_05_game
./2009_05_game/2009_05.log
find . -name "*2009*" -prune
./check/check_list_2009_05.php
./check/check_list_2009_06.php
./2009_03_MSIE8
./2009_05_apache
./2009_05_game

./2009_05_game/ 디렉토리 이하에 2009_05.log 파일이 있지만, -prune옵션으로 해당 디렉토리 이하는 찾지 않았다.

[예제 2] ./foo/bar 디렉토리는 제외하고, *.txt 파일을 찾아라.

find . ! \( -path './foo/bar' -prune \) -name "*.txt"
find . ! \( -type d -path './foo/bar' -prune \) -name "*.txt" (보다 정확한 표현)

[예제 3] ./foo/bar 디렉토리와 ./coffeenix/temp 디렉토리는 제외하고, *.bak 파일을 찾아라.

find . ! \( \( -path './foo/bar' -o -path './coffeenix/temp' \) -prune \) -name "*.bak"
find . ! \( \( -type d -path './foo/bar' -o -path './coffeenix/temp' \) -prune \) -name "*.bak" (보다 정확한 표현)

[예제 4] ./2008로 시작하는 디렉토리는 제외하고, .*.bak 파일을 찾아라.

find . ! \( -path './2008*' -prune \) -name "*.bak"
find . ! \( -type d -path './2008*' -prune \) -name "*.bak" (보다 정확한 표현)

-path 대신 -wholename 옵션을 사용해도 된다.

 
-path pattern
      See -wholename.   The predicate -path is also supported by HP-UX find.

-wholename pattern
      File name matches shell pattern pattern.  The metacharacters do not treat `/' or `.' specially; so, for example,
                find . -wholename './sr*sc'
      will  print  an entry for a directory called './src/misc' (if one exists).  To ignore a whole directory tree, use -prune rather
      than checking every file in the tree.  For example, to skip the directory `src/emacs' and all files and directories  under  it,
      and print the names of the other files found, do something like this:
                find . -wholename './src/emacs' -prune -o -print
 



2. 참고자료

* find manpage
  http://unixhelp.ed.ac.uk/CGI/man-cgi?find

* 유용한 find 명령어 예 모음 (2003, 글 좋은진호)
  http://coffeenix.net/board_view.php?bd_code=36