Linux ETC

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

2009.04.24 15:42

jook 조회 수:27279

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:~#

 

번호 제목 글쓴이 날짜 조회 수
386 scp 명령어를 이용한 파일 복사 및 전송 호스트웨이 2012.03.16 269702
385 ls 명령어의 파일 사이즈 쉽게 확인하기 호스트웨이 2012.10.23 204893
384 sar 명령 옵션 호스트웨이 2012.06.14 100271
383 리눅스 서버 누가 언제 무슨 작업을 했는지 확인 방법 호스트웨이 2012.03.08 98178
382 Linux Charset 확인 및 변경 file 호스트웨이 2012.03.30 90006
381 ulimit 설정 관련 호스트웨이 2015.07.29 80600
380 서비스 이름으로 포트 번호 확인하기 호스트웨이 2012.04.13 73692
379 국가명 약어 (국가코드) 호스트웨이 2012.04.13 71358
378 Swap 메모리 늘리기 file 호스트웨이 2012.03.29 69927
377 head 와 tail 사용하기 sylee 2008.05.14 66312
376 fdisk 명령어를 이용하여 수동으로 파티션 나누기 file 호스트웨이 2015.11.30 65390
375 비대해진 로그 파일 내용을 비우는 방법 - /dev/null 호스트웨이 2012.03.16 64206
374 강제 umount 방법 (umount : device is busy 발생시) 호스트웨이 2012.11.29 62196
373 top 명령어 및 옵션 설명 file 호스트웨이 2012.03.26 55880
372 history 조회시 '날짜,시간' 표시하기 호스트웨이 2012.03.16 55759
371 /bin/false, /sbin/nologin 의 차이점 호스트웨이 2012.08.09 54337
370 xinetd 소개 및 설정 호스트웨이 2012.06.29 54323
369 Kernel Parameters HOSTWAY 2008.06.03 52752
368 시스템 시간 확인 및 동기화 하기 sylee 2008.05.14 49115
367 ssh-key를 생성하여 서버에 패스워드 없이 접속하는 방법 jook 2008.06.24 47578