1. 程式人生 > >部署rsync+inotify服務

部署rsync+inotify服務

部署rsync+inotify

  • rsync簡介
    rsync是linux系統下的資料映象備份工具。使用快速增量備份工具 Remote Sync 可以遠端同步,支援本地複製,或者與其他ssh,rsync同步。
  • inotify簡介
    inotify是一種強大的、細粒度的、非同步的檔案系統事件監控機制,linux核心從2.6.13起,加入了inotify支援。通過inotify可以監控檔案系統中新增、刪除、修改、移動等各種細微事件,利用這個核心介面,第三方軟體就可以監控檔案系統下檔案的各種變化情況,而inotify-tools就是這樣的一個第三方軟體。
環境說明:需要兩臺伺服器來模擬,一臺做為源伺服器,一臺做為目標伺服器。這裡源伺服器地址為192.168.226.128,目標伺服器地址為192.168.226.130。
需求:把源伺服器上/hxd目錄實時同步到目標伺服器/tmp/hxd/目錄下
  • 配置目標伺服器
//關閉防火牆與selinux
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# getenforce 
Enforcing
[[email protected] ~]# setenforce 0
[[email protected] ~]# vim /etc/sysconfig/selinux
SELINUX=disabled  //修改
//安裝rsync軟體
[
[email protected]
~]# yum install -y rsync //修改配置檔案/etc/rsyncd.conf [[email protected] ~]# vim /etc/rsyncd.conf log file = /var/log/rsyncd.log pidfile = /var/run/rsync.pid lock file = /var/run/rsync.lock secrets file = /etc/rsync.pass [hxd] path = /tmp/hxd 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 = dubai hosts allow = 192.168.226.128 hosts deny = 172.16.1.1 //新增以上配置 //建立使用者認證檔案 [
[email protected]
~]# echo 'dubai:123' > /etc/rsync.pass [[email protected] ~]# cat /etc/rsync.pass dubai:123 //修改檔案許可權 [[email protected] ~]# chmod 600 /etc/rsync* [[email protected] ~]# ll /etc/rsync* -rw-------. 1 root root 854 9月 18 19:20 /etc/rsyncd.conf -rw-------. 1 root root 10 9月 18 19:27 /etc/rsync.pass //啟動rsync服務並設定開機自啟 [[email protected] ~]# systemctl start rsyncd [[email protected] ~]# systemctl enable rsyncd Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. [[email protected] ~]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 5 *:873 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 5 :::873 :::*
  • 配置源伺服器
//關閉防火牆與selinux
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# getenforce 
Disabled

//配置yum源
[[email protected] ~]# yum install -y wget
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[[email protected] yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[[email protected] yum.repos.d]#  sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[[email protected] yum.repos.d]#  yum clean all
[[email protected] yum.repos.d]# yum install -y epel-release
//安裝rsync服務端軟體,只需要安裝不需要啟動,也不需要配置。
[[email protected] ~]# yum install -y rsync
//建立認證密碼檔案並設定許可權。
[[email protected] ~]# echo '123' > /etc/rsync.pass
[[email protected] ~]# cat /etc/rsync.pass 
123
[[email protected] ~]# chmod 600 /etc/rsync.pass 
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# mkdir -p hxd/xixi
[[email protected] ~]# ls
anaconda-ks.cfg  hxd
[[email protected] ~]# rsync -acH --port 873 --progress --delete /root/hxd [email protected]::hxd --password-file=/etc/rsync.pass
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
//這裡報錯了,原因是目標主機沒有dubai使用者。和hxd目錄。
//在目標主機先建立dubai使用者和/tmp/hxd目錄
[[email protected] ~]# useradd dubai
[[email protected] ~]# cd /tmp/
[[email protected] tmp]# mkdir hxd

//再回到源主機測試
[[email protected] ~]# rsync -acH --port 873 --progress --delete /root/hxd [email protected]::hxd --password-file=/etc/rsync.pass
sending incremental file list
hxd/
hxd/xixi/
[[email protected] ~]# ls /tmp/hxd/
hxd

//資料同步成功
//安裝inotify-tools工具,實時觸發rsync進行同步
//檢視伺服器核心是否支援inotify
[[email protected] ~]# ll /proc/sys/fs/inotify/
總用量 0
-rw-r--r-- 1 root root 0 9月  19 09:41 max_queued_events
-rw-r--r-- 1 root root 0 9月  19 09:41 max_user_instances
-rw-r--r-- 1 root root 0 9月  19 09:41 max_user_watches

//如果有這三個max開頭的檔案則表示伺服器核心支援inotify
//安裝inotify-tools
[[email protected] ~]# yum install -y inotify-tools
//寫同步指令碼,關鍵的一步,讓指令碼自動去檢測我們制定的目錄下
//檔案發生的變化,然後再執行rsync的命令把它同步到服務端去
[[email protected] ~]# mkdir /scripts
[[email protected]gxiande ~]# touch /scripts/inotify.sh
[[email protected] ~]# chmod 755 /scripts/inotify.sh 
[[email protected] ~]# ll /scripts/inotify.sh 
-rwxr-xr-x 1 root root 0 9月  19 09:56 /scripts/inotify.sh
//編寫指令碼
[[email protected] ~]# vim /scripts/inotify.sh
host=192.168.226.130
src=/root/hxd
des=hxd
password=/etc/rsync.pass
user=dubai
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 [email protected]$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

//新增如上配置
//執行指令碼
[[email protected] ~]# nohup bash /scripts/inotify.sh &
[[email protected] ~]# nohup bash /scripts/inotify.sh &
[1] 1851
[[email protected] ~]# nohup: 忽略輸入並把輸出追加到"nohup.out"

[[email protected] ~]# ps -ef|grep inotify
root       1851   1243  0 10:22 pts/0    00:00:00 bash /scripts/inotify.sh
root       1852   1851  0 10:22 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/hxd
root       1853   1851  0 10:22 pts/0    00:00:00 bash /scripts/inotify.sh
root       1857   1243  0 10:23 pts/0    00:00:00 grep --color=auto inotify

//在源伺服器上生成一個新檔案
[[email protected] ~]# touch hxd/xixi/test
[[email protected] ~]# echo 'hello world' > hxd/xixi/test
//檢視inotify生成的日誌
[[email protected] ~]# tail /tmp/rsync.log 
20180919 10:34 /root/hxd/xixi/testCREATE was rsynced
20180919 10:34 /root/hxd/xixi/testATTRIB was rsynced
20180919 10:34 /root/hxd/xixi/testMODIFY was rsynced

//日誌中有記錄我們所做的操作
//再到目標伺服器上檢視是否已經同步
[[email protected] ~]# cat /tmp/hxd/hxd/xixi/test 
hello world
//同步了 說明是沒問題的
//最後把指令碼設定為開機自啟
[[email protected] ~]# chmod +x /etc/rc.
rc.d/     rc.local  
[[email protected] ~]# chmod +x /etc/rc.d/rc.local 
[[email protected] ~]# ll /etc/rc.d/rc.local 
-rwxr-xr-x. 1 root root 473 6月  27 2017 /etc/rc.d/rc.local
[[email protected] ~]# vim /etc/rc.d/rc.local

nohup /bin/bash /scripts/inotify.sh  //新增這一條配置
這樣rsync+inotify就搭建好了
//錯誤解決
[[email protected] ~]# rsync -acH --port 873 --progress --delete /root/hxd [email protected]::hxd --password-file=/etc/rsync.pass
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
//這裡報錯了,原因是目標主機沒有dubai使用者。和hxd目錄。
//在目標主機先建立dubai使用者和/tmp/hxd目錄