rsync 实验

参考1:http://www.jb51.net/LINUXjishu/142722.html

参考2:http://sookk8.blog.51cto.com/455855/328076

主服务器IP 192.168.109.128

从服务器IP 192.168.109.132

1.查看 rsync xinetd是否安装

  rpm -q rsync

2.关闭 iptables 和 SELinux

从服务器IP 192.168.109.132 上的配置开始

1.yum install rsync xinetd #安装  如果已经安装可略过

2.vi /etc/xinetd.d/rsync #编辑配置文件

  disable = no #修改为no

3./etc/init.d/xinetd start #启动

4.创建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启动时欢迎信息页面文件位置(文件内容自定义)
[test_rsync] #自定义名称
path = /test/ #rsync服务端数据目录路径
comment = test_rsync #模块名称与[home_www.jb51.net]自定义名称相同
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 = root #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 192.168.109.128 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 192.168.21.254 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

5、创建用户认证文件

  vi /etc/rsync.pass #配置文件,添加以下内容  

root:123456 #格式,用户名:密码,可以设置多个,每行一个用户名:密码

 6、设置文件权限

chmod 600 /etc/rsyncd.conf #设置文件所有者读取、写入权限 
chmod 600 /etc/rsync.pass #设置文件所有者读取、写入权限

 7、让配置生效
   service xinetd restart

8.检查rsync
#netstat -a | grep rsync
   tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN  

9.设置开机启动项

  #vi /etc/rc.d/rc.local  加入
    /usr/bin/rsync --daemon     //不知道干嘛用的

    service xinetd start

第二部分:在主服务器192.168.109.128上操作

1.查看和安装 

whereis rsync #查看系统是否已安装rsync,出现下面的提示,说明已经安装
rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz
yum install xinetd #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的
yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理rsync服务的)

2、创建认证密码文件

  vi /etc/rsync.pass  #编辑文件,添加以下内容

123456 #密码

 3.修改权限

 chmod 600 /etc/passwd.txt  #设置文件权限,只设置文件所有者具有读取、写入权限即可

 4.测试是否成功  

rsync -arHz --port=873 --progress --delete /test/ root@192.168.109.132::text_rsync --password-file=/etc/rsync.pass

选项解释:

  -a  保持文件属性

  -r 子目录递归处理

  -H  保持硬链接关系

  -z  传输时压缩处理

  --progress 显示传输过程

  --delete  删除目标备份没有的文件

  -e ssh   使用ssh加密隧道传输

  --password-file  使用本地密码和目标密码匹配  跳过手动输入密码的过程

 二.sersync的安装

查看linux机器是32位还是64位的方法

1.查看linux机器是32位还是64位的方法:

  file /sbin/init 或者 file /bin/ls
  /sbin/init: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, stripped

  如果显示 64-bit 则为64位;

  file /sbin/init
  /sbin/init: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped
  如果显示为32 bit 则为32bit;

2.32位系统TAR包 http://pan.baidu.com/s/1ntyn2XF

  64位系统 http://pan.baidu.com/s/1qWNPMpu

3.ls -l /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查看内核

CentOS 5.X 内核为2.6.18,默认已经支持inotify

 4.修改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"

 参数说明:

max_queued_events:

inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确

max_user_watches:

要同步的文件包含多少目录,可以用:find /home/www.jb51.net -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.jb51.net为同步文件目录)

max_user_instances:

每个用户创建inotify实例最大值

5.安装sersync

  上传安装包到服务器 解压

    tar -zxvf ......

    mv GNU-Linux-x86 /usr/local/sersync #移动目录到/usr/local/sersync

6.配置sersync

  cd /usr/local/sersync #进入sersync安装目录
  cp confxml.xml confxml.xml-bak #备份原文件

  vi confxml.xml  编辑,修改下面的代码

  

<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="192.168.109.128" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*).svn"></exclude>
<exclude expression="(.*).gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/test">
<remote ip="192.168.109.132" name="test_rsync"/>
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsync.pass"/>
<userDefinedPort start="false" port="873"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="true" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*).php"/>
<include expression="(.*).sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="<a href="http://pic.xoyo.com/cms"/">http://pic.xoyo.com/cms"/</a>>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>

7.设置sersync监控开机自动执行

  vi /etc/rc.d/rc.local  #编辑,在最后添加一行

    /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行脚本

8.添加脚本监控sersync是否正常运行

  vi  /root/check_sersync.sh 编辑,添加以下代码

#!/bin/sh
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi

   :wq! #保存退出
  chmod +x /root/check_sersync.sh #添加脚本执行权限

  vi /etc/crontab #编辑,在最后添加下面一行
  */5 * * * * root  /root/check_sersync.sh > /dev/null 2>&1 #每隔5分钟执行一次脚本
  service crond reload #重新加载服务

 9.启动服务查看效果

  /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml

  mkdir /test/rsync_success

  去从服务器查看效果

原文地址:https://www.cnblogs.com/mr-amazing/p/3746292.html