rsync를 사용하여 원격으로 데이터 백업 하기

rsync를 사용하여 원격으로 데이터 백업 하기

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

테스트 환경 
master서버 : 10.30.100.60
backup서버 : 10.30.100.61


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


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

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

[add4s] --> 서비스명
comment = add4s home directory --> 서비스에 대한 코멘트
path = /home/add4s/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  --> 클라이언트에서 접근시 타임아웃시간


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

/etc/xinetd.d/rsync 파일에서 disable = no 로 변경하고 xinetd 데몬을 실행한다. 데몬이 실행되면, 873포트가 open된 것을 확인 할 수 있다.
ubunt계열의 Linux에서는 /etc/default/rsync 파일에서 RSYNC_ENABLE=true 로 변경하고 rsync 데몬을 실행한다.
==============================================================================
[root@master ~]# /etc/init.d/xinetd start
Starting xinetd: [  OK  ]
[root@master ~]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name   
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      1242/sshd           
tcp        0      0 :::873                      :::*                        LISTEN      7022/xinetd         
tcp        0      0 :::22                       :::*                        LISTEN      1242/sshd        
[root@master ~]#
==============================================================================


1.3. iptables 873 포트 접속 허용

iptables 방화벽에 873 포트 접속이 가능하도록 포트를 허용한다. 
================================================================================
[root@master ~]# iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination         
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED 
ACCEPT     icmp --  anywhere             anywhere            
ACCEPT     all  --  anywhere             anywhere            
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:https 
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:rsync 
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination         
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited 

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination         
[root@master ~]#
==============================================================================


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

backup서버에서 아래와 같은 방법으로 master서버에서 허용된 서비스와 comment를 확인할 수 있다.
==============================================================================
[root@backup ~]# rsync 10.30.100.60::
add4s            add4s home directory
[root@backup ~]#
==============================================================================


1.5. 백업 테스트

- master서버에서 파일 생성
==============================================================================
[root@master ~]# cd /home/add4s/public_html
[root@master public_html]# touch testfile1 testfile2 testfile3
[root@master 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
[root@master public_html]#
==============================================================================

- backup서버에서 rsync명령으로 업데이트(master서버에서 생성된 파일이 백업된 것을 확인할 수 있다.)
  아래는 master서버에서 허용된 서비스의 디렉토리와 backup서버의 /backup/add4s 디렉토리를 동기화 시키는 작업이다.
==============================================================================
[root@backup ~]# rsync -avzr --delete 10.30.100.60::add4s /backup/add4s/
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
[root@backup ~]# cd /backup/add4s
[root@backup add4s]# 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
[root@backup add4s]#
==============================================================================

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

- backup서버에서 rsync명령으로 업데이트(master서버에서 삭제한 파일이 같이 삭제 된것을 확인할 수 있다.)
==============================================================================
[root@backup add4s]# rsync -avzr --delete 10.30.100.60::add4s /backup/add4s/
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
[root@backup add4s]# ls -l
total 0
-rw-r--r-- 1 root root 0 Apr 24  2009 testfile1
[root@backup add4s]#
==============================================================================


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

아래와 같은 방법으로 backup서버의 cron에 등록을 하면 원하는 시간에 주기적으로 데이터를 미러링 하게 된다.
==============================================================================
[root@backup add4s]# crontab -l
00 02 * * * /usr/bin/rsync -avzr --delete 10.30.100.60::add4s /backup/add4s/
==============================================================================


2. ssh를 이용하여 rsync사용하기

ssh를 이용하는 경우는 아래와 같이 -e ssh 옵션(생략가능)을 추가해서 rsync 명령을 사용하면 된다. 그리고, master서버와 backup서버 둘다 명령을 수행 할 수 있다.
마찬가지로 아래의 명령을 스크립트로 만들어서 사용하거나, cron에 등록해서 주기적인 데이터 미러링 작업이 가능하다.


2.1. master서버에서 rsync 명령으로 백업
==============================================================================
[root@master ~]# rsync -avzr --delete -e ssh /home/add4s/public_html/ [email protected]:/backup/add4s/
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.
[email protected]'s password:
sending incremental file list

sent 67 bytes  received 12 bytes  10.53 bytes/sec
total size is 0  speedup is 0.00
[root@master ~]#
==============================================================================


2.2. slave서버에서 rsync 명령으로 백업
==============================================================================
[root@backup ~]# rsync -avzr --delete -e ssh [email protected]:/home/add4s/public_html/ /backup/add4s/
[email protected]'s password:
receiving incremental file list

sent 11 bytes  received 68 bytes  8.32 bytes/sec
total size is 0  speedup is 0.00
[root@backup ~]#
==============================================================================

2개의 댓글이 있습니다.

약 6년 전

감사합니다. 잘 활용해보도록 하겠습니다^^

Reply

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입

약 6년 전

나중에 기회되면 한번 해보겠습니다~~^^

Reply

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입

댓글 남기기

댓글을 남기기 위해서는 로그인이 필요합니다.

로그인 회원가입