實時同步rsync+inotifyhtml
原創博文http://www.cnblogs.com/elvi/p/7658071.htmllinux
#linux同步 #實時同步rsync+inotify,雙向同步inotify+unison #centos6 #關閉selinux、防火牆 chkconfig iptables off chkconfig ip6tables off /etc/init.d/iptables stop /etc/init.d/ip6tables stop sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config sed -i '/^SELINUX=/ s/^SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config grep --color=auto '^SELINUX' /etc/selinux/config #查看 setenforce 0 # 使配置當即生效 vim /etc/hosts #添加hostname名稱 reboot #最好重啓 #更換阿里源 yum -y install wget vim mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #安裝epel包 yum -y install http://mirrors.ustc.edu.cn/fedora/epel/6/x86_64/epel-release-6-8.noarch.rpm yum makecache #生成緩存 ##安裝 yum install -y rsync #server1 IP 172.16.11.31 mkdir -p /bak/sys1 cd /bak/sys1 echo test >$(date +%Y-%m-%d-%H%M%S).txt #server2 IP 172.16.11.32 mkdir -p /bak/sys2 cd /bak/sys2 #ssh同步源 rsync -avz root@172.16.11.31:/bak/sys1/* /bak/sys2 #rsync -avz /bak/sys2 root@172.16.11.31:/bak/sys1/* #rsync同步源配置 vim /etc/rsyncd.conf # uid = nobody gid = nobody user chroot = no max connections = 200 timeout = 600 port = 873 pid file = /var/run/rsyncd.pid #lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log [sys1] comment= sys1 test path=/bak/sys1 ignore errors read only = no list = no #hosts allow = 192.168.0.0/24 auth users = test secrets file = /etc/rsyncd.db echo test:test>/etc/rsyncd.db chown root:root /etc/rsyncd.db chmod 600 /etc/rsyncd.db yum -y install xinetd vim /etc/xinetd.d/rsync # disable = yes 改爲 disable = no service xinetd restart #啓動獨立進程運行 pkill rsync /usr/bin/rsync --daemon #client /usr/bin/rsync -avz test@172.16.11.31::sys1 /bak/sys2 #若須要寫入權限,須要設置目錄 setfacl -m u:nobody:rwx /bak/sys1/ ###免密碼驗證 #ssh祕鑰 ssh-keygen -t rsa -P '' ssh-copy-id root@172.16.11.31 ssh 172.16.11.31 rsync -avz root@172.16.11.31:/bak/sys1/* /bak/sys2 #rsync的同步源,設置變量export RSUNC_PASSWORD=test #設置密碼文件 echo test> /etc/rsyncd.password chmod 600 /etc/rsyncd.password /usr/bin/rsync -avz test@172.16.11.31::sys1 /bak/sys2 --password-file=/etc/rsyncd.password ###實時同步 #安裝inotify-tools yum install make gcc gcc-c++ #安裝編譯工具 cd /usr/local/src wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz tar zxvf inotify-tools-3.14.tar.gz #解壓 cd inotify-tools-3.14 #進入解壓目錄 ./configure && make && make install #編譯 #安裝 #目錄測試 inotifywait -mrq -e create,delete /bak/sys1/ #修改inotify默認參數 sysctl -w fs.inotify.max_queued_events="99999999" sysctl -w fs.inotify.max_user_watches="99999999" sysctl -w fs.inotify.max_user_instances="65535" vim /etc/sysctl.conf #添加如下代碼 fs.inotify.max_queued_events=99999999 fs.inotify.max_user_watches=99999999 fs.inotify.max_user_instances=65535 #建立實時監控腳本 vim /bak/inotify_rsync.sh #!/bin/sh SRC=/bak/sys1/ DST=root@172.16.11.32:/bak/sys2/ #/bin/su - rsync /usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F do /usr/bin/rsync -ahqzt --delete $SRC $DST done # chmod +x /bak/inotify_rsync.sh /bak/inotify_rsync.sh & #echo "/bak/inotify_rsync.sh &" >> /etc/rc.local #開機自啓動 #雙向同步inotify+unison yum install ocaml ocaml-camlp4-devel ctags ctags-etags -y wget http://www.seas.upenn.edu/~bcpierce/unison//download/releases/stable/unison-2.48.4.tar.gz tar -zxvf unison* cd src make UISTYLE=text THREADS=true STATIC=true make install cp unison /usr/local/bin #cp unison /usr/bin #建立實時監控腳本 vim /bak/unison.sh #!/bin/sh SRC=/bak/sys1/ DST=ssh://172.16.11.32//bak/sys2/ /usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F do /usr/local/bin/unison -batch $SRC $DST done # chmod +x /bak/unison.sh /bak/unison.sh & #nohup /bak/unison.sh & #server2也配置運行 vim /bak/unison.sh #!/bin/sh SRC=/bak/sys2/ DST=ssh://172.16.11.31//bak/sys1/ /usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${SRC} | while read D E F do /usr/local/bin/unison -batch $SRC $DST done # mkdir /bak/log nohup /bak/unison.sh &>>/bak/log/unison.log & #開機自啓動(vim /etc/rc.local無效) crontab -e * * * * * /bak/unison.sh >>/bak/log/unison.log 2>&1 & #這樣會形成不少進程 ## echo "nohup /bak/unison.sh >>/bak/log/unison.log 2>&1 &" >> /etc/rc.local #vim /etc/rc.local 測試無效chmod +x /etc/rc.d/rc.local echo $(date) >>11.txt #關閉進程 pkill unison.sh pkill inotifywait