Pages

2/04/2014

Rsync(synchronizes files and directories)

서버간의 데이터 동기화나 백업을 해야할 경우는 종종 생긴다.
rsync(Remote Sync)를 이용하면 간단한 설정으로 쉽게 동기화 시킬 수 있다.

rsync는 xinetd를 이용하기 때문에 rsync와 xinetd가 설치되어있어야한다.

yum install rsync
yum install xinetd
rsync는 ssh를 사용할수 있고 873포트를 이용 할 수있다.
873포트가 열려 있지 않다면 열어주자.
$telnet localhost 873
Trying ::1...
Connected to localhost.
Escape character is '^]'.
@RSYNCD: 30.0

#didn't open port 873
$vi /etc/sysconfig/iptables
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

/etc/init.d/iptables restart
rysync를 사용할 수 있게 활성화 시킨다.
$vi /etc/xinetd.d/rsync

#disable = yes -> disable = no
service rsync
{
        disable = no
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
rsync모듈을 등록하자.
$vi /etc/rsyncd.conf

#모듈 명
[testr]
#백업 경로
path = /mnt/testdir
#코멘트
comment = testr
#user
uid = root
gid = root
#위 path를 루트로 사용
use chroot = yes
#일기 전용
read only = yes
#허용 ip
hosts allow = 10.24.132.164
#허용커넥션갯수
max connections = 3
#타임아웃
timeout = 600


#restart xinetd
$service xinetd restart
이제 백업서버에서 rsync를 이용해 file을 가져오자.
rsync (option) user@127.0.0.1::module /path
$rsync -avz root@12.34.56.78::testr  /data/backup

#use ssh
$rsync -avz -e ssh root@12.34.56.78:/mnt/testdir  /data/backup
끝~

rsync option들은~ 이러하다.
-a : archice mode ( 심볼릭 링크, 속성, 퍼미션, 소유권 등 보곤)
-v : verbose mode (자세한 정보 출력)
-z : compress (전송시 압축)
-r : 하위 디렉토리 포함
-u : --update update only (don't overwrite newer files)
-e ssh : ssh를 이용한 rsync 동기화
--stats : 결과보고
--delete : 원본 서버에 없는 파일은 백업 서버에서 삭제
--progress : rsync 진행 상항 보기
--exclude : 제외할 파일 지정
--bwlimit : 대역폭(복사속도) 제어
--max-size : 특정 크기 이상 파일 제외
--min-size : 특정 크기 이하 파일 제외

2/03/2014

Enable ssh login without password(공개키 인증)

linux를 사용하다보면면 공개키인증으로 패스워드없이 로그인해야될때가 있다.(혹은 그냥하고싶을때도 있다..콜록)
그러나 우리는 망각의 동물 항상 까먹는다....
정리해 놓자...^^

centos에서 start@12.12.12.12에서 destination@34.34.34.34로 인증없이 접속한다고 가정하겠다.
공개키 인증은 사설키를 가지고 배포한 공개키로 조합하여 인증을 한다.
공개키와 사설키를 만들자.

$ssh start@12.12.12.12
start@12.12.12.12's password:
Last login: Mon Feb  3 09:47:08 2014 from 111.223.54.49
#if ~/.ssh doesn't exist
#you should make '.ssh' directory.(mkdir ~/.ssh)
$cd ~/.ssh/
$ssh-keygen -t rsa

#if you want a defualt value, push enter!!
Generating public/private rsa key pair.
Enter file in which to save the key (/home/start/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/start/.ssh/id_rsa.
Your public key has been saved in /home/start/.ssh/id_rsa.pub.
The key fingerprint is:
60:20:69:d4:64:8a:29:3a:1b:3d:d8:1d:0e:7b:59:7e silapps@e297d92a-7cf3-40be-8085-f344091a98eb
The key's randomart image is:
+--[ RSA 2048]----+
| .o++            |
| oo+..           |
|+.o . +          |
|o+ = * .         |
|= = = . E        |
| + o   .         |
|.                |
|                 |
|                 |
+-----------------+
공개키와 사설키가 생성되었다. 이제 공개키를 destination에 배포하자.

destination서버에도 .shh 디렉토리가 없다면 생성해주자.
$ssh destination@34.34.34.34 mkdir -p .ssh
$cat ~/.ssh/id_rsa.pub | ssh destination@34.34.34.34 'cat >> .ssh/authorized_keys'
destination에 공개키인증으로 접속 할 수 있도록 설정해주자.
$ssh destination@34.34.34.34
$sudo vi /etc/ssh/sshd_config
# 주석을 풀어준다.
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile     .ssh/authorized_keys

$sudo service sshd restart
#권한 설정
$chmod 700 ~/.ssh
$chmod 600 ~/.ssh/authorized_keys
끄읕~ 테스트해보자.
$ssh start@12.12.12.12
$ssh destination@34.34.34.34
Last login: Mon Feb  3 16:46:54 2014 from 12.12.12.12
$
성공~