CentOS7之Rsync+Inotify架構實現實時同步文件和文件夾

什麼是rsync?html

sync官方網站: https://www.samba.org/ftp/rsync/rsync.htmllinux

rsync是能夠實現增量備份的工具。配合任務計劃,rsync能實現定時或間隔同步,配合inotify或sersync,能夠實現觸發式的實時同步。web

rsync工做方式:shell

rsync有三種工做方式:vim

(1).本地文件系統上實現同步。命令行語法格式爲上述」Local」段的格式。bash

(2).本地主機使用遠程shell和遠程主機通訊。命令行語法格式爲上述」Access via remote shell」段的格式。服務器

(3).本地主機經過網絡套接字鏈接遠程主機上的rsync daemon。命令行語法格式爲上述」Access via rsync daemon」段的格式。網絡

前二者的本質是經過管道通訊,即便是遠程shell。而方式(3)則是讓遠程主機上運行rsync服務,使其監聽在一個端口上,等待客戶端的鏈接。ide

由上面能夠知道:rsync能夠本地,遠程同步文件。能夠定時,或者時間間隔可是不能實時!工具

詳情參考網址:http://www.javashuo.com/article/p-hgmuxrfn-cm.html

什麼是inotity?

Inotify API用於檢測文件系統變化的機制。Inotify可用於檢測單個文件,也能夠檢測整個目錄。當檢測的對象是一個目錄的時候,目錄自己和目錄裏的內容都會成爲檢測的對象。

此種機制的出現的目的是當內核空間發生某種事件以後,能夠當即通知到用戶空間。方便用戶作出具體的操做。

由上可知道:inotiy用於監聽文件夾變化

inotity詳情參考:https://blog.csdn.net/longwang155069/article/details/54016789

那麼由rsync+inotity 就能夠作到實時去同步文件了

web1服務器(內發發布節點:rsync+inotify)
1.安裝Inotify

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
</span>

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
</span>
yum install rsync -y
wget http://rsync.samba.org/ftp/rsync/src/rsync-3.0.9.tar.gz
tar zxvf rsync-3.0.9.tar.gz
cd rsync-3.0.9
./configure –prefix=/usr/local/rsync
make
make install

–安裝完後,就會產生下面兩個命令
/usr/local/bin/inotifywait
/usr/local/bin/inotifywatch

安裝完畢後:

vim /etc/rsyncd.conf

建立密碼文件
vim /etc/server.pass

web123
vim /etc/server.pass

web123
<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/server.pass
1
<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/server.pass
/usr/bin/rsync --daemon
1
/usr/bin/rsync --daemon
echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local
1
echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local
[root@zabbix-server ~]# ps -ef | grep rsync
root 23846 1 0 15:09 ? 00:00:00 /usr/bin/rsync --daemon
root 23857 10697 0 15:09 pts/0 00:00:00 grep --color rsync
1
2
3
[root@zabbix-server ~]# ps -ef | grep rsync
root 23846 1 0 15:09 ? 00:00:00 /usr/bin/rsync --daemon
root 23857 10697 0 15:09 pts/0 00:00:00 grep --color rsync
編寫腳本

vim /web/inotifyrsync.sh
#!/bin/bash
host1=192.168.100.26 //被同步服務器IP
src=/web/wwwroot/
dst1=web1
user1=web1user
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1 > /dev/null 2>&1
echo "${files} was rsynced." >> /tmp/rsync.log 2>&1
done
vim /web/inotifyrsync.sh
#!/bin/bash
host1=192.168.100.26 //被同步服務器IP
src=/web/wwwroot/
dst1=web1
user1=web1user
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src \
| while read files
do
/usr/bin/rsync -vzrtopg --delete --progress --password-file=/etc/server.pass $src $user1@$host1::$dst1 > /dev/null 2>&1
echo "${files} was rsynced." >> /tmp/rsync.log 2>&1
done

爲其指定可執行權限,而後放入後臺運行
<span class="hljs-attribute">chmod</span> <span class="hljs-number">755</span> /web/inotifyrsync.sh
/web/inotifyrsync.sh &
1
2
<span class="hljs-attribute">chmod</span> <span class="hljs-number">755</span> /web/inotifyrsync.sh
/web/inotifyrsync.sh &
將腳本加入系統自啓動文件
echo <span class="hljs-string">"/web/inotifyrsync.sh &"</span> <span class="hljs-meta">>> </span>/etc/rc.local
1
echo <span class="hljs-string">"/web/inotifyrsync.sh &"</span> <span class="hljs-meta">>> </span>/etc/rc.local

被同步服務器設置

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
yum install rsync -y
</span>

<span class="hljs-attribute">systemctl</span> stop firewalld
setenforce <span class="hljs-number">0
sed -i "s/enforcing/disabled/" /etc/selinux/config
yum install rsync -y
</span>
vim /etc/rsyncd.conf

#/etc/rsyncd: configuration file for rsync daemon mode
#See rsyncd.conf man page for more options.
#configuration example:
uid = nobody
gid = nobody
use chroot = yes
max connections = 10
strict mode=yes
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
[web1]
path = /web1/wwwroot
comment = web1 file
ignore errrors
read only=no
write only=no
hosts allow=*
hosts deny=192.168.100.10
list=false
uid=root
gid=root
auth users=web1user
secrets file=/etc/web1.pass

#/etc/rsyncd: configuration file for rsync daemon mode
#See rsyncd.conf man page for more options.
#configuration example:
uid = nobody
gid = nobody
use chroot = yes
max connections = 10
strict mode=yes
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsync.lock
log file=/var/log/rsyncd.log
[web1]
path = /web1/wwwroot
comment = web1 file
ignore errrors
read only=no
write only=no
hosts allow=*
hosts deny=192.168.100.10
list=false
uid=root
gid=root
auth users=web1user
secrets file=/etc/web1.pass
建立密碼文件(文件格式:user:pass)
vim /etc/web1.pass
1
vim /etc/web1.pass

修改密碼文件權限<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/web1.pass1<span class="hljs-attribute">chmod</span> <span class="hljs-number">600</span> /etc/web1.pass4.啓動rsync守護進程/usr/bin/rsync --daemon 1/usr/bin/rsync --daemon 5.加入系統自啓動文件echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local1echo <span class="hljs-string">"/usr/bin/rsync --daemon"</span> <span class="hljs-meta">>> </span>/etc/rc.local測試在web1服務器節點的/web/wwwroot目錄下添加、刪除、修改文件,而後到web2服務器節點對應目錄去查看文件是否跟隨發佈節點同步變化。

相關文章
相關標籤/搜索