#先在节点服务器上安装安装Rsync服务端
#编辑防火墙配置文件
vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
#保存编辑,立即生效
setenforce 0
#开启防火墙tcp 873端口(Rsync默认端口)
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
#重启服务
/etc/init.d/iptables restart
#安装Rsync服务端软件
yum -y install rsync xinetd
#编辑配置文件,设置开机启动rsync
vi /etc/xinetd.d/rsync
disable = no #修改为no
#启动(CentOS中是以xinetd来管理Rsync服务的)
/etc/init.d/xinetd start
创建rsyncd.conf配置文件
vi /etc/rsyncd.conf #创建配置文件,添加以下代码,需要清除中文注释
#############################################################################
log file = /var/log/rsyncd.log #日志文件位置,启动rsync后自动产生这个文件,无需提前创建
pidfile = /var/run/rsyncd.pid #pid文件的存放位置
lock file = /var/run/rsync.lock #支持max connections参数的锁文件
secrets file = /etc/rsync.pass #用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件
motd file = /etc/rsyncd.Motd #rsync启动时欢迎信息页面文件位置(文件内容自定义)
[downfiles] #自定义名称
path = /www/web/yousite.com/ #rsync服务端数据目录路径
comment = downfiles #模块名称与[downfiles]自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port=873 #默认端口
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
read only = no #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
max connections = 200 #最大连接数
timeout = 600 #设置超时时间
auth users = downfile_user #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.6.133,192.168.6.129 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 127.0.0.1 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
#############################################################################
#创建用户认证文件
vi /etc/rsync.pass #配置文件,添加以下内容
downfile_user:123456 #格式,用户名:密码,可以设置多个,每行一个用户名:密码
#设置文件权限
chmod 600 /etc/rsyncd.conf
chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限
#启动rsync
/etc/init.d/xinetd start #启动
service xinetd stop #停止
service xinetd restart #重新启动
*********************************************************************************************
第二部分:在源服务器上操作
安装Rsync客户端
#关闭SELINUX
vi /etc/selinux/config #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
#保存编辑,立即生效
setenforce 0
#开启防火墙tcp 873端口(Rsync默认端口)
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
#重启服务
/etc/init.d/iptables restart
#安装Rsync客户端端软件
whereis rsync #查看系统是否已安装rsync,出现下面的提示,说明已经安装
rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz
yum -y install xinetd #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的
yum -y install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
#启动(CentOS中是以xinetd来管理rsync服务的)
/etc/init.d/xinetd start
创建认证密码文件
vi /etc/passwd.txt #编辑文件,添加以下内容
123456 #密码
#设置文件权限,只设置文件所有者具有读取、写入权限即可
chmod 600 /etc/passwd.txt
rsync -avH --port=873 --progress --delete /www/web/yousite.com/ downfile_user@192.168.6.129::downfiles --password-file=/etc/passwd.txt
安装Inotify-tools工具,实时触发rsync进行同步
#查看服务器内核是否支持inotify
ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar 7 02:17 max_user_watches
备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
#下载安装inotify-tools
wget http://www.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar -zxvf inotify-tools-3.14.tar.gz && cd inotify-tools-3.14 && ./configure --prefix=/usr/local/inotify && make && make install
#设置系统环境变量,添加软连接
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
#使设置立即生效
source /etc/profile.d/inotify.sh
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include /usr/include/inotify
#修改inotify默认参数(inotify默认内核参数值太小)
#查看系统默认参数值
sysctl -a | grep max_queued_events
结果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
结果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
结果是:fs.inotify.max_user_instances = 128
#修改参数:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
#添加以下代码
vi /etc/sysctl.conf
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
参数说明:
max_queued_events:
inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确
max_user_watches:
要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)
max_user_instances:
每个用户创建inotify实例最大值
#创建脚本,实时触发rsync进行同步
#编辑,添加以下代码
vi /usr/local/inotify/rsync.sh
#!/bin/bash
srcdir=/www/web/yousite.com/ #源服务器目录地址
dstdir=downfiles #模块名称
excludedir=/www/web/yousite.com/exclude.list #不需要同步的目录
rsyncuser=downfile_user #执行数据同步的用户名
rsyncpassdir=/etc/passwd.txt #密码文件位置
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move /www/web/yousite.com/ | while read file
do
rsync -avH --port=873 --progress --delete --exclude-from=$excludedir $srcdir $rsyncuser@192.168.6.129::$dstdir --password-file=$rsyncpassdir
echo "${files} was rsynced" >> /var/log/rsync.log 2>&1
done
######################################################################
#脚本参数说明:
srcdir=/www/web/yousite.com/ #源服务器同步目录
dstdir=downfiles #目标服务器rsync同步目录模块名称
excludedir=/www/web/yousite.com/exclude.list
#不需要同步的目录,如果有多个,每一行写一个目录,使用相对于同步模块的路径;
#例如:不需要同步/home/www.osyunwei.com/目录下的a目录和b目录下面的b1目录,exclude.list文件可以这样写
a/
b/b1/
rsyncuser=downfile_user #目标服务器rsync同步用户名
rsyncpassdir=/etc/passwd.txt #目标服务器rsync同步用户的密码在源服务器的存放路径
/var/log/rsync.log #脚本运行日志记录
######################################################################
#添加脚本执行权限
chmod +x /usr/local/inotify/rsync.sh
#设置脚本开机自动执行
vi /etc/rc.d/rc.local #编辑,在最后添加一行
#设置开机自动在后台运行脚本
sh /usr/local/inotify/rsync.sh &
#杀死后台运行的进程
ps -aux |grep 'rsync.sh'
kill -9 pid
#或者
ps -elf| pgrep inotify |xargs kill -9
#执行脚本,在后台执行
sh /usr/local/inotify/rsync.sh &
#编写监听inotify进程脚本,防止inotify由于某种原因中断无法实时同步(如果不需要监听,此步可以跳过)
vi /usr/local/inotify/inotifyjianting.sh
#!/bin/bash
ps -lef |pgrep inotify &> /dev/null #如果有inotify进程在运行,那么echo $? 返回值是0,条件为真。否则返回值为非0
if [ -z $? ] ;then #关键在$?这个变量 ,它是代表上一条命令执行后的退出状态,如果是0的话表示成功,非0表示未开启
echo "inotify runing 正在运行...."
else
sh /usr/local/inotify/rsync.sh &
echo "inotify runing 已运行....."
fi
#修改运行权限
chmod +x /usr/local/inotify/inotifyjianting.sh
#添加定时任务计划
crontab -e
* * * * * /usr/local/inotify/inotifyjianting.sh &> /dev/null
评论已有( 0 )条评论