您的位置:首页 > 运维架构 > Linux

inotify+rsync实现linux文件批量更新

2015-02-26 15:46 447 查看
---恢复内容开始---

公司网站一台服务器顶不住了,就打算用负载均衡,现在是3台机器一起运行网站,之前做的批量更新一直没写日志,现在补上,也是借鉴了不少博客

这里不想编译安装就直接yum了

yum install rsync

//建立配置文件
#touch /etc/rsyncd.conf

//建立帐号配置文件
#touch /etc/rsyncd.pwd

//配置文件只能自己看
#chmod 600 /etc/rsyncd.conf

//帐号配置文件只能自己看
#chmod 600 /etc/rsyncd.pwd

# vi /etc/rsyncd.conf

uid=root
gid=root
max connections=4
use chroot=yes
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
#auth users=root
secrets file=/etc/rsyncd.pwd
[web]
path = /home/wwwroot/web1/
comment = welcome to 0.1
ignore errors
read only = no
list = yes
auth users = www


vi /etc/rsyncd.pwd

文件内容如下:

www:xxxxxxxxxxxxxxxxxxxxxx


  

启动 rsync server (会自动加载配置文件/etc/rsyncd.conf)
#rsync --daemon

如果开启了iptables,要先打开 tcp 873 的 input(入站)规则

设置密码文件
#echo "xxxxxxxxxxxxxxx" >/script/rsync_remote.pwd

密码文件只能自己看
#chmod 600 /script/rsync_remote.pwd

安装inotifywait

[root@nginx rsync]# cd /usr/src/
[root@nginx src]# wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz [root@nginx src]# tar zxvf inotify-tools-3.14.tar.gz
[root@nginx src]# cd inotify-tools-3.14
[root@nginx inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify
[root@nginx inotify-tools-3.14]# make
[root@nginx inotify-tools-3.14]# make install


下面是我用来更新执行的脚本

/script/inotify_rsync.sh
注释:
host1 host2 为2台需要同步的服务器,如果有更多的服务器需要同步可以在下面增加
------------------------------------------------------------------------
#!/bin/bash
if [ "$1" != "" ] ;
then
echo "run /etc/init.d/inotify start"
exit 1
fi
host1=192.168.0.22
host2=192.168.0.23
src=/home/wwwroot/web1/
src2="`echo $src | sed -e 's/\//+++/g'`"
des=web
user=www
rsync_exclude_file=/script/rsync_skip.setup
password_file=/script/rsync_remote.pwd

do_rsync()
{
files=$1
user=$2
host=$3
des_url=$4
rsync_exclude_file=$5
password_file=$6
/usr/bin/rsync  --delete -zrtopg --exclude-from=${rsync_exclude_file}  --progress --password-file=${password_file} ${files} $user@$host::$des_url
}

#echo "src2:$src2"
/usr/local/inotify/bin/inotifywait --excludei '(.svn|.swp|.swo|Runtime)' -mrq --timefmt '%d/%m/%y %H:%M' --format '%w::%f' -e close_write,delete,create,attrib $src \
| while read filess
do
files=`echo "$filess" | awk -F:: '{print $1}'`
file=`echo "$filess" | awk -F:: '{print $2}'`
des_dir=`echo "$files" | sed -e 's/\//+++/g' -e "s/${src2}/\//g" -e 's/+++/\//g'`
des_url="${des}${des_dir}"
do_rsync "$files" "$user" "$host1" "$des_url" "$rsync_exclude_file" "$password_file"
do_rsync "$files" "$user" "$host2" "$des_url" "$rsync_exclude_file" "$password_file"
echo "`date` dir:${files} , file:${file}  was rsync to ${host1},${host2}/${des_url}" >> /home/rsync.log
done
----------------------------------------------------------------------------------------------
/etc/init.d/inotify

#! /bin/sh

### BEGIN INIT INFO
# Provides:          inotify
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts inotify
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

case "$1" in
start)
echo -n "Starting inotify "

/script/inotify_rsync.sh&

if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
sleep 2
if [ "`ps aux | grep inotifywait | wc -l`" -gt 1 ] ;
then
echo " done"
else
echo " failed"
exit 1
fi

;;
stop)
echo -n "Gracefully shutting down inotify"

killall inotifywait
killall inotify_rsync.sh
sleep 2
if [ "`ps aux | grep inotifywait | wc -l`" -gt 1 ] ;
then
echo " failed"
exit 1
else
echo " done"
exit 1
fi
;;

restart)
$0 stop
$0 start
;;

*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac


为了方便,在/etc/init.d/里加上一个启动项inotify

#! /bin/sh

### BEGIN INIT INFO
# Provides:          inotify
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts inotify
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

case "$1" in
start)
echo -n "Starting inotify "

/script/inotify_rsync.sh&

if [ "$?" != 0 ] ; then
echo " failed"
exit 1
fi
sleep 2
if [ "`ps aux | grep inotifywait | wc -l`" -gt 1 ] ;
then
echo " done"
else
echo " failed"
exit 1
fi

;;

stop)
echo -n "Gracefully shutting down inotify"
echo -n "Gracefully shutting down inotify"

killall inotifywait
killall inotify_rsync.sh
sleep 2
if [ "`ps aux | grep inotifywait | wc -l`" -gt 1 ] ;
then
echo " failed"
exit 1
else
echo " done"
exit 1
fi
;;

restart)
$0 stop
$0 start
;;

*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;

esac


  这样通过/etc/init.d/inotify start stop来开启关闭同步
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: