一,環境信息
1, 使用環境
CentOS Linux release 7.5.1804 (Core)
更新源:192.168.21.131
目標源:192.168.21.132
2, 檢查Rsync是否安裝(沒有的話直接yum安裝即可)
rpm -qa | grep rsync
2.1,我使用的rsync版本
rsync-3.1.2-6.el7_6.1.x86_64
2.2,rsync的常用參數
-v –verbose詳細模式輸出。
-a –archive歸檔模式,表示以遞歸方式傳輸文件,並保持所有文件屬性不變。
-l 保留軟鏈接
-R 保留相對路徑
-H 保留硬鏈接
-p,-o,-g,-A 分別保留權限,屬主,屬組,acl等,但如果加了-a,這些就都包括了
-z, –compress對備份的文件在傳輸時進行壓縮處理。
-D 等於–devices --specials 表示支持b,c,s,p類型的文件
–delete刪除那些DST中存在而在SRC中沒有的文件。
–progress顯示轉移過程中的進度
二,目標服務器的配置,Rsync服務端(192.168.21.132)
3, 定義Rsync配置文件
vim /etc/rsyncd.conf
uid = tongbu #守護進程用戶
gid = tongbu #守護進程組
use chroot = no #不適用chroot
max connections = 100 #最大連接數,0爲不限制
port = 873 #默認端口爲873
timeout = 600 #指定IP的超時時間
pid file = /var/run/rsyncd.pid #Pid存放的位置
lock file = /var/run/rsyncd.lock #鎖文件存放位置,默認爲/var/run/rsync.lock
log file = /var/log/rsyncd.log #日誌文件的存放位置
Log format = %t %a %m %f %b #自定義日誌文件字段
syslog facility = local3 #定義日誌級別,默認是daemon
#模塊參數
[web1] #認證模塊名,在client端需要指定
path = /home #需要做鏡像的目錄
ignore errors #忽略無關的IO錯誤
comment = backup web #模塊的註釋信息
read only = no #是否允許客戶上載文件
list = no #不允許列文件
hosts allow = 192.168.21.0/255.255.255.0 #允許的主機網段
auth users = tongbu #認證的用戶名,如果沒有這行則表明是匿名,此用戶與系統無關
secrets file = /etc/tongbu.pwd #用戶及密碼對比表
3.1,注意
報錯內容:rsync: failed to set times on “xxxx”: Operation not permitted
這裏uid及gid需要與同步的目錄的用戶及組保持一致,否則會報錯
4, 創建認證文件
echo “tongbu:Admin123” >> /etc/tongbu.pwd
5, 設定權限
chmod 600 /etc/{tongbu.pwd,rsyncd.conf}
6, 建立motd文件(rsync的歡迎信息,隨便填寫)
echo “Welcome to use the rsync services!” >> /var/rsyncd.motd
7, 啓動Rsync(以守護進程啓動)
/usr/bin/rsync –daemon
8, 加入自啓動
echo “/usr/bin/rsync --daemon” >> /etc/rc.local
三,更新服務器配置,Rsync客戶端(192.168.21.131)
1, 安裝inotify-tools
1.1, 注意
在安裝inotify-tools前請先確認你的linux內核是否打到了2.6.13,並且在編譯時開啓了CONFIG_INOTIFY選項,也可以通過以下命令檢測
1.2, 命令(出現以下三個MAX則爲開啓)
ls /proc/sys/fs/inotify
max_queued_events max_user_instances max_user_watches
1.3, 安裝inotify-tools
tar -zxf inotify-tools-3.14.gz
cd inotify-tools-3.14
./confifure
Make && make install
1.4, inotifywait的幾個參數介紹
-m 即–monitor,表示始終保持事件監聽狀態。
-r 即–recursive,表示遞歸查詢目錄。
-q 即–quiet,表示打印出監控事件。
-e 即–event,通過此參數可以指定要監控的事件,常見的事件有modify、delete、create、attrib等
–timefmt:指定時間的輸出格式
–format:指定變化文件的詳細信息
2, 編寫實時監控腳本
vim /root/rsync.sh
#!/bin/bash
UNISON=ps -ef |grep -v grep|grep -c inotifywait
if [ ${UNISON} -lt 1 ];then
HOST1=192.168.21.132
SRC=/home/tongbu
DES1=web1
USER1=tongbu
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib ${SRC} |
while read file
do
rsync -vzrtopg --delete --progress ${SRC} ${USER1}@${HOST1}::${DES1} --password-file=/etc/tongbu.pwd &&
echo "${file} was rsynced" >> /tmp/rsync.log 2>&1
echo "---------------------------------------------------------------------------"
done
fi
2.1,如果不需要實時監控的話只需要做一個定時任務即可
30 1 * * * /usr/bin/rsync -vzrtopg --delete --progress /home/tongbu/ [email protected]::web1 --password-file=/etc/tongbu.pwd >/dev/null 2>&1 &
3, 創建認證文件
echo “tongbu:Admin123” >> /etc/tongbu.pwd
4, 設定權限
chmod 600 /etc/tongbu.pwd
5, 檢查啓動
5.1,語法檢查
/usr/bin/bash -n /root/rsync.sh
5.2,後臺啓動
chmod +x /root/rsync.sh
nohup /usr/bin/sh /root/rsync.sh &
6, 在腳本之前可以使用命令先進行測試
rsync -vzrtopg --delete --progress /home/tongbu/ [email protected]::web1 --password-file=/etc/tongbu.pwd
When you don’t know what to do, just settle down and study!