rsync


1. rsync簡介

rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,或者與其他SSH、rsync主機同步。

2. rsync特性

rsync支持很多特性:

  • 可以鏡像保存整個目錄樹和文件系統
  • 可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等
  • 無須特殊權限即可安裝
  • 快速:第一次同步時rsync會復制全部內容,但在下一次只傳輸修改過的文件。rsync在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬
  • 安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接
  • 支持匿名傳輸,以方便進行網站鏡象

3. rsync的ssh認證協議

rsync命令來同步系統文件之前要先登錄remote主機認證,認證過程中用到的協議有2種:

  • ssh協議
  • rsync協議

rsync server端不用啟動rsync的daemon進程,只要獲取remote host的用戶名和密碼就可以直接rsync同步文件 rsync server端因為不用啟動daemon進程,所以也不用配置文件/etc/rsyncd.conf ssh認證協議跟scp的原理是一樣的,如果在同步過程中不想輸入密碼就用ssh-keygen -t rsa打通通道

//這種方式默認是省略了 -e ssh 的,與下面等價: rsync -avz /SRC -e ssh root@172.16.12.129:/DEST -a //文件宿主變化,時間戳不變 -z //壓縮數據傳輸 //當遇到要修改端口的時候,我們可以: rsync -avz /SRC -e "ssh -p2222" root@172.16.12.129:/DEST //修改了ssh 協議的端口,默認是22

4. rsync命令

//Rsync的命令格式常用的有以下三種: rsync [OPTION]... SRC DEST rsync [OPTION]... SRC [USER@]HOST:DEST rsync [OPTION]... [USER@]HOST:SRC DEST    //對應於以上三種命令格式,rsync有三種不同的工作模式: 1)拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啟動這種工作模式。如: [root@dest-liuzhenchao ~]# ls anaconda-ks.cfg [root@dest-liuzhenchao ~]# rsync -a anaconda-ks.cfg anaconda-ks-123.cfg [root@dest-liuzhenchao ~]# ll 總用量 8 -rw-------. 1 root root 1453 4月 22 23:54 anaconda-ks-123.cfg -rw-------. 1 root root 1453 4月 22 23:54 anaconda-ks.cfg [root@dest-liuzhenchao ~]# ll -i 總用量 8 33877713 -rw-------. 1 root root 1453 4月 22 23:54 anaconda-ks-123.cfg 33574978 -rw-------. 1 root root 1453 4月 22 23:54 anaconda-ks.cfg 2)使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包 \ 含單個冒號":"分隔符時啟動該模式。如: [root@dest-liuzhenchao ~]# rsync -avz anaconda-ks.cfg root@192.168.56.20:/root/text sending incremental file list anaconda-ks.cfg sent 856 bytes received 35 bytes 137.08 bytes/sec total size is 1,453 speedup is 1.63 [root@source-liuzhenchao text]# ls anaconda-ks.cfg 3)使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑 \ 包含單個冒號":"分隔符時啟動該模式。如: [root@source-liuzhenchao text]# rsync -avz abc root@192.168.56.23:/root sending incremental file list abc sent 98 bytes received 35 bytes 17.73 bytes/sec total size is 12 speedup is 0.09 [root@dest-liuzhenchao ~]# ls abc anaconda-ks-123.cfg anaconda-ks.cfg //rsync常用選項: -a, --archive //歸檔 -v, --verbose //啰嗦模式 -q, --quiet //靜默模式 -r, --recursive //遞歸 -p, --perms //保持原有的權限屬性 -z, --compress //在傳輸時壓縮,節省帶寬,加快傳輸速度 --delete //在源服務器上做的刪除操作也會在目標服務器上同步

5. rsync+inotify

rsync與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支持增量備份等優點,通過rsync可以解決對實時性要求不高的數據備份需求,例如定期的備份文件服務器數據到遠端服務器,對本地磁盤定期做數據鏡像等。 隨着應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了很多不足,首先,rsync同步數據時,需要掃描所有文件后進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基於以上原因,rsync+inotify組合出現了!

Inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了Inotify支持,通過Inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools就是這樣的一個第三方軟件。 在前面有講到,rsync可以實現觸發式的文件同步,但是通過crontab守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync同步,這樣剛好解決了同步數據的實時性問題。

環境說明:

服務器類型 IP地址 應用 操作系統
源服務器 192.168.56.20 rsync
inotify-tools
腳本
centos7/redhat7
目標服務器 192.168.56.23 rsync centos7/redhat7

需求:

  • 把源服務器上/etc目錄實時同步到目標服務器的/liuzhenchao/下

在目標服務器上做以下操作:

//關閉防火牆與SELINUX [root@dest-liuzhenchao ~]# systemctl stop firewalld [root@dest-liuzhenchao ~]# systemctl disable firewalld [root@dest-liuzhenchao ~]# getenforce Disabled //安裝rsync服務端軟件 [root@dest-liuzhenchao ~]# yum -y install rsync 已加載插件:product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. ........ //設置rsyncd.conf配置文件 [root@dest-liuzhenchao ~]# cat >> /etc/rsyncd.conf <<EOF log file = /var/log/rsyncd.log pidfile = /var/run/rsyncd.pid //pid文件的存放位置 lock file = /var/run/rsync.lock //支持max connections參數的鎖文件 secrets file = /etc/rsync.pass //用戶認證配置文件,里面保存用戶名稱和密碼,必須手動創建這個文件 [etc_from_source] //自定義同步名稱 path = /liuzhenchao/ //rsync服務端數據存放路徑,客戶端的數據將同步至此目錄 comment = sync etc from source uid = root //設置rsync運行權限為root gid = root //設置rsync運行權限為root port = 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.56.20 //允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 hosts deny = 192.168.1.1 //禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 > EOF //創建同步目錄 [root@dest-liuzhenchao ~]# mkdir /liuzhenchao //創建用戶認證文件 [root@dest-liuzhenchao ~]# echo 'admin:123456' > /etc/rsync.pass [root@dest-liuzhenchao ~]# cat /etc/rsync.pass admin:123456 //設置文件權限 [root@dest-liuzhenchao ~]# chmod 600 /etc/rsync* [root@dest-liuzhenchao ~]# ll /etc/rsync* -rw------- 1 root root 853 4月 25 23:26 /etc/rsyncd.conf -rw------- 1 root root 13 4月 25 23:27 /etc/rsync.pass //啟動rsync服務並設置開機自啟動 [root@dest-liuzhenchao ~]# systemctl start rsyncd [root@dest-liuzhenchao ~]# systemctl enable rsyncd Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. [root@dest-liuzhenchao ~]# ss -antl |grep 873 LISTEN 0 5 *:873 *:* LISTEN 0 5 :::873 :::*

在源服務器上做以下操作:

