inotify+rsync

1、服务器配置rsync

vim /etc/rsyncd.secrets #配置用户和密码

cOolyou2015

chmod 600 /etc/rsyncd.secrets

chown -R www:www /data/web/src/

vim /etc/rsyncd.conf#配置文件

pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.2.14
uid = root
gid = root
use chroot = yes
read only = yes
charset = utf-8
hosts allow = 192.168.2.0/24
hosts deny=*
max connections = 5


log file = /var/log/rsync.log


log format = %t %a %m %f %b
syslog facility = local3
timeout = 300


[web]
path = /data/src
list = yes
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
comment = This is tongji_src
exclude = .svn/

2、客户端配置rsync

vim /etc/rsyncd.secrets #配置用户和密码

www:cOolyou2015

chmod 600 /etc/rsyncd.secrets

mkdir /data/src

vim /etc/rsyncd.conf #配置文件

# This line is required by the /etc/init.d/rsyncd script
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.2.15
uid = www
gid = www
use chroot = yes
read only = no
charset = utf-8
hosts allow = 192.168.2.14
hosts deny=*
max connections = 5


log file = /data/logs/rsync.log


log format = %t %a %m %f %b
syslog facility = tv_php_01
timeout = 300


[src]
path = /data/src/
list = yes
ignore errors
auth users = www
secrets file = /etc/rsyncd.secrets
comment = This is php_01_src
exclude =.git/ .svn/ develop/console/ product/console/ uploads/ news/#服务器推送会根据这个设置排除同步目录下所有的排除目录



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

chown www:www /data/src/ -R

3、创建inotify+rsync脚本

yum install inotify-tools

mkdir /root/shell
vim /root/shell/rsync.sh

#!/bin/bash
src=/data/web/src/
des=src
rsync_passwd_file=/etc/rsyncd.secrets
INOTIFY_EXCLUDE='/etc/inotify_exclude.lst'
RSYNC_EXCLUDE='/etc/rsync_exclude.lst'
log_file=/data/logs/rsync.log
# backend
ip1=192.168.2.15
#api-1
ip2=192.168.2.13
#api-2
ip3=192.168.2.12
# tv_jingcai
#ip4=10.19.140.72
# rest_01
#ip5=10.10.3.37
# rest_02
#ip6=10.10.32.244
user=www
cd ${src}
inotify_fun(){
/usr/bin/inotifywait -mrq --exclude    "(^(.*)/(\.svn|\.git))|(\.\/vendor)|(sjjx\/vendor)|(oldapi\/vendor)"    --format  '%Xe %w%f' -e modify,create,delete,close_write,move ./ | while read file
do
        INO_EVENT=$(echo $file | awk '{print $1}')
        INO_FILE=$(echo $file | awk '{print $2}')
        echo "-------------------------------$(date)------------------------------------"
        echo $file
        if [[ $INO_EVENT =~ 'CREATE' ]] || [[ $INO_EVENT =~ 'MODIFY' ]] || [[ $INO_EVENT =~ 'CLOSE_WRITE' ]] || [[ $INO_EVENT =~ 'MOVED_TO' ]]
        then
                echo 'CREATE or MODIFY or CLOSE_WRITE or MOVED_TO'
                rsync -avzcR --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des}
                rsync -avzcR --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
rsync -avzcR --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip3}::${des}
fi        
        if [[ $INO_EVENT =~ 'DELETE' ]] || [[ $INO_EVENT =~ 'MOVED_FROM' ]]
        then
                echo 'DELETE or MOVED_FROM'
                rsync -avzR --delete --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des}
                rsync -avzR --delete --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
rsync -avzR --delete --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip3}::${des}
fi
        if [[ $INO_EVENT =~ 'ATTRIB' ]]
        then
                echo 'ATTRIB'
                if [ ! -d "$INO_FILE" ]
                then
                        rsync -avzcR --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip1}::${des}
                        rsync -avzcR --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip2}::${des}
                        rsync -avzcR --exclude-from "/etc/rsyncd.list" --password-file=${rsync_passwd_file} $(dirname ${INO_FILE}) ${user}@${ip3}::${des}
                fi
        fi
done
}
inotify_fun >> ${log_file} 2>&1 &

4、每小时全局同步一次

vim rsync_cron.sh

#!/bin/bash
cd /data/web/src/
/usr/bin/rsync -avzR --delete --exclude-from "/etc/rsyncd.list"--password-file=/etc/rsyncd.secrets ./* [email protected]::src
/usr/bin/rsync -avzR --delete --exclude-from "/etc/rsyncd.list"--password-file=/etc/rsyncd.secrets ./* [email protected]::src
/usr/bin/rsync -avzR --delete --exclude-from "/etc/rsyncd.list" --password-file=/etc/rsyncd.secrets ./* [email protected]::src


crontab -e

0 */1 * * * /bin/sh /root/shell/rsync_cron.sh

service crond reload


5、rsync排除

vim /etc/rsyncd.list

vendor
sjjx/vendor
oldapi/vendor

/usr/bin/rsync -avzR --delete --exclude-from "/etc/rsyncd.list" --password-file=/etc/rsyncd.secrets ./* [email protected]::src

6、报错

rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1526) [generator=3.0.6]

服务器推向客户端,客户端yum安装的3.0.6版本,在rsyncd.conf加添加exclude排除,就会报上面错误

wget https://download.samba.org/pub/rsync/src/rsync-3.1.2.tar.gz

cd rsync-3.1.2

./configure

make

make install

编译安装最新版本后正常

/usr/local/bin/rsync

7、报错

Please increase the amount of inotify watches allowed per user via `/proc/sys/fs/inotify/max_user_watches’.

vim /etc/sysctl.conf添加

fs.inotify.max_user_watches=10000000 #设置监控目录个数,默认为8016太小了
fs.inotify.max_user_instances=6553 #最多开启实例

sysctl -p

你可能感兴趣的:(自动同步工具,inotify+rsync)