鹰击长空yuetao.org

2月 12

[原创]inotify rsync 文件实时同步 不指定

yuetao , 14:41 , linux » inotify , 评论(0) , 引用(0) , 阅读(8175) , Via 本站原创 | |
/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
Tags: ,
发表评论

昵称

主页

邮箱

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]