rsync+notify 同步(異步方式)文件
阿新 • • 發佈:2018-08-28
usr 內核 17. ble progress pidfile thead reg pan
同步文件,多個主機。可以做圖片服務同步,代碼管理同步等。通過異步方式同步,監控到文件的變化。同步更新變化的內容,效率比較好。
環境說明
服務類型 | IP地址 | 應用 | 操作系統 |
---|---|---|---|
源服務器 | 192.168.217.151 | rsync inotify-tools 腳本 | centos7/redhat7 |
目標服務器 | 192.168.217.150 | rsync | centos7/redhat7 |
在目標服務器上做以下配置
1.關閉防火墻與SELINUX
# systemctl stop firewalld
# systemctl disable firewalld
# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/sysconfig/selinux
# getenforce 0
2.安裝rsync服務端軟件
#yum -y install rsync
3.設置rsyncd.conf配置文件(前後不要有空格)test為同步的文件夾
# vim /etc/rsyncd.conf
log file = /var/log/rsyncd.log
//日誌文件位置,啟動rsync後自動產生,無需提前創建
pidfile = /var/run/rsyncd.pid
//pid文件存放位置
lock file = /var/run/rsync.lock
//支持max connections參數的鎖文件
secrets file = /etc/rsync.pass
//用戶認證配置文件,裏面存放用戶名稱和密碼,必須手動創建這個文件
[etc_from_client]
//自定義同步名稱
path = /test/
//rsync服務端存放路徑,客戶端的數據將同步到此目錄
comment = sync etc from client
uid = root
//設置rsync運行權限為root
gid = root
//設置rsync運行權限為root
port = 873
//默認端口為873
ignore errors
//表示出現錯誤忽視錯誤
use chroot = no
//默認為true ,修改為no,增加對目錄軟鏈接的備份
read only = no
//設置rsync服務端為讀寫權限
list = no
//不顯示rsync服務端資源列表
max connections = 200
//最大連接數
timeout = 600
//設置超時時間
auth users = admin
//執行數據同步的用戶名,可以設置多個,用英文逗號隔開
hosts allow = 192.168.217.151
//允許進行數據同步的IP地址,可以設置多個,用英文逗號隔開
4.創建存放路徑目錄
# mkdir /test
5.創建用戶認證文件
echo ‘admin:111‘ > /etc/rsync.pass
cat /etc/rsync.pass
6.設置文件權限
chmod 600 /etc/rsync*
ll /etc/rsync*
7.啟動rsync服務並設置開機自啟動
# systemctl start rsyncd
# systemctl enable rsyncd
在源服務器上做以下部署:
1.關閉防火墻與SELINUX
# systemctl stop firewalld
# systemctl disable firewalld
# sed -ri ‘s/^(SELINUX=).*/\1disabled/g‘ /etc/sysconfig/selinux
# setenforce 0
2.安裝rsync服務端軟件
yum -y install rsync
3.創建認證密碼文件
echo ‘111‘ > /etc/rsync.pass
# cat /etc/rsync.pass
4.設置文件權限,只設置文件所有者具有讀取、寫入的權限
# chmod 600 /etc/rsync.pass
# ll /etc/rsync.pass
5.在源服務器上創建測試目錄,然後在源服務器上運行以下命令
rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.217.150::etc_from_client --password-file=/etc/rsync.pass
6.運行完成後在目標服務器上查看,在/test/目錄下有test目錄,說明數據同步成功
ls /
test
7.安裝inotify-tools工具,實時觸發rsync同步
檢查服務器內核是否支持inotify,
如果有這三個max開頭的文件則表示服務器內核支持inotify
ll /proc/sys/fs/inotify/
yum -y install make gcc gcc-c++ inotify-tools
8.寫同步腳本
# mkdir /scripts
# touch /scripts/inotify.sh
# chmod 755 /scripts/inotify.sh
# ll
# vim /scripts/inotify.sh
host=192.168.217.150 //目標服務器的ip(備份服務器)
src=/test //在源服務器上所要監控的備份目標
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
//檢查腳本
# bash -x /scripts/inotify.sh
//啟動腳本
nohup bash /scripts/inotify.sh &
# ps -ef|grep inotify
9.設置腳本開機自動啟動
# chmod +x /etc/rc.d/rc.local
ll /etc/rc.d/rc.local
echo ‘nohup /bin/bash /scripts/inotify.sh‘ >> /etc/rc.d/rc.local
tail /etc/rc.d/rc.local
10.誰便在根目錄test裏邊建點文件,到目標服務器上查看是否把新生成的文件自動傳上去了
rsync+notify 同步(異步方式)文件