//關閉防火牆與SELINUX [root@source-liuzhenchao ~]# systemctl stop firewalld [root@source-liuzhenchao ~]# systemctl disable firewalld [root@source-liuzhenchao ~]# getenforce Disabled //安裝rsync服務端軟件,只需要安裝,不要啟動,不需要配置 [root@source-liuzhenchao yum.repos.d]# yum -y install rsync 已加載插件:fastestmirror, product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager to register. ....... //創建認證密碼文件 [root@source-liuzhenchao ~]# echo '123456' > /etc/rsync.pass [root@source-liuzhenchao ~]# cat /etc/rsync.pass 123456 //設置文件權限,只設置文件所有者具有讀取、寫入權限即可 [root@source-liuzhenchao ~]# chmod 600 /etc/rsync.pass [root@source-liuzhenchao ~]# ll /etc/rsync.pass -rw------- 1 root root 7 4月 25 15:33 /etc/rsync.pass //在源服務器上創建測試目錄,然后在源服務器運行以下命令 [root@source-liuzhenchao ~]# mkdir -pv /root/etc/test mkdir: 已創建目錄 "/root/etc" mkdir: 已創建目錄 "/root/etc/test" [root@source-liuzhenchao ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.56.23::etc_from_source --password-file=/etc/rsync.pass sending incremental file list ./ test/ sent 77 bytes received 27 bytes 208.00 bytes/sec total size is 0 speedup is 0.00 //運行完成后,在目標服務器上查看,在/liuzhenchao目錄下有test目錄,說明數據同步成功 [root@dest-liuzhenchao ~]# cd /liuzhenchao [root@dest-liuzhenchao liuzhenchao]# ls test //安裝inotify-tools工具,實時觸發rsync進行同步 //查看服務器內核是否支持inotify [root@source-liuzhenchao ~]# ll /proc/sys/fs/inotify/ 總用量 0 -rw-r--r-- 1 root root 0 4月 25 15:53 max_queued_events -rw-r--r-- 1 root root 0 4月 25 15:53 max_user_instances -rw-r--r-- 1 root root 0 4月 25 15:53 max_user_watches //如果有這三個max開頭的文件則表示服務器內核支持inotify //安裝inotify-tools [root@source-liuzhenchao ~]# yum -y install make gcc gcc-c++ 安裝過程略.... [root@source-liuzhenchao ~]# yum -y install inotify-tools 安裝過程略.... //寫同步腳本,此步乃最最重要的一步,請慎之又慎。讓腳本自動去檢測我們制定的目錄下 \ //文件發生的變化,然后再執行rsync的命令把它同步到我們的服務器端去 [root@source-liuzhenchao ~]# mkdir /scripts [root@source-liuzhenchao ~]# touch /scripts/inotify.sh [root@source-liuzhenchao ~]# chmod 755 /scripts/inotify.sh [root@source-liuzhenchao ~]# ll /scripts/ |grep inotify.sh -rwxr-xr-x 1 root root 0 4月 25 15:56 inotify.sh [root@source-liuzhenchao ~]# vim /scripts/inotify.sh host=192.168.56.23 //目標服務器的ip(備份服務器) src=/etc //在源服務器上所要監控的備份目錄(此處可以自定義,但是要保證存在) des=etc_from_source //自定義的模塊名,需要與目標服務器上定義的同步名稱一致 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 //啟動腳本 [root@source-liuzhenchao ~]# nohup bash /scripts/inotify.sh & [1] 1876 [root@source-liuzhenchao ~]# nohup: 忽略輸入並把輸出追加到"nohup.out" [root@source-liuzhenchao ~]# ps -ef|grep inotify root 1876 1658 0 16:05 pts/0 00:00:00 bash /scripts/inotify.sh root 1877 1876 0 16:05 pts/0 00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /etc root 1878 1876 0 16:05 pts/0 00:00:00 bash /scripts/inotify.sh root 1880 1658 0 16:06 pts/0 00:00:00 grep --color=auto inotify

設置腳本開機自動啟動:

[root@source-liuzhenchao ~]# chmod +x /etc/rc.d/rc.local [root@source-liuzhenchao ~]# ll /etc/rc.d/rc.local -rwxr-xr-x. 1 root root 473 2月 20 2018 /etc/rc.d/rc.local [root@source-liuzhenchao ~]# echo 'nohup /bin/bash /scripts/inotify.sh' >> /etc/rc.d/rc.local [root@source-liuzhenchao ~]# tail /etc/rc.d/rc.local # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. touch /var/lock/subsys/local nohup /bin/bash /scripts/inotify.sh

到目標服務器上去查看是否把新生成的文件自動傳上去了:

[root@dest-liuzhenchao liuzhenchao]# ls etc test //由此可見,已將源服務器的/etc目錄整個同步到了目標服務器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM