当前位置: > Ubuntu >

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

时间:2018-01-16 13:09来源:linux.it.net.cn 作者:IT

一、安装配置

1. 安装rsync,inotify-tools

sudo apt-get install rsync inotify-tools 
  • 1

2. 拷贝rsync配置文件

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

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

4. 创建同步目录

mkdir /opt/rsyncdate
  • 1

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

vim /etc/rsync/rsync.secrets
user:passwd
chmod 600  /etc/rsync/rsync.secrets
  • 1
  • 2
  • 3

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

RSYNC_ENABLE=true
RSYNC_CONFIG_FILE= "/etc/rsync/rsyncd.conf"
  • 1
  • 2

7. 服务端启动rsync服务

/etc/init.d/rsync start
  • 1

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

mkdir /etc/rsync
vim /etc/rsync/rsync.secrets
passwd
chmod 600 /etc/rsync/rsync.secrets
  • 1
  • 2
  • 3
  • 4

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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

二、附加脚本

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

3. include.rule

# cat /etc/rsync/include.rule
html
  • 1
  • 2

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

在对一个大磁盘进行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
  • 1

临时生效:修改 max_user_watches

max_user_watches 这个文件,默认值是8192

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

永久生效:修改 max_user_watches

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

sudo vim /etc/sysctl.conf 
  • 1

注意添加一行内容:

fs.inotify.max_user_watches=99999999  #(你想设置的值)
(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容