rsync+notify實時同步
簡介
rsync
與傳統的cp
、tar
備份方式相比,rsync
具有安全性高、備份迅速、支持增量備份等優點,通過rsync
可以解決對實時性要求不高的數據備份需求,例如定期的備份文件服務器數據到遠端服務器,對本地磁盤定期做數據鏡像等。
隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync
在高端業務系統中也逐漸暴露出了很多不足,首先,rsync
同步數據時,需要掃描所有文件后進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync
不能實時的去監測、同步數據,雖然它可以通過linux
守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基於以上原因,rsync
+inotify
組合出現了!
Inotify
是一種強大的、細粒度的、異步的文件系統事件監控機制,linux
內核從2.6.13
起,加入了Inotify
支持,通過Inotify
可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools
就是這樣的一個第三方軟件。
在前面有講到,rsync可以實現觸發式的文件同步,但是通過crontab
守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify
可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync
同步,這樣剛好解決了同步數據的實時性問題。
原理
Inotify只需要部署在同步的源服務器上;
當監控的文件有變化觸動 rsync腳本來同步。
實驗:
環境說明:
服務器類型 | 主機名 | IP地址 | 安裝的應用 | 系統版本 |
---|---|---|---|---|
源服務器 | node1 | 192.168.110.12 | rsync inotify-tools | redhat 8 |
目標服務器 | node2 | 192.168.110.13 | rsync | redhat 8 |
目標
- 本次需要把源服務器上/opt目錄實時同步到目標服務器的/opt/下
要求
- 實時同步目錄
- 開機實時同步功能自動啟動
准備工作:
關閉防火牆,安裝應用
//node1
#關閉防火牆和selinux
[root@node1 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node1 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@node1 ~]# setenforce 0
#安裝rsync命令,inotify-tools工具
[root@node1 ~]# yum -y install rsync
[root@node2 ~]# yum -y install inotify-tools
//node2
#關閉防火牆和selinux
[root@node2 ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@node2 ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@node2 ~]# setenforce 0
#安裝rsync命令
[root@node2 ~]# yum -y install rsync
創建測試文件
//node1
#創建test1-3目錄
[root@node1 opt]# pwd
/opt
[root@node1 opt]# mkdir test{1..3}
[root@node1 opt]# ls
test1 test2 test3
#在test1-3的每一個目錄下寫入file1-3
[root@node1 opt]# touch test1/file{1..3} test2/file{1..3} test3/file{1..3}
[root@node1 opt]# tree .
.
├── test1
│ ├── file1
│ ├── file2
│ └── file3
├── test2
│ ├── file1
│ ├── file2
│ └── file3
└── test3
├── file1
├── file2
└── file3
3 directories, 9 files
開始部署
配置目標服務器
//node2
#編輯rsyncd.conf配置文件
[root@node2 ~]# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass
[etc_from_client]
path = /opt/
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = admin
#創建用戶認證文件
# $RANDOM表示隨機數
[root@node2 ~]# echo "admin:$RANDOM" > /etc/rsync.pass
[root@node2 ~]# cat /etc/rsync.pass
admin:11196 //記住這個免密,等下需要在源服務器上用到
#設置文件權限
[root@node2 ~]# chmod 600 /etc/rsync*
[root@node2 ~]# ll /etc/rsync*
-rw-------. 1 root root 396 May 11 17:48 /etc/rsyncd.conf
-rw-------. 1 root root 12 May 11 17:49 /etc/rsync.pass
#啟動rsync服務
[root@node2 ~]# rsync --daemon
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:22 [::]:*
配置源服務器
//node2
#創建認證密碼文件,密碼是剛在在目標服務器種生成的密碼
[root@node1 ~]# echo '11196' > /etc/rsync.pass
[root@node1 ~]# cat /etc/rsync.pass
11196
#設置密碼文件權限,只設置文件所有者具有讀取、寫入權限即可
[root@node1 ~]# chmod 600 /etc/rsync.pass
[root@node1 ~]# ll /etc/rsync.pass
-rw-------. 1 root root 6 May 11 19:12 /etc/rsync.pass
測試
//node2
#查看node2的opt文件
[root@node2 ~]# ls /opt/
//node1
#執行命令
[root@node1 ~]# rsync -avH --port 873 --progress --delete /opt admin@192.168.110.13::etc_from_client --password-file=/etc/rsync.pass
sending incremental file list
opt/
opt/test1/
opt/test1/file1
0 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=8/13)
opt/test1/file2
0 100% 0.00kB/s 0:00:00 (xfr#2, to-chk=7/13)
opt/test1/file3
0 100% 0.00kB/s 0:00:00 (xfr#3, to-chk=6/13)
opt/test2/
opt/test2/file1
0 100% 0.00kB/s 0:00:00 (xfr#4, to-chk=5/13)
opt/test2/file2
0 100% 0.00kB/s 0:00:00 (xfr#5, to-chk=4/13)
opt/test2/file3
0 100% 0.00kB/s 0:00:00 (xfr#6, to-chk=3/13)
opt/test3/
opt/test3/file1
0 100% 0.00kB/s 0:00:00 (xfr#7, to-chk=2/13)
opt/test3/file2
0 100% 0.00kB/s 0:00:00 (xfr#8, to-chk=1/13)
opt/test3/file3
0 100% 0.00kB/s 0:00:00 (xfr#9, to-chk=0/13)
sent 600 bytes received 211 bytes 1,622.00 bytes/sec
total size is 0 speedup is 0.00
//node2
#執行成功,node2上的opt目錄下有node1的opt文件
[root@node2 ~]# ls /opt/
opt
[root@node2 ~]# cd /opt/
[root@node2 opt]# ls
opt
[root@node2 opt]# tree
.
└── opt
├── test1
│ ├── file1
│ ├── file2
│ └── file3
├── test2
│ ├── file1
│ ├── file2
│ └── file3
└── test3
├── file1
├── file2
└── file3
4 directories, 9 files
使用inodify實時觸發rsync進行同步
//node1
#查看服務器內核是否支持inotify,如果有這三個max開頭的文件則表示服務器內核支持inotify
[root@node1 ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 May 11 16:15 max_queued_events
-rw-r--r--. 1 root root 0 May 11 16:15 max_user_instances
-rw-r--r--. 1 root root 0 May 11 16:15 max_user_watches
#編寫同步腳本
[root@node1 ~]# vim /scripts/inotify.sh
#/bin/bash
host=192.168.110.13 # 目標服務器的ip(備份服務器)
src=/opt # 在源服務器上所要監控的備份目錄(此處可以自定義,但是要保證存在)
des=etc_from_client # 自定義的模塊名,需要與目標服務器上定義的同步名稱一致
password=/etc/rsync.pass # 執行數據同步的密碼文件
user=admin # 執行數據同步的用戶名
inotifywait=/usr/bin/inotifywait
$inotifywait -mrq --timefmt '%Y%m%d %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src \
| while read files;do
rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done
#設置腳本權限755
[root@node1 ~]# chmod 755 /scripts/inotify.sh
[root@node1 ~]# ll /scripts/inotify.sh
-rwxr-xr-x. 1 root root 431 May 11 18:45 /scripts/inotify.sh
#啟動腳本
#nohub command & :后台運行,關掉終端程序不會停止,還會繼續運行
[root@node1 ~]# nohup bash /scripts/inotify.sh &
[1] 14937
[root@node1 ~]# nohup: ignoring input and appending output to 'nohup.out'
(直接回車)
#查看后台運行程序
[root@node1 ~]# ps -ef | grep inotify
root 14937 14902 0 19:56 pts/2 00:00:00 bash /scripts/inotify.sh
root 14938 14937 0 19:56 pts/2 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /opt
root 14939 14937 0 19:56 pts/2 00:00:00 bash /scripts/inotify.sh
root 14941 14902 0 19:56 pts/2 00:00:00 grep --color=auto inotify
測試
//node1
#在opt下創建一個新目錄test4
[root@node1 ~]# mkdir /opt/test4
[root@node1 ~]# ls /opt/
test1 test2 test3 test4
//node2
#查看一下,成功實時同步
[root@node2 opt]# pwd
/opt
[root@node2 opt]# tree
.
└── opt
├── test1
│ ├── file1
│ ├── file2
│ └── file3
├── test2
│ ├── file1
│ ├── file2
│ └── file3
├── test3
│ ├── file1
│ ├── file2
│ └── file3
└── test4
5 directories, 9 files
設置腳本文件開機自啟動
//node1
#添加到rc.local文件中
[root@node1 ~]# chmod +x /etc/rc.d/rc.local
[root@node1 ~]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 510 May 12 09:13 /etc/rc.d/rc.local
[root@node1 ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
[root@node1 ~]# tail /etc/rc.d/rc.local
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.
touch /var/lock/subsys/local
nohup /bin/bash /scripts/inotify.sh //添加了這一行
設置rsync開機自啟動service文件
//node2
#配置rsyncd.service啟動文件
[root@node2 ~]# vim /usr/lib/systemd/system/rsyncd.service
[Unit]
Description=fast remote file copy program daemon
ConditionPathExists=/etc/rsyncd.conf
[Service]
EnvironmentFile=/etc/sysconfig/rsyncd
ExecStart=/usr/bin/rsync --daemon --no-detach
[Install]
WantedBy=multi-user.target
#創建rsyncd文件,給service文件調用
[root@node2 ~]# touch /etc/sysconfig/rsyncd
#重讀一下文件
[root@node2 ~]# systemctl daemon-reload
#使用systemctl命令,開機自啟
[root@node2 ~]# systemctl enable --now rsyncd
[root@node2 ~]# systemctl status rsyncd
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: >
Active: active (running) since Wed 2021-05-12 09:58:50 CST; 1min 54s ago
Main PID: 2149 (rsync)
Tasks: 1 (limit: 11300)
Memory: 268.0K
CGroup: /system.slice/rsyncd.service
└─2149 /usr/bin/rsync --daemon --no-detach
May 12 09:58:50 node2 systemd[1]: Started fast remote file copy program daemon.
重啟查看狀態
//重啟
[root@node1 ~]# reboot
[root@node2 ~]# reboot
//node1
#腳本文件也在運行
[root@node1 ~]# ps -ef | grep inotify
root 977 970 0 10:05 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 981 977 0 10:05 ? 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /opt
root 982 977 0 10:05 ? 00:00:00 /bin/bash /scripts/inotify.sh
root 1446 1421 0 10:07 pts/1 00:00:00 grep --color=auto inotify
//node2
#873端口已經啟動
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 5 0.0.0.0:873 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 [::]:873 [::]:*
LISTEN 0 128 [::]:22 [::]:*
測試
//node1
#在opt下創建abc目錄
[root@node1 opt]# pwd
/opt
[root@node1 opt]# mkdir abc
[root@node1 opt]# ls
abc test1 test2 test3
//node2
#可以看到abc目錄
[root@node2 opt]# pwd
/opt
[root@node2 opt]# ls
opt
[root@node2 opt]# tree
.
└── opt
├── abc
├── test1
│ ├── file1
│ ├── file2
│ └── file3
├── test2
│ ├── file1
│ ├── file2
│ └── file3
└── test3
├── file1
├── file2
└── file3