很久沒做這個了,同僚說了一聲才想起來,是以這裡留個筆記
案例
伺服器1 192.168.1.20 (服務端)
伺服器2 192.168.1.21 (推送端)
#檢視伺服器上有沒有rsync的包
#一般是有的。最小安裝可能沒有。手動裝一下
[root@k8s-master01 ~]# rpm -qa rsync
rsync-3.1.2-4.el7.x86_64
#這裡兩個節點建立一下準備同步的目錄
mkdir /apps/
一. 配置服務端 (192.168.1.20)
1. vi /etc/rsyncd.conf
#添加以下配置
motd file = /etc/rsyncd.motd
transfer logging = yes
log file = /var/log/rsyncd.log
port = 873
address = 192.168.1.20
#服務端位址,注釋不可同行,不然會報錯
uid = root
gid = root
use chroot = no
read only = no
max connections = 10
[common]
#标簽名稱,用戶端連接配接時使用
comment = rsync info
path = /apps
#指定服務端同步目錄
ignore errors
auth users = root
secrets file = /etc/rsyncd.secrets
hosts allow = 192.168.1.0/255.255.255.0
#和服務端相同網段
hosts deny = *
list = false
2. 建立使用者密碼檔案
echo "root:123.com" > /etc/rsyncd.secrets
chmod 600 /etc/rsyncd.secrets
#密碼檔案權限必須為600,不然不認
3.啟動rsync服務
systemctl restart rsyncd
systemctl enable rsyncd
二. 部署推送節點 (192.168.1.21)
1. 設定密碼檔案
echo "123.com" > /root/passwd
chmod 600 /root/passwd
2. 啟動服務
systemctl restart rsyncd
systemctl enable rsyncd
三. 測試推送
登陸推送端,建立測試檔案,進行推送測試
#192.168.1.21
cd /apps
touch 1 2 3 4 5
#推送
rsync -avz --password-file=/root/passwd /apps/ [email protected]::common
參數說明
-a #歸檔模式,遞歸傳輸所有檔案,保持權限
-v #詳細資訊
-z #對備份的檔案傳輸時進行壓縮
--password-file=/root/passwd #密碼檔案路徑,用戶端的檔案裡隻需要有密碼即可
/apps/ #先後順序,如果前面放的是本地目錄,後面放的是同步位址,那麼就是推送
#反過來,前面是位址,後面是本地目錄,則是拉取
[email protected]::common #rsync服務端位址 結尾是::+前面我們配置檔案裡的标簽名稱common
三. 實時監聽同步
#拉取軟體包
wget http://rpmfind.net/linux/epel/7/x86_64/Packages/i/inotify-tools-3.14-9.el7.x86_64.rpm
#安裝
rpm -ivh inotify-tools-3.14-9.el7.x86_64.rpm
#!/bin/bash
inotify="inotifywait -mrq -e modify,create,move,delete /apps/ "
rsy="rsync --delete -avz --password-file=/root/passwd /apps/ [email protected]::common"
#這裡指定服務端位址
$inotify | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -ne 0 ] ; then
$rsy
fi
done