同步文件,多個主機。能夠作圖片服務同步,代碼管理同步等。經過異步方式同步,監控到文件的變化。同步更新變化的內容,效率比較好。linux
服務類型 | IP地址 | 應用 | 操做系統 |
---|---|---|---|
源服務器 | 192.168.217.151 | rsync inotify-tools 腳本 | centos7/redhat7 |
目標服務器 | 192.168.217.150 | rsync | centos7/redhat7 |
# systemctl stop firewalld # systemctl disable firewalld # sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux # getenforce 0
#yum -y install rsync
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地址,能夠設置多個,用英文逗號隔開
# vim /etc/rsyncd.conf
# mkdir /test
echo 'admin:111' > /etc/rsync.passcat /etc/rsync.pass
chmod 600 /etc/rsync* ll /etc/rsync*
# systemctl start rsyncd # systemctl enable rsyncd
# systemctl stop firewalld # systemctl disable firewalld # sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux # setenforce 0
yum -y install rsync
c++
echo '111' > /etc/rsync.pass # cat /etc/rsync.pass
# chmod 600 /etc/rsync.pass # ll /etc/rsync.pass
rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.217.150::etc_from_client --password-file=/etc/rsync.pass
test
ls /
檢查服務器內核是否支持inotify,若是有這三個max開頭的文件則表示服務器內核支持inotify
ll /proc/sys/fs/inotify/
yum -y install make gcc gcc-c++ inotify-tools
# mkdir /scripts
vim
# 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
# chmod +x /etc/rc.d/rc.localll /etc/rc.d/rc.local
echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local
tail /etc/rc.d/rc.local