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

来源:互联网 发布:网达软件 股票 编辑:程序博客网 时间:2024/05/23 11:54

一、安装配置

1. 安装rsync,inotify-tools

sudo apt-get install rsync inotify-tools 

2. 拷贝rsync配置文件

mkdir /etc/rsynccp /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/htmlpath=/usr/share/nginx/htmllog file=/var/log/rsyncd.logpid file=/var/log/rsyncd.pidlock file=/var/run/rsync.lockread only=nolist=yesuid=rootgid=rootuse chroot=nomax connections=5comment=share file#ignore errors

4. 创建同步目录

mkdir /opt/rsyncdate

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

vim /etc/rsync/rsync.secretsuser:passwdchmod 600  /etc/rsync/rsync.secrets

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

RSYNC_ENABLE=trueRSYNC_CONFIG_FILE= "/etc/rsync/rsyncd.conf"

7. 服务端启动rsync服务

/etc/init.d/rsync start

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

mkdir /etc/rsyncvim /etc/rsync/rsync.secretspasswdchmod 600 /etc/rsync/rsync.secrets

9. 监控需要同步的目录

#!/bin/bashpath='/opt/rsyncdate/'  #远程目录mod1=rsyncdate  #远程模块host1='192.168.1.200'  #远程主机ipuser1=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.secretsecho "${file} was rsyncd " >> /var/log/rsync.log 2 >&1done

二、附加脚本

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.rulehtml

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

在对一个大磁盘进行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  #(你想设置的值)
0 0
原创粉丝点击