ubuntu 14.04 配置 rsync + inotify 文件双向实时同步

一、安装配置

1. 安装rsync,inotify-tools

sudo apt-get install rsync inotify-tools 

2. 拷贝rsync配置文件

mkdir /etc/rsync
cp /usr/share/doc/rsync/examples/rsyncd.conf   /etc/rsync/

3. 服务端配置/etc/rsync/rsyncd.conf

#secrets file=/etc/rsync/rsyncd.secrets
#motd file=/etc/rsync/rsyncd.motd
[linuxsirhome]

#path=/usr/share/nginx/html
path=/usr/share/nginx/html

log file=/var/log/rsyncd.log
pid file=/var/log/rsyncd.pid
lock file=/var/run/rsync.lock

read only=no
list=yes
uid=root
gid=root
use chroot=no
max connections=5
comment=share file
#ignore errors

4. 创建同步目录

mkdir /opt/rsyncdate

5. 创建认证文件,修改权限600

vim /etc/rsync/rsync.secrets
user:passwd
chmod 600  /etc/rsync/rsync.secrets

6. 修改 /etc/default/rsync,设置为true

RSYNC_ENABLE=true
RSYNC_CONFIG_FILE= "/etc/rsync/rsyncd.conf"

7. 服务端启动rsync服务

/etc/init.d/rsync start

8. 客户端创建密码认证文件

mkdir /etc/rsync
vim /etc/rsync/rsync.secrets
passwd
chmod 600 /etc/rsync/rsync.secrets

9. 监控需要同步的目录

#!/bin/bash
path='/opt/rsyncdate/'  #远程目录
mod1=rsyncdate  #远程模块
host1='192.168.1.200'  #远程主机ip
user1=www  #远程用户
/usr/bin/inotifywait -mrq --timefmt '%y/%m/%d %H:%M' --format '%T %w %f' -e modify,delete,create,attrib $path | while read file 
do
/usr/bin/rsync -avzP --delete --progress $path $user1@$host1::$mod1 --password-file=/etc/rsync/rsync.secrets
echo "${file} was rsyncd " >> /var/log/rsync.log 2 >&1
done

二、附加脚本

1. 重写 rsync 服务启动脚本:

#!/bin/bash 

#this script for start|stop rsync daemon service 

status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
pidfile="/etc/rsync/rsyncd.pid"
start_rsync="rsync --daemon --config=/etc/rsync/rsyncd.conf"

function rsyncstart() {

    if [ "${status1}X" == "X" ];then

        rm -f $pidfile

        ${start_rsync}

        status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')

        if [  "${status2}X" != "X"  ];then

            echo "rsync service start.......OK" 

        fi

    else

        echo "rsync service is running !"    

    fi
}

function rsyncrestart() {

    if [ "${status1}X" == "X" ];then

               echo "rsync service is not running..." 

               rsyncstart
        else

               rsyncstop

               rsyncstart

        fi
}

case $1 in

        "start")
               rsyncstart
                ;;

        "stop")
               rsyncstop
                ;;

        "status")
               rsyncstatus
               ;;

        "restart")
               rsyncrestart
               ;;

        *)
          echo 
                echo  "Usage: $0 start|stop|restart|status" 
          echo 
esac

2. 启动监控脚本(实时监控):rsync_do.sh

#!/bin/bash

#src='/usr/share/nginx/html'
src='/usr/share/nginx/html'
#src='/tmp/nginx/html'
user='ubuntu'
host='34.192.231.27'
rsync_module='linuxsirhome'

/usr/bin/inotifywait -mrq --exclude=html/App/Runtime --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e modify,delete,create,attrib ${src} \
        | while read DATE TIME DIR file
        do
                # /usr/bin/rsync -vzrtopg --delete --progress /usr/share/nginx/html ubuntu@34.192.231.27::linuxsirhome
                #/usr/bin/rsync -vzrtopg --exclude-from=/etc/rsync/exclude.rule --include-from=/etc/rsync/include.rule --delete --progress ${src} ${user}@${host}::${rsync_module}
                /usr/bin/rsync -vzrtopg --include-from=/etc/rsync/include.rule --exclude-from=/etc/rsync/exclude.rule --exclude=/* --delete --progress ${src} ${user}@${host}::${rsync_module}
                echo "${file} was rsynced at ${DATE}_${TIME} in ${DIR}" >> /var/log/rsync.log 2>&1
        done

3. include.rule

# cat /etc/rsync/include.rule
html

三、目录大量文件 :错误处理:

在对一个大磁盘进行inotify监听时,爆出如下错误:
Failed to watch /mnt/;
upper limit on inotify watches reached!
Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches’.

cat  /proc/sys/fs/inotify/max_user_watches

临时生效:修改 max_user_watches

max_user_watches 这个文件,默认值是8192

echo 99999999 > /proc/sys/fs/inotify/max_user_watches

永久生效:修改 max_user_watches

Linux系统重启inotify配置max_user_watches无效被恢复默认值8192的正确修改方法为:

sudo vim /etc/sysctl.conf 

注意添加一行内容:

fs.inotify.max_user_watches=99999999  #(你想设置的值)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值