Linux ETC

rsync를 사용하여 데이터 백업 하기

2009.04.25 00:42

jook 조회 수:25179

rsync는 873번 포트를 사용하며, 두 서버간의 데이터 백업을 위해서 주로 사용된다.
master서버에서 rsync데몬을 띄우고, backup 서버에서 master서버에 접근하여 허용된 디렉토리의 데이터를 백업하는 방식과, ssh를 이용하여 데이터를 미러링 하는 방법 두가지가 있다.

아래의 예제는 debian5.0에서 제작 되었으며, master서버는 10.30.100.60, backup서버는 10.30.100.61 이다.


rsync데몬(873포트)을 이용하는 방법


1. /etc/rsyncd.conf 파일 생성

/etc/rsyncd.conf 파일은 기본적으로 생성되어 있지 않기 때문에 아래와 같은 형식으로 생성 한다.
========================================
jook-debian1:~# cat /etc/rsyncd.conf
[jook]
comment = jook's home directory
path = /home/jook/public_html
uid = root
gid = root
use chroot = yes
read only = yes
hosts allow = 10.30.100.61
max connection = 2
timeout = 300
jook-debian1:~#
========================================


[jook] --> 서비스명
comment = jook's home directory --> 서비스에 대한 코멘트
path = /home/jook/public_html  --> 서비스 디렉토리(백업 디렉토리)
uid = root --> 접속 user 권한
gid = root --> 접속 group 권한
use chroot = yes  --> 서비스 디렉토리를 최상위 디렉토리로 사용
read only = yes --> 읽기 전용으로 설정
hosts allow = 10.30.100.61  --> 접속을 허용할 호스트(백업서버의 ip)
max connection = 2  --> 최대 접속 가능 수
timeout = 300  --> 클라이언트에서 접근시 타임아웃시간


2. master서버에서 rsync데몬 실행

/etc/default/rsync 파일에서 RSYNC_ENABLE=true 로 변경하고 rsync 데몬을 실행한다. 데몬이 실행되면, 873포트가 open된 것을 확인 할 수 있다.
redhat계열의 Linux에서는 xinetd 방식이나, standalone방식 등을 이용해서 rsync데몬을 실행하면 된다.

=====================================================================================================
jook-debian1:~# /etc/init.d/rsync start
Starting rsync daemon: rsync.
jook-debian1:~# netstat -natp | grep LISTEN
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      2797/rsync
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      2036/mysqld
tcp        0      0 0.0.0.0:48878           0.0.0.0:*               LISTEN      1631/rpc.statd
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1620/portmap
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1950/sshd
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      2118/cupsd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2458/exim4
tcp6       0      0 :::873                  :::*                    LISTEN      2797/rsync
tcp6       0      0 :::22                   :::*                    LISTEN      1950/sshd
tcp6       0      0 ::1:631                 :::*                    LISTEN      2118/cupsd
jook-debian1:~#
=====================================================================================================


3. backup서버에서 master서버의 rsync 서비스와 comment 확인하기

backup서버에서 아래와 같은 방법으로 master서버에서 허용된 서비스와 comment를 확인할 수 있다.

=======================================
jook-debian2:~# rsync 10.30.100.60::
jook            jook's home directory
=======================================


4. 백업 테스트

- master서버에서 파일 생성
=====================================================================================================
jook-debian1:~# cd /home/jook/public_html
jook-debian1:/home/jook/public_html# touch testfile1 testfile2 testfile3
jook-debian1:/home/jook/public_html# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile1
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile2
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile3
jook-debian1:/home/jook/public_html#
=====================================================================================================

- backup서버에서 rsync명령으로 업데이트(master서버에서 생성된 파일이 백업된 것을 확인할 수 있다.)
  아래는 master서버에서 허용된 서비스의 디렉토리와 backup서버의 /backup/jook 디렉토리와 동기화 시키는 작업이다.
=====================================================================================================
jook-debian2:~# rsync -avzr --delete 10.30.100.60::jook /backup/jook/
receiving incremental file list
./
testfile1
testfile2
testfile3

sent 86 bytes  received 200 bytes  52.00 bytes/sec
total size is 0  speedup is 0.00
jook-debian2:~# cd /backup/jook
jook-debian2:/backup/jook# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile1
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile2
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile3
jook-debian2:/backup/jook#
=====================================================================================================

- master서버에서 file 삭제
=================================================================
jook-debian1:/home/jook/public_html# rm -rf testfile2 testfile3
jook-debian1:/home/jook/public_html# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24 01:14 testfile1
jook-debian1:/home/jook/public_html#
=================================================================

- backup서버에서 rsync명령으로 업데이트(master서버에서 삭제한 파일이 같이 삭제 된것을 확인할 수 있다.)
=====================================================================================================
jook-debian2:/backup/jook# rsync -avzr --delete 10.30.100.60::jook /backup/jook/
receiving incremental file list
deleting testfile3
deleting testfile2
./

sent 29 bytes  received 82 bytes  20.18 bytes/sec
total size is 0  speedup is 0.00
jook-debian2:/backup/jook# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile1
jook-debian2:/backup/jook#
=====================================================================================================


5. cron에 등록하여 주기적으로 백업

아래와 같은 방법으로 backup서버의 cron에 등록을 하면 하루한번 주기적으로 데이터를 미러링 하게 된다.

00 02 * * * /usr/bin/rsync -avzr --delete 10.30.100.60::jook /backup/jook/

 

 

ssh를 이용하여 rsync사용하기

 

ssh를 이용하는 경우는 아래와 같이 -e ssh 옵션을 추가해서 사용하면 된다. 그리고, master서버와 backup서버 둘다 명령을 수행 할 수 있다.


1. master서버에서 rsync 명령으로 백업

jook-debian1:~# rsync -avzr --delete -e ssh /home/jook/public_html/ root@10.30.100.61:/backup/jook/
The authenticity of host '10.30.100.61 (10.30.100.61)' can't be established.
RSA key fingerprint is 4a:42:46:c9:55:48:63:08:a5:84:0a:69:d8:a6:47:0e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.30.100.61' (RSA) to the list of known hosts.
root@10.30.100.61's password:
sending incremental file list

sent 67 bytes  received 12 bytes  10.53 bytes/sec
total size is 0  speedup is 0.00
jook-debian1:~#


2. slave서버에서 rsync 명령으로 백업

jook-debian2:~# rsync -avzr --delete -e ssh root@10.30.100.60:/home/jook/public_html/ /backup/jook/
root@10.30.100.60's password:
receiving incremental file list

sent 11 bytes  received 68 bytes  8.32 bytes/sec
total size is 0  speedup is 0.00
jook-debian2:~#

 

번호 제목 글쓴이 날짜 조회 수
306 서버의 바이오스, 메인보드에서 지원하는 최대 메모리 확인 방법 호스트웨이 2012.03.23 19413
305 arp 명령 사용법 호스트웨이 2012.03.27 28281
304 top 명령어 및 옵션 설명 file 호스트웨이 2012.03.27 52527
303 Swap 메모리 늘리기 file 호스트웨이 2012.03.30 67199
302 현재 설치된 메모리 정보를 확인하는 방법 호스트웨이 2012.03.30 32448
301 하드디스크의 배드블럭을 검사하는 명령어 (badblocks) 호스트웨이 2012.03.30 21061
300 find 명령어-1 호스트웨이 2012.03.30 14935
299 Linux 서버 root password 분실시 변경 방법 file 호스트웨이 2012.03.30 23999
298 VI 편집기에서 사용되는 명령어 호스트웨이 2012.03.30 13903
297 Linux Charset 확인 및 변경 file 호스트웨이 2012.03.30 84962
296 VI 편집기에서 사용되는 명령어(2) 호스트웨이 2012.04.06 13737
295 Linux lastlog를 이용한 최근 접속정보 확인 file 호스트웨이 2012.04.06 19742
294 계정 생성 시 ftp 계정만 주고 Shell 로그인 차단하기 file 호스트웨이 2012.04.06 23704
293 ipv6 비활성화 하기 호스트웨이 2012.04.06 25457
292 서버 아이피 변경으로 인한 다량의 네임서버 존파일 일괄 변경 방법 호스트웨이 2012.04.06 14683
291 vsftpd.conf 옵션 설명 호스트웨이 2012.04.06 16816
290 chattr 명령을 이용한 파일 속성 변경 file 호스트웨이 2012.04.07 18558
289 서비스 이름으로 포트 번호 확인하기 호스트웨이 2012.04.13 57713
288 국가명 약어 (국가코드) 호스트웨이 2012.04.13 64960
287 ngrep 사용법 호스트웨이 2012.04.13 16808