一、环境准备(一定要配置主机名和hosts记录)

IP地址:
192.168.1.103
192.168.1.221

操作系统:
centos 6.5 64位

需求:
将103的/home/backup/同步到221的/home/backup/。



二、rsync部署(221)

1、关闭SELINUX
vi /etc/selinux/config
用 # 注释掉下面两行代码:  
#SELINUX=enforcing
#SELINUXTYPE=targeted
增加下面一行代码:
SELINUX=disabled
运行此命令立即生效。
setenforce 0


2、开启防火墙tcp 873端口、或关掉防火墙(rsync默认端口)
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
或
service iptables stop
chkconfig iptables off


3、安装rsync服务端软件
yum install rsync xinetd
vi /etc/xinetd.d/rsync
修改disable的值为no:
disable = no
启动xinetd(CentOS中是以xinetd来管理Rsync服务的)
/etc/init.d/xinetd start


4、创建rsyncd.conf配置文件(假如需要同步多个目录,注意加多个目录)
创建配置文件:
vi /etc/rsyncd.conf
添加以下代码
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsyncd.secret
motd file = /etc/rsyncd.motd
[syncdata]
path = /home/backup/
comment = syncdata
uid = gxm
gid = gxm
incoming chmod = Du=rwx,Dog=rx,Fu=rwx,Fgo=rx
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = root
hosts allow = 192.168.1.103
hosts deny = *


5.创建用户认证文件
配置文件
vi /etc/rsyncd.secret
root:123456
vi /etc/rsyncd.passwd
123456
保存退出


6.设置文件权限
设置文件所有者读取、写入权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.secret


7、操作系统建立gxm用户和组


8.启动rsync,并设置开机自启动
/etc/init.d/rsync start 
/etc/init.d/xinetd start
chkconfig rsync on
chkconfig xinetd on



三、rsync部署(103)


8.创建用户认证文件
配置文件
vi /etc/rsyncd.secret
root:123456
vi /etc/rsyncd.passwd
123456
保存退出


9.设置文件权限
设置文件所有者读取、写入权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsyncd.passwd
chmod 600 /etc/rsyncd.secret


10、安装rsync同步工具
yum install rsync


11、执行同步命令
rsync -azv --delete /home/backup/ root@192.168.1.221::syncdata --password-file=/etc/rsyncd.passwd


12、将上面的同步命令写成脚本,然后定时运行。
脚本:
vi rsyncdata.sh
#!/bin/bash
rsync -azv --delete /home/backup/ root@192.168.1.221::syncdata --password-file=/etc/rsyncd.passwd
定时任务:
* 22 * * * sh /root/rsyncdata.sh