2016년 11월 2일 수요일

[Linux] rsync 동기화 사용방법

OS : centos 6.6

rsync 사용을 위하여 설치여부 검사

서버쪽 검사 사항
[root@server ~]# rpm -qa | grep xinetd
xinetd-2.3.14-39.el6_4.x86_64
[root@server ~]# rpm -qa | grep rsync
rsync-3.0.6-12.el6.x86_64

만일 둘 중 하나라도 설치가 되어 있지 않다면 yum 을 통하여 설치합니다.

[root@server ~]# yum install -y xinetd
[root@server ~]# yum install -y rsync

클라이언트쪽 검사 사항
[root@client ~]# rpm -qa | grep rsync
rsync-3.0.6-12.el6.x86_64

클라이언트는  rsync만 설치되어 있으면 됩니다.

이제부터 환경설정에 들어가도록 하겠습니다.
환경설정은 서버 -> 클라이언트 순으로 진행됩니다.

* 서버 쪽 환경설정
[root@server ~]# vi /etc/xinetd.d/rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = yes           # 좌측의 disable를 no로 변경해줍니다.
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

----------------------------------------------------------------------------------------------------------------------
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
:wq

이제 rsync.conf를 수정 할 예정입니다.
만일 rsync.conf가 없다면 당황하지 말고 만들어주시면 됩니다.

[root@server ~]# vi /etc/rsync.conf
[www]                                    <- 연결될 예명
comment=www                        <- 코멘트(사용자가 알아볼 코멘트 입니다)
path=/home/user/www            <- 동기화 할 폴더
host allow=192.168.0.20         <- 접근 허용 할 IP
uid=user                                <- 접근시의 사용자ID
gid=group                              <- 접근시의 사용자 그룹명
use chroot=yes                      <- 해당 폴더 Root 여부
read only=yes                        <- 읽기전용 여부
max connections=1                <- 최대 커넥션 수
timeout 600                           <- timeout 시간

요기까지 하셨으면 xinetd를 재시작 해줍니다.

[root@server ~]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

서버 설정은 여기서 끝납니다. 쉽죠?

* 클라이언트 쪽 실행
클라이언트 부분은 환경설정은 별도로 없습니다만..
바로 실행을 하면 됩니다. 단.. 단순 실행 시 접속 계정의 비밀번호를 물어보게 됩니다.
상당히 불편하죠..!!
실행은 아래와 같습니다.
[root@client ~]# rsync -avz 192.168.0.10::www /home/user

만일 오류가 난다면 아래와 같이 직접 명시를 해주시면 됩니다.

[root@client ~]# rsync -avz 192.168.0.10:/home/user/www /home/user


이걸 좀 더 편리하게 이용하기 위해 pwd를 패스 하도록 하겠습니다. 클라이언트에서 서버로 key를 보내야 합니다.
ssh-keygen 시 여러가지를 물어보는데 마구 엔터를 눌러주시면 됩니다.
아 실행하기 전 서버 root path에 .ssh 폴더가 생성되어 있어야만 합니다.

[root@client ~]# ssh-keygen -t dsa
[root@client ~]# cat ~/.ssh/id_dsa.pub | ssh root@192.168.0.10 "cat >> .ssh/authorized_keys"

이제 서버로 ssh 접속을 하시면 비밀번호 묻는것 없이 접속이 가능합니다.

[root@client ~]# ssh 192.168.0.10
Last login: Tue Sep  8 16:35:55 2015 from 192.168.0.20
[root@server ~]# 

요기까지 돼셨다면

제 개인 스크립트를 통해 log와 실시간(매 1분당) 동기화 작업을 하겠습니다.


[root@client ~]# mkdir /home/rsynclog
[root@client ~]# vi rsync.sh
#!/bin/bash

date=`date +%Y-%m-%d`
date_delete=`date --date "15 day ago" +%Y-%m-%d`

backup_dir="/home/rsynclog/"

rm -Rf ${backup_dir}${date_delete}
mkdir ${backup_dir}${date}

/usr/bin/rsync -avz --progress --delete --log-file=${backup_dir}${date}/rsync.log 192.168.0.10:/home/user/www /home/user/
:wq

[root@client ~]# chmod +x rsync.sh
[root@client ~]# crontab -e
*/1 * * * * su - root -c '/root/rsync.sh'
:wq

요기까지 설정하셨으면 매 1분마다 /home/rsynclog 폴더에 날짜별로 rsync.log 파일이 쌓일겁니다 :-)

rsync option
-a : 심볼릭 링크, 속성, 퍼미션, 소유권 등 보존
-v : 자세한 정보출력
-z : 전송시 압축
-r : 하위디렉토리포함
-e ssh : ssh를 이용한 rsync 동기화
--delete : 서버동기화후 원본에서 파일이 삭제되면 백업에서도 파일을 삭제
--stats : 결과출력
--log-file : 로그파일 저장

댓글 없음:

댓글 쓰기