rsync是一個開源的快速備份工具,能夠再不一樣主機之間鏡像同步整個目錄樹,支持增量備份,保持連接和權限,且採用優化的同步算法,再傳輸前執行壓縮,所以很是適用於異地備份、鏡像服務器等應用。html
再遠程同步任務中,負責發起rsync同步操做的客戶機稱爲發起端,而負責響應來自客戶機的rsync同步操做的服務器稱爲同步源。再同步過程當中,同步源負責提供文檔的原始位置,而發起端對該位置具備讀取權限,如圖所示:c++
1.檢查rsync是否安裝算法
[root@localhost ~]# rpm -q rsync rsync-3.0.9-18.el7.x86_64
2.修改rsync默認配置文件,位於/etc/rsyncd.conf。vim
插入如下內容 uid = nobody gid = nobody use chroot = yes //禁錮在源目錄// address = 192.168.126.138 //監聽地址// port 873 //監聽端口// log file = /var/log/rsyncd.log //日誌文件位置// pid file = /var/run/rsyncd.pid //存放進程ID的文件位置// hosts allow = 192.168.126.0/24 //容許訪問的客戶機地址// # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 [wwwroot] //共享模塊名稱// path = /var/www/html //源目錄的實際路徑// read only = no //是否爲只讀// dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 //同步時再也不壓縮的文件類型// auth users = backuper //受權帳戶// secrets file = /etc/rsyncd_users.db //存放帳戶信息的數據文件//
3.爲備份帳戶建立數據文件bash
根據上一步的設置,建立帳號數據文件,添加一行用戶記錄,以冒號分隔,用戶名稱爲backup,密碼爲abc123。因爲帳號信息採起明文存放,所以應調整文件權限,避免帳號信息泄露。服務器
[root@localhost ~]#vim /etc/rsyncd_users.db backuper:abc123 [root@localhost ~]#chmod 600 /etc/rsyncd_users.db
4.開啓rsync服務,運行參數爲 --daemon。tcp
[root@localhost opt]# systemctl stop firewalld.service [root@localhost opt]# setenforce 0 [root@localhost opt]# rsync --daemon [root@localhost opt]# netstat -ntap | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2934/rsync
配置源的方法:ide
在執行運程同步任務時,rsync命令須要指定同步源服務器中的資源位置。rsync同步源的資源表示方式爲「用戶名@主機地址::共享模塊名」或者「rsync://用戶名@主機地址/共享模塊名」,前者爲兩個冒號分隔形式,後者爲URL地址形式。工具
rsync -avz backuper@192.168.175.129::wwwroot /opt/ rsync -avz rsync://backuper@192.168.175.129/wwwroot /opt/
1.執行如下操做將源服務器中的wwwroot共享模塊,下載到客戶機的本地/var/www/html目錄下。優化
源服務器: [root@localhost ~]# cd /var/www/html/ [root@localhost html]# echo "123" > 111.txt [root@localhost html]# echo "456" > 222.txt [root@localhost html]# ls 111.txt 222.txt 客戶端: [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ //下載到當前目錄// Password: receiving incremental file list ./ 111.txt 222.txt sent 102 bytes received 221 bytes 23.93 bytes/sec total size is 8 speedup is 0.02 [root@localhost html]# ls 111.txt 222.txt
2.在客戶端上傳文件到源服務器
[root@localhost opt]#mkdir b1 b2 b3 b4 [root@localhost opt]# ls b1 b2 b3 b4 b5 rh [root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./ #上傳 Password: receiving incremental file list ./ 111.txt 222.txt sent 102 bytes received 221 bytes 23.93 bytes/sec total size is 8 speedup is 0.02 #上傳成功 源服務器上查看: [root@localhost ~]# cd /var/www/html/ [root@localhost html]# ls b1 b2 b3 b4 b5 rh
注意: 上傳前須要把源服務器rsync的配置文件rsyncd.conf中的uid、gid修改成root
將rsync工具與inotify機制相結合,能夠實現觸發式備份(實時同步)——只要原始位置的文檔發生變化,就當即啓動增量備份操做,如圖所示,不然處於靜默等待狀態。這樣,就避免了按固定週期備份時存在的延遲性、週期過密等問題。
正由於inotify通知機制由Linux內核提供,所以要作本機監控,在觸發式備份中應用時更適合上行同步。下面一次介紹其配置過程。
1.調整inotify內核參數
當要監控的目錄、文件數量較多或者變化較頻繁時,建議加大這三個參數的值。能夠直接修改/etc/sysctl.conf的配置文件,將管理隊列、實例數、監控數進行設置。
[root@localhost html]# vim /etc/sysctl.conf # For more information, see sysctl.conf(5) and sysctl.d(5). //添加// fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 1048576 [root@localhost html]# sysctl -p //啓動// fs.inotify.max_queued_events = 16384 fs.inotify.max_user_instances = 1024 fs.inotify.max_user_watches = 1048576
2.安裝inotifi-tools
使用inotify機制還須要安裝inotifi-tools,以便提供inotifywait和inotifywatch輔助工具程序,用來監控和彙總改動狀況。
[root@localhost rs]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/ #解包 [root@localhost opt]# cd inotify-tools-3.14/ [root@localhost inotify-tools-3.14]# yum install gcc gcc-c++ make -y #安裝編譯軟件 [root@localhost inotify-tools-3.14]# ./configure [root@localhost inotify-tools-3.14]#make && make install
3.編寫觸發式同步腳本
root@localhost opt]# vim inotify.sh #!/bin/bash INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.126.138::wwwroot/" $INOTIFY_CMD | while read DIRECTORY EVENT FILE do if [ $(pgrep rsync | wc -l) -le 0 ] ; then $RSYNC_CMD fi done
上述腳本用來檢測本機/var/www/html目錄的變更狀況,一旦有更新觸發rsync同步操做,上傳備份至服務器192.168.126.138的/var/www/html目錄下。
4.驗證
1).在源服務器運行inotifywait -mrq -e modify,create,move,delete /var/www/html/
[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ //靜默等待狀態//
2)打開源服務器的另外一個窗口在/var/www/html目錄下建立新的文件
[root@localhost ~]# cd /var/www/html/ [root@localhost html]# ls [root@localhost html]# echo "this is 111" > 111.txt //寫入文件// [root@localhost html]# echo "this is 222" > 222.txt [root@localhost html]# rm -rf 111.txt //刪除文件//
3)查看源服務器的更新觸發狀態
[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ /var/www/html/ CREATE 111.txt /var/www/html/ MODIFY 111.txt /var/www/html/ CREATE 222.txt /var/www/html/ MODIFY 222.txt /var/www/html/ DELETE 111.txt //監控端已有反饋//
4)查看服務器中的/var/www/html目錄下的變化狀況
[root@localhost html]# ls 111.txt 222.txt [root@localhost html]# ls 222.txt
實驗成功