一、RSYNC簡介
rsync是linux系統下的數據鏡像備份工具。使用快速增量備份工具Remote Sync可以遠程同步,支持本地復制,或者與其他SSH、rsync主機同步。
二、rsync特性
rsync支持很多特性:
- 可以鏡像保存整個目錄樹和文件系統
- 可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等
- 無須特殊權限即可安裝
- 快速:第一次同步時rsync會復制全部內容,但在下一次只傳輸修改過的文件。rsync在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬
- 安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接
- 支持匿名傳輸,以方便進行網站鏡像
三、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@192.168.100.20:/DEST
//當遇到要修改端口的時候,我們可以:
rsync -avz /SRC -e "ssh -p2222" root@192.168.100.20:/DEST
#//修改了ssh 協議的端口為2222,默認是22
#rsync常用選項:
-a, --archive #歸檔,表示以遞歸方式傳輸文件,并保持所有屬性
-v, --verbose #顯示打印的信息,比如文件列表、文件數量等
-q, --quiet #不顯示打印信息
-r, --recursive #遞歸,傳輸目錄時必須加
-p, --perms #保持原有的權限屬性
-z, --compress #在傳輸時壓縮,節省帶寬,加快傳輸速度
--delete #表示刪除目標服務中源服務器沒有的文件,在源服務器上做的刪除操作也會在目標服務器上同步
四、rsync的使用
#sync的命令格式常用的有以下三種:
rsync [OPTION]... SRC DEST #在本地實現操作,[OPTION]選項,[SRC]路徑
rsync [OPTION]... SRC [USER@]HOST:DEST #將源服務器文件傳輸到目標服務器
rsync [OPTION]... [USER@]HOST:SRC DEST #將目標服務器文件傳輸到源服務器
對應于以上三種命令格式,rsync有三種不同的工作模式:
1)拷貝本地文件。當SRC和DES路徑信息都不包含有單個冒號":"分隔符時就啟動這種工作模式。如:
[root@server tmp]# touch a [root@server tmp]# rsync -a a afile #復制a文件為afile文件 [root@server tmp]# ll total 0 -rw-r--r--. 1 root root 0 Sep 20 23:53 a -rw-r--r--. 1 root root 0 Sep 20 23:53 afile
2)使用一個遠程shell程序(如rsh、ssh)來實現將本地機器的內容拷貝到遠程機器。當DST路徑地址包
含單個冒號":"分隔符時啟動該模式。如:[root@server tmp]# rsync -avz a root@192.168.100.20:/tmp/b sending incremental file list a sent 80 bytes received 35 bytes 25.56 bytes/sec total size is 0 speedup is 0.00#登錄192.168.100.20主機 [root@cy tmp]# ll total 0 -rw-r--r--. 1 root root 0 Sep 20 2022 b
3)使用一個遠程shell程序(如rsh、ssh)來實現將遠程機器的內容拷貝到本地機器。當SRC地址路徑 \包含單個冒號":"分隔符時啟動該模式。如:
#192.168.100.20主機 [root@client tmp]# touch file1 [root@client tmp]# ll total 0 -rw-r--r--. 1 root root 0 Sep 20 2022 b -rw-r--r--. 1 root root 0 Sep 20 15:57 file1#在192.168.100.10主機: [root@server tmp]# rsync -avz root@192.168.100.20:/tmp/file1 /tmp/ receiving incremental file list file1sent 43 bytes received 84 bytes 36.29 bytes/sec total size is 0 speedup is 0.00 [root@localhost ~]# ls anaconda-ks.cfg a.sh nfs.sh yum.repos.d [root@server tmp]# ll total 0 -rw-r--r--. 1 root root 0 Sep 20 23:53 a -rw-r--r--. 1 root root 0 Sep 20 23:53 afile -rw-r--r--. 1 root root 0 Sep 20 15:57 file1
五、rsync+inotify數據實時同步實驗
**背景:**隨著應用系統規模的不斷擴大,對數據的安全性和可靠性也提出的更好的要求,rsync在高端業務系統中也逐漸暴露出了很多不足,首先,rsync同步數據時,需要掃描所有文件后進行比對,進行差量傳輸。如果文件數量達到了百萬甚至千萬量級,掃描所有文件將是非常耗時的。
而且正在發生變化的往往是其中很少的一部分,這是非常低效的方式。其次,rsync不能實時的去監測、同步數據,雖然它可以通過linux守護進程的方式進行觸發同步,但是兩次觸發動作一定會有時間差,這樣就導致了服務端和客戶端數據可能出現不一致,無法在應用故障時完全的恢復數據。基于以上原因,rsync+inotify組合出現了!**對比:**rsync與傳統的cp、tar備份方式相比,rsync具有安全性高、備份迅速、支持增量備份等優點,通過rsync可以解決對實時性要求不高的數據備份需求,例如定期的備份文件服務器數據到遠端服務器,對本地磁盤定期做數據鏡像等。
**Inotify:**inotify是一種強大的、細粒度的、異步的文件系統事件監控機制,linux內核從2.6.13起,加入了inotify支持,通過inotify可以監控文件系統中添加、刪除,修改、移動等各種細微事件,利用這個內核接口,第三方軟件就可以監控文件系統下文件的各種變化情況,而inotify-tools就是這樣的一個第三方軟件。
在前面有講到,rsync可以實現觸發式的文件同步,但是通過crontab守護進程方式進行觸發,同步的數據和實際數據會有差異,而inotify可以監控文件系統的各種變化,當文件有任何變動時,就觸發rsync同步,這樣剛好解決了同步數據的實時性問題。
環境:
服務器類型 ip地址 應用 操作系統
源服務器 192.168.100.10 rsync inotify-tools腳本 centos7
目標服務器 192.168.100.20 rsync centos7
需求:
把源服務器上/root/etc目錄實時同步到目標服務器的/tmp下
操作:
一、手動同步
-
配置時鐘同步,將源服務器當作時鐘服務器
源服務器:#關閉防火墻與SELINUX [root@server ~]# systemctl stop firewalld [root@server ~]# systemctl disable firewalld [root@server ~]# setenforce 0[root@server ~]# yum -y install chrony #安裝時鐘同步軟件包 [root@server ~]# vim /etc/chrony.conf #在/etc/chrony.conf文件中 Serve time even if not synchronized to a time source. local stratum 10[root@server ~]# systemctl restart chronyd [root@server ~]# systemctl enable chronyd [root@server ~]# hwclock -w
目標服務器:
#關閉防火墻與SELINUX [root@client~]# systemctl stop firewalld [root@client~]# systemctl disable firewalld [root@client~]# setenforce 0 [root@client tmp]# yum -y install chrony #安裝時鐘同步軟件包 [root@client tmp]# vim /etc/chrony.conf #在/etc/chrony.conf文件中 server 192.168.100.10 iburst [root@client tmp]# systemctl restart chronyd [root@client tmp]# systemctl enable chronyd [root@client tmp]# hwclock -w [root@client tmp]# chronyc sources #查看時鐘同步是否啟動
-
配置遠程yum倉庫
[root@server ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo [root@server ~]# yum -y install epel-release
-
安裝rsync服務端軟件,默認server中已經安裝了rsync,如果未安裝,則執行下面命令進行安裝
[root@client~]# yum -y install rsync
-
在目標主機中設置rsyncd.conf配置文件
[root@client~]# vim /etc/rsyncd.conf#在/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 = /tmp/ # rsync服務端數據存放路徑,客戶端的數據將同步至此目錄 comment = sync etc from client 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.100.10 # 允許進行數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開 hosts deny = 192.168.1.1 # 禁止數據同步的客戶端IP地址,可以設置多個,用英文狀態下逗號隔開
-
在目標主機中創建用戶認證文件
[root@client tmp]# echo 'admin:redhat' > /etc/rsync.pass [root@client tmp]# cat /etc/rsync.pass admin:redhat
-
在目標主機中設置文件權限
[root@client tmp]# chmod 600 /etc/rsync* #只給所有者有讀寫的權限 [root@client tmp]# ll /etc/rsync*
?
-
啟動rsync服務并設置開機自啟動
[root@client ~]# rsync --daemon #啟動rsync服務 [root@client ~]# echo 'rsync --daemon --config=/etc/rsyncd.conf' >> /etc/rc.d/rc.local [root@client ~]# netstat -tulnp | grep 873 #查看873端口是否啟用
-
在源服務器上
#關閉防火墻與SELINUX [root@server ~]# systemctl stop firewalld [root@server ~]# systemctl disable firewalld [root@server ~]# setenforce 0
-
在源服務器上安裝rsync服務端軟件,只需要安裝,不要啟動,不需要配置
[root@server ~]# yum -y install rsync
-
創建認證密碼文件
[root@server ~]# echo 'redhat' > /etc/rsync.pass [root@server ~]# cat /etc/rsync.pass
?
-
設置文件權限,只設置文件所有者具有讀取、寫入權限即可
[root@server ~]# chmod 600 /etc/rsync.pass
-
在源服務器上創建測試目錄,然后在源服務器運行以下命令
[root@server ~]# mkdir -pv /root/etc/test [root@server ~]# rsync -avH --port 873 --progress --delete /root/etc/ admin@192.168.100.20::etc_from_client --password-file=/etc/rsync.pass
-
運行完成后,在目標服務器上查看,在/tmp目錄下有test目錄,說明數據同步成功
[root@client ~]# cd /tmp/ [root@client tmp]# ls
?
二、安裝inotify-tools工具,實時觸發rsync進行同步
-
配置遠程yum倉庫
[root@server ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo [root@server ~]# yum -y install epel-release
-
查看服務器內核是否支持inotify
[root@server ~]# ll /proc/sys/fs/inotify #如果有這三個max開頭的文件則表示服務器內核支持inotify
-
安裝inotify-tools
[root@server ~]# yum -y install make gcc gcc-c++ [root@server ~]# yum -y install inotify-tools
-
寫同步腳本,讓腳本自動去檢測我們制定的目錄下
#文件發生的變化,然后再執行rsync的命令把它同步到源服務器端去 [root@server ~]# mkdir /maoqi [root@server ~]# touch /maoqi/inotify.sh [root@server ~]# chmod +x /maoqi/inotify.sh [root@server ~]# vim /maoqi/inotify.sh host=192.168.100.20 # 目標服務器的ip(備份服務器) src=/root/etc # 在源服務器上所要監控的備份目錄(此處可以自定義,但是要保證存在) 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;dorsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$desecho "${files} was rsynced" >>/tmp/rsync.log 2>&1 done
-
啟動腳本
[root@server ~]# nohup bash /maoqi/inotify.sh & [root@server ~]# ps -ef | grep inotify #查看腳本是否運行
-
在源服務器上生成一個新文件
[root@localhost ~]# touch /root/etc/maoqi123
-
查看inotify生成的日志
[root@server ~]# tail /tmp/rsync.log #從日志上可以看到,生成了一個maoqi123文件,并且添加了內容到其里面
?
-
源服務器中設置開機自啟
#設置腳本開機啟動: [root@server ~]# chmod +x /etc/rc.d/rc.local [root@server ~]# ll /etc/rc.d/rc.local -rwxr-xr-x. 1 root root 474 Mar 24 2020 /etc/rc.d/rc.local
[root@server ~]# echo 'nohup /bin/bash /maoqi/inotify.sh &' >> /etc/rc.d/rc.local #在/etc/rc.d/rc.local文件中設置開機自啟 [root@server ~]# tail /etc/rc.d/rc.local
或者直接進入/etc/rc.d/rc.local 文件內進行修改
#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 /chenyu/inotify.sh & #在末尾寫入這一行
-
到目標服務器上去查看是否把新生成的文件自動傳上去了
由此可見,已將源服務器的/root/etc目錄整個同步到了目標服務器,且新增的test文件也自動同步了
?