inotify+rsync 实现实时同步
為什么80%的碼農都做不了架構師?>>> ??
實現實時同步(也就是源一變化,它就會觸發同步)
inotify+rsync
?
源服務器:172.16.12.167
同步服務器:172.16.12.169
先操作同步服務器
1設置SELINUX
vi /etc/selinux/config?#編輯防火墻配置文件
#SELINUX=enforcing?#注釋掉
#SELINUXTYPE=targeted?#注釋掉
SELINUX=disabled?#增加
:wq!?#保存,退出
setenforce 0 ?#立即生效
2、開啟防火墻tcp 873端口(Rsync默認端口)
vi /etc/sysconfig/iptables?#編輯防火墻配置文件
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
:wq!?#保存,退出
/etc/init.d/iptables restart?#最后重啟防火墻使配置生效
3安裝Rsync服務端?
yum -y install rsync
4安裝xinetd ,把服務托管給xinetd
yum install rsync xinetd?#安裝
vi /etc/xinetd.d/rsync?#編輯配置文件,設置開機啟動rsync
disable = no?#修改為no
:wq!?#保存退出
service xinetd start 啟動xinetd
創建rsyncd.conf配置文件
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
motd file = /etc/rsyncd.Motd
[web]
path = /data/backup
comment = web
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = jerry
:wq!?#保存退出
配置項說明
vi /etc/rsyncd.conf?#創建配置文件,添加以下代碼
log file = /var/log/rsyncd.log?#日志文件位置,啟動rsync后自動產生這個文件,無需提前創建
pidfile = /var/run/rsyncd.pid??#pid文件的存放位置
lock file = /var/run/rsync.lock??#支持max connections參數的鎖文件
secrets file = /etc/rsync.pass??#用戶認證配置文件,里面保存用戶名稱和密碼,后面會創建這個文件
motd file = /etc/rsyncd.Motd??#rsync啟動時歡迎信息頁面文件位置(文件內容自定義)
[web]?#自定義名稱
path =/data/backup#rsync服務端數據目錄路徑 此路徑必須要在同步服務器中存在,不然會報
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
comment = web #模塊名稱與[web]自定義名稱相同
uid = root?#設置rsync運行權限為root
gid = root?#設置rsync運行權限為root
port=873??#默認端口
use chroot = no?#默認為true,修改為no,增加對目錄文件軟連接的備份
read only = no??#設置rsync服務端文件為讀寫權限
list = no?#不顯示rsync服務端資源列表
max connections = 200?#最大連接數
timeout = 600??#設置超時時間
auth users =jerry#執行數據同步的用戶名,可以設置多個,用英文狀態下逗號隔開
hosts allow = 192.168.21.129??#允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 可以不寫
hosts deny = 192.168.21.254?#禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 可以不寫
?
5.創建用戶認證文件
vi /etc/rsync.pass?#配置文件,添加以下內容
jerry:123456??#格式,用戶名:密碼,可以設置多個,每行一個用戶名:密碼
:wq!??#保存,退出
chmod 600 /etc/rsyncd.conf??#設置文件所有者讀取、寫入權限
chmod 600 /etc/rsync.pass??#設置文件所有者讀取、寫入權限
service xinetd restart #重啟xinetd
?
?在源服務器上操作:172.16.12.167
1設置SELINUX
vi /etc/selinux/config?#編輯防火墻配置文件
#SELINUX=enforcing?#注釋掉
#SELINUXTYPE=targeted?#注釋掉
SELINUX=disabled?#增加
:wq!?#保存,退出
setenforce 0 ?#立即生效
2、開啟防火墻tcp 873端口(Rsync默認端口)
vi /etc/sysconfig/iptables?#編輯防火墻配置文件
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
:wq!?#保存,退出
/etc/init.d/iptables restart?#最后重啟防火墻使配置生效
3安裝Rsync服務端?
yum -y install rsync
4安裝xinetd ,把服務托管給xinetd
yum install rsync xinetd?#安裝
vi /etc/xinetd.d/rsync?#編輯配置文件,設置開機啟動rsync
disable = no?#修改為no
:wq!?#保存退出
service xinetd start 啟動xinetd
5、創建認證密碼文件
vi /etc/passwd.txt??#編輯文件,添加以下內容
123456?#密碼
:wq!?#保存退出
?
?測試下RSYNC是否能同步
試源服務器172.16.12.167到同步服務器172.16.12.169之間的數據同步
mkdir /home/test/cheshi?#在源服務器上創建測試文件夾,然后在源服務器運行下面命令
rsync -avH ?--port=873 --progress --delete ?/home/test jerry@172.16.12.169::web --password-file=/etc/passwd.txt?注意空隔,如果多一個空隔可能會報錯。,本人在測試中,因為用戶名和前面的目錄有兩個空隔,結果報用戶名沒有授權
安裝Inotify-tools工具,實時觸發rsync進行同步
是監控源服務器,所有在源服務器上安裝inotify-tools
1、查看服務器內核是否支持inotify
ll /proc/sys/fs/inotify ??#列出文件目錄,出現下面的內容,說明服務器內核支持inotify
-rw-r--r-- 1 root root 0 Mar? 7 02:17 max_queued_events
-rw-r--r-- 1 root root 0 Mar? 7 02:17 max_user_instances
-rw-r--r-- 1 root root 0 Mar? 7 02:17 max_user_watches
備注:Linux下支持inotify的內核最小為2.6.13,可以輸入命令:uname -a查看內核
CentOS 5.X 內核為2.6.18,默認已經支持inotify
2、安裝inotify-tools
yum install make? gcc gcc-c++??#安裝編譯工具
wget?http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxvf inotify-tools-3.14.tar.gz -C /usr/local/src?#解壓
cd?/usr/local/src inotify-tools-3.14?#進入解壓目錄
./configure --prefix=/usr/local/inotify??#配置
make??#編譯
make install??#安裝
--安裝完后,就會產生下面兩個命令
/usr/local/bin/inotifywait
/usr/local/bin/inotifywatch
#?/usr/local/bin/inotifywait?--help
/usr/local/bin/inotifywait:?error?while?loading?shared?libraries:?libinotifytools.so.0:?cannot?open?shared?object?file:?No?such?file?or?directory
如果報錯,找不到那個庫,這個庫實際上是在/usr/local/lib下的
解決方法
#?echo?/usr/local/lib/??>?/etc/ld.so.conf.d/abc.conf
#?ldconfig
3、設置系統環境變量,添加軟連接
echo "PATH=/usr/local/inotify/bin:$PATH" >>/etc/profile.d/inotify.sh
source /etc/profile.d/inotify.sh??#使設置立即生效
echo "/usr/local/inotify/lib" >/etc/ld.so.conf.d/inotify.conf
ln -s /usr/local/inotify/include? /usr/include/inotify
4、修改inotify默認參數(inotify默認內核參數值太小)
查看系統默認參數值
sysctl -a | grep max_queued_events
結果是:fs.inotify.max_queued_events = 16384
sysctl -a | grep max_user_watches
結果是:fs.inotify.max_user_watches = 8192
sysctl -a | grep max_user_instances
結果是:fs.inotify.max_user_instances = 128
修改參數:
sysctl -w fs.inotify.max_queued_events="99999999"
sysctl -w fs.inotify.max_user_watches="99999999"
sysctl -w fs.inotify.max_user_instances="65535"
vi /etc/sysctl.conf?#添加以下代碼
fs.inotify.max_queued_events=99999999
fs.inotify.max_user_watches=99999999
fs.inotify.max_user_instances=65535
:wq!?#保存退出
參數說明:
max_queued_events:
inotify隊列最大長度,如果值太小,會出現"** Event Queue Overflow **"錯誤,導致監控文件不準確
max_user_watches:
要同步的文件包含多少目錄,可以用:find /home/www.osyunwei.com -type d | wc -l 統計,必須保證max_user_watches值大于統計結果(這里/home/www.osyunwei.com為同步文件目錄)
max_user_instances:
每個用戶創建inotify實例最大值
?
5創建腳本,實時觸發rsync進行同步?.
vi /usr/local/inotify/rsync.sh
/usr/local/bin/inotifywait -mrq ?-e modify,delete,create,attrib,move /home/test |while read events
do
? ? ? ? ? ? ? ? rsync -avH ?--port=873 --progress --delete ?/home/test jerry@172.16.12.169::web --password-file=/etc/passwd.txt
? ? ? ? ? ? ? ? echo "`date +'%F %T'`出現事件events" >> /var/log/rsync.log 2>&1
done
?
chmod +x /usr/local/inotify/rsync.sh???#添加腳本執行權限
腳本參數說明:
srcdir=/home/test ?#源服務器同步目錄
dstdir=web ? #目標服務器rsync同步目錄模塊名稱
rsyncuser=jerry ?#目標服務器rsync同步用戶名
rsyncpassdir=/etc/passwd.txt??#目標服務器rsync同步用戶的密碼在源服務器的存放路徑
dstip="172.16.12.169"??#目標服務器ip,多個ip用空格分開
/tmp/rsync.log??#腳本運行日志記錄
可以運行下rsync.sh執行下,看是否能成功執行
?
6、設置腳本開機自動執行
vi /etc/rc.d/rc.local??#編輯,在最后添加一行
?nohup? /usr/local/inotify/rsync.sh &?#設置開機自動在后臺運行腳本
:wq!??#保存退出
--使用nohup掛起到后臺執行,終端關閉,這個進程也不會被關閉
7、測試inotify實時觸發rsync同步腳本是否正常運行
在源服務器172.16.12.167上創建文件
mkdir /home/test
重新啟動源服務器:172.16.12.167
等系統啟動之后,查看兩臺目標服務器172.16.12.169的/data/backup下是否有test文件夾
?
至此,Linux下Rsync+Inotify-tools實現數據實時同步完成。
轉載于:https://my.oschina.net/bibo/blog/719190
總結
以上是生活随笔為你收集整理的inotify+rsync 实现实时同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 随笔 2022年11月第二周
- 下一篇: Span<T> —— .NET Core