实现目标:

 

 A 服务器上 /opt/web 目录,与B服务器上 /opt/web目录实现同步。即:B主动与A进行同步。

 

OS: Centos6.5

 

A Server  192.168.189.152 /opt/app

B Server  192.168.189.153 /opt/app



使用yum 安装xinetd服务 (两个节点都要安装)

yum install -y xinetd


一. A Server config

 

1.rsync 系统自带, 需要使用 --deamon 方式进行启动,服务端口是 TCP 873

 

2. vi /etc/xinetd.d/rsync  修改 disable =yes 为 disable =no ,修改后的文件如下

 

# default: off

# description: The rsync server is a good addition to an ftp server, as it \

#       allows crc checksumming etc.

service rsync

{

        disable = no

        socket_type     = stream

        wait            = no

        user            = root

        server          = /usr/bin/rsync

        server_args     = --daemon

        log_on_failure  += USERID

}

 

3. service   xinetd   restart,重启 xinted 服务

 

4. 编辑主要配置文件 /etc/rsyncd.conf  这个文件需要自己创建

 

vi /etc/rsyncd.conf

 

uid = nobody

gid = nobody

use chroot = no

max connections = 4

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

 

[app]

path = /opt/app

ignore errors

read only = true

list = false

hosts allow = 192.168.189.0  # 允许的IP地址

hosts deny = 0.0.0.0/32    # 禁止的IP 地址

auth users = backup        # 认证用户名,此例是 backup

secrets file = /etc/backup.pass #认证用户的密码文件

 

5 编辑服务器的密码文件 /etc/backup.pass

vi /etc/backup.pass

backup:123456

 

6. chmod 400 /etc/backup.pass

 

二. B Server  config 这个是被同步端机器

 

1. 编辑rsync连接时的密码文件 /etc/rsync_client.pass

vi /etc/rsync_client.pass

123456                    # 只需要配置连接时使用的密码即可,必须与A服务器上定义的密码相同.

 

2. chmod 600 /etc/rsync_client.pass

 

3. 使用 rsync 命令连接服务器,实现文件同步

 # rsync -vzrtopg --progress --delete --password-file=/etc/rsync_client.pass backup@192.168.189.152::app /opt/app

4. 使用 --execlude= 排除不需要同步的文件后缀名

rsync -vzrtopg --progress --delete --password-file=/etc/rsync_client.pass --exclude="*.tmp" backup@192.168.189.152::app /opt/app

 

5. 使用 --execlude-from= 排除不需要同步的目录

[root@ASB opt]# rsync -vzrtopg --progress --delete --password-file=/etc/rsync_client.pass --exclude-from=/opt/pcdir backup@192.168.189.152::app /opt/app