文件同步rsync

通过使用rsync作为文件同步服务器,通过sersync + inotify-tools监控文件变化,调用rsync实现增量同步至rsync远程文件服务器

1. 环境:

  服务器:centos 7: 192.168.41.49

      客户端:centos 7:192.168.3.88

2. 安装部署:

  • 安装:yum install rsync -y
  • 配置:
  • rsyncd 配置文件:

      vim /etc/rsyncd.conf

    • uid = rsync
      gid = rsync
      fake super = yes
      use chroot = no
      ignore errors = yes
      
      max connections = 200
      timeout = 600
      pid file = /var/run/rsyncd.pid
      log format = %o %h [%a] %m (%u) %f %l
      lock file = /var/run/rsync.lock
      log file = /var/log/rsyncd.log
      read only = false
      list = false
      
      [app_backup]
      comment = welcome to app scan backup!
      path = /opt/xScan/mercury/data/app_wait_parse/
      hosts allow = 192.168.3.0/24
      auth users = rsync_app_backup
      secrets file = /etc/rsync.passwd

      - [app_backup] : 要备份的模块名,[...]

      - comment: 注释

      - path:同步的文件目录,注意设置目录权限为uid可读写

      - hosts allow: 允许同步到此目录的主机ip地址

      - auth users: 同步时使用的账号

      - secrets file:通过使用的密码所在文件,密码文件权限为600

      - 配置文件格式与ini类型,外城为全局变量,模块内为模块局部变量   

  • 配置模块登陆用户的密码文件:/etc/rsync.passwd
echo "rsync_app_backup:app_backup" > /etc/rsync.passwd
# 有用户+”:”+密码构成
chmod 600 /etc/rsync.passwd
  • 添加非登陆用户:
useradd -s /sbin/nologin  -M rsync
id rsync

chown -R rsync.rsync /opt/xScan/mercury/data/app_wait_parse/
  • 部署启动:
# systemctl 启动
systemctl restart rsyncd
# 设置开机启动
systemctl enable rsyncd

# daemon 启动
rsync --daemon --config=/etc/rsyncd.conf

3. sersync 安装部署

  • 安装
[root@test02 ~]# cd /opt/
[root@test02 opt]# wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@test02 opt]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@test02 opt]# mv GNU-Linux-x86/ /usr/local/sersync/
  • 依赖:inotify,centos7 默认支持,确认:
[root@test02 ~]# ll /proc/sys/fs/inotify
总用量 0
-rw-r--r--. 1 root root 0 12月 28 19:35 max_queued_events
-rw-r--r--. 1 root root 0 12月 28 17:16 max_user_instances
-rw-r--r--. 1 root root 0 12月 28 19:35 max_user_watches
[root@test02 ~]# 
    • 调整inotify:应用于监控的目录中包含文件和目录数量较多的情况
      • 查看默认参数:
        • sysctl -a | grep max_queued_events
        • sysctl -a | grep max_user_instances
        • sysctl -a | grep max_user_watches
      • 修改:
        • sysctl -w fs.inotify.max_queued_events="99999999"
        • sysctl -w fs.inotify.max_user_watches="99999999"
        • sysctl -w fs.inotify.max_user_instances="65535"
      • 保存生效:
        • sysctl -p
  • 配置
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
    <host hostip="localhost" 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="/opt/xScan/mercury/data/app_wait_parse">
            <remote ip="192.168.41.49" name="app_backup"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-artuz"/>
            <auth start="true" users="rsync_app_backup" passwordfile="/etc/rsync_app_backup.passwd"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/opt/xScan/mercury/data/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
                <exclude expression="*.php"></exclude>
                <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
    </sersync>
  • 配置密码文件(注意客户端与服务器的密码文件不同):
[root@test03 sersync]# echo 'app_backup' > /etc/rsync_app_backup.passwd
[root@test03 sersync]# chmod 600 /etc/rsync_app_backup.passwd
  • 部署启动:
[root@test03 sersync]# nohup /usr/local/sersync/sersync2 -r -d -o /usr/local/sersync/confxml.xml > /var/log/sersync.log 2>&1 &
-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n:指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件
原文地址:https://www.cnblogs.com/spaceapp/p/14210583.html