0、rsync+inotify是實現文件實時同步的,加什麼參數才能實現實時同步,--delete參數又是什麼意思?vim
1.運行模式(服務器)bash
rsync有兩種經常使用的認證方式,一種是rsync-daemon方式,咱們使用最多的是rsync-daemon方式。服務器
這種模式是基於C/S模式的,rsync在後臺啓用了一個守護進程,這個守護進程在rsync服務器永久運行,用於接收請求傳輸文件,所以,客戶端既能夠把文件推送(push)給守護進程,也能夠向守護進程拉取(pull)文件。rsync的服務器模式很是適合爲異地的中心備份服務器或數據異地存儲庫來使用。架構
個人實驗架構:ui
10.1.1.36 ---------------push---------------------10.1.185加密
(client) (server)spa
2.安裝配置orm
a.在客戶端10.1.1.36(這是一臺郵件服務器)vim /etc/rsyncd.pass (寫入123456) server
chmod 600 /etc/rsyncd.pass
yum install rsync -y
b.在服務端10.1.1.85(專門用來備份的機器)遞歸
yum install rsync -y
建立 mkdir /data/test,首先進行配置文件的設定,咱們設置的以下:
# cat /etc/rsyncd.conf uid = root gid = root use chroot = no max connections = 10 pid file = /var/run/rsyncd.pid log file = /var/log/rsync.log transfer logging = yes timeout = 900 ignore nonreadable = yes dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 [nextcloud] path = /data/nextcloud comment = pan.comratings.com read only = no write only = no hosts allow = 10.0.2.30 list = no ignore errors auth users = backup secrets file = /etc/rsyncd.pass
增長密碼認證文件:(在服務端)
# vim /etc/rsyncd.pass backup:123456
chmod 600 /etc/rsyncd.pass
啓動服務器:
systemctl start rsyncd.service
在10.1.1.36(client)rsync -vzrtopg --progress ./ --password-file=/etc/rsyncd.pass backup@10.1.1.85::test ------push 增量推送 把 ./下的文件增量得同步到10.1.1.85:/data/test/下
在10.1.1.36 (client) rsync -vzrtopg --progress --password-file=/etc/rsyncd.pass backup@10.1.1.85::test ./ -------pull 增量拉下 把10.1.1.85:/data/test/下文件增量拉到36的./
v是「--verbose」顯示詳細輸出模式 z是「--compress」壓縮模式 r是「--recursive」以子目錄遞歸模式 t是「--times「用來保持文件信息時間 o是」--owner「用來保持文件的屬主信息 p是」--perms「用來保持文件權限 g是」--group「用來保持文件的屬組 --progress:用來顯示數據鏡像同步的過程 --delete:指定以rsync服務器爲基準進行數據鏡像同步,也就是要保持rsync服務器端目錄與客戶端目錄的徹底一致
rsync中的-delete參數是指「 刪除那些DST中SRC沒有的文件」。 --exclude:用於排除不須要文件傳輸的類型
下面是我在客戶端的小腳本,把客戶端本地的文件同步到rsync服務器
經過inotifywait 中的-m參數能夠實現「始終保持事件監聽狀態」
#!/bin/bash
#author:xiaoweige
host_slave=10.1.1.85
master_src=/data/test/
inotify_home=/usr/local/inotify
${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,delete,create,attrib $master_src \
| while read file
do
echo $file
rsync -vzrtopg --progress /data/test/ --password-file=/etc/rsyncd.pass backup@10.1.1.85::test
done