文章目錄
- 一、rsync同步簡介
- 二、配置rsync源服務器
- 三、rsync遠程同步實驗
- 3.1實驗環境
- 3.2實驗步驟
- 四、rsync實時同步
- 4.1為什么要實時同步
- 4.2關于inotify
- 4.3rsync+inotify實時同步
- 4.4rsync+inotify結合進行實時上行同步(上傳)實驗
- 實驗環境:
- 實驗步驟:
- 實驗注意事項:
一、rsync同步簡介
●一款快速增量備份工具
Remote Sync,遠程同步
支持本地復制,或者與其他SSH、rsync主機同步
●應用場景
“推”
即由客戶端推送資料至服務器,比如個人電腦上傳資料至云盤,
“拉”
即由客戶端拉取服務器上的資料,比如利用個人電腦下載云盤檔案,
●rsync 包括如下的一些特性
1. 能更新整個目錄樹和檔案系統;
2. 有選擇性的保持符號鏈鏈、硬鏈接、檔案屬性、權限、設備以及時間等;
3. 傳輸前執行壓縮,因此非常適用于異地備份、鏡像服務器等應用,
4. 能用rsh、ssh 或直接埠做為傳輸埠;
5. 支持匿名rsync 同步檔案,是理想的鏡像工具;
Rsync可以通過rsh或ssh使用,也能以daemon模式去運行,在以daemon方式運行時Rsync server會打開一個873埠,等待客戶端去連接,
在遠程同步任務中,負責發起rsync同步操作的客戶機稱為發起端,而負責回應來自客機的rsync同步操作的服務器稱為備份源,
二、配置rsync源服務器
2.1rsync同步源簡介
指備份操作的遠程服務器,也稱為備份源

2.2如何配置rsync源
●基本思路
修改rsyncd.conf組態檔、獨立的密碼檔案
啟用rsync的--daemon模式
●應用示例
用戶backuper,允許下行同步
操作的目錄為/var/www/html
●組態檔rsyncd.conf
語法類似于Samba配置
認證配置auth users、secrets file,不加則為匿名(nobody)
●rsync賬號檔案
采用“用戶名:密碼”的記錄格式,每行一個用戶記錄
獨立的賬號資料,不依賴于系統賬號(安全)
●如何啟動和關閉rsync
啟動rsync:rsync --daemon
關閉rsync:kill $(cat /var/run/rsyncd.pid)
2.3如何使用rsync備份工具
rsync命令的用法
rsync [選項] 原始位置 目標位置
常用選項
-a:歸檔模式,遞回并保留物件屬性,等同于-rlptgoD
-v:顯示同步程序的詳細資訊
-z:在傳輸檔案時進行壓縮
-H:保留硬連接檔案
-A:保留ACL屬性資訊
--delete:洗掉目標位置有而原始位置沒有的檔案
--checksum:根據物件的校驗和來決定是否跳過檔案
三、rsync遠程同步實驗
3.1實驗環境

3.2實驗步驟
源端服務器配置:
1.確認rsync是否已經安裝(Centos 7.6自帶安裝)
[root@localhost ~]# rpm -q rsync
rsync-3.1.2-4.el7.x86_64
2.修改組態檔
[root@localhost ~]# vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes #禁錮家目錄
address = 14.0.0.10 #提供同步服務的地址
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 14.0.0.0/24 #允許同步的網段
[wwwroot]
path = /var/www/html #同步的目錄
comment = www.test.com
read only = yes #只讀模式開啟
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 #這些結尾的檔案同步時不再壓縮
auth users = backuper
secrets file = /etc/rsyncd_users.db #用戶密碼存放在secrets file中
3.創建backuper用戶的密碼檔案
[root@localhost ~]# touch /etc/rsyncd_users.db
[root@localhost ~]# vim /etc/rsyncd_users.db
backuper:abc123
4.服務端的密碼檔案要600權限
[root@localhost html]# chmod 600 /etc/rsyncd_users.db
5.啟動服務
[root@localhost html]# rsync --daemon
6.查看狀態
[root@localhost html]# netstat -ntap | grep rsync
客戶端驗證同步功能:
需要輸密碼的兩種方式:
方式一:
[root@localhost opt]# rsync -avz backuper@14.0.0.10::wwwroot /opt/
Password:
receiving incremental file list
./
index.html
index1.html
index2.html
方式二:
[root@localhost opt]# rsync -avz rsync://backuper@14.0.0.10/wwwroot /opt/
Password:
receiving incremental file list
./
index.html
index1.html
index2.html
免密方式同步檔案:
要先在客戶端本地創建密碼檔案/etc/server.pass
[root@localhost etc]# vim server.pass
[root@localhost etc]# ll | grep server.pass
-rw-r--r--. 1 root root 7 9月 10 16:43 server.pass
[root@localhost etc]# chmod 600 server.pass
[root@localhost etc]# rsync -az --delete --password-file=/etc/server.pass backuper@14.0.0.10::wwwroot /opt/
[root@localhost opt]# ls
index1.html index2.html index.html
四、rsync實時同步
4.1為什么要實時同步
●定期同步的不足
執行備份的時間固定,延遲明顯,實時性差
當同步源長期不變化時,密集的定期任務是不必要的
●實時同步的優點
一旦同步源出現變化,立即啟動備份
只要同步源無變化,則不執行備份
4.2關于inotify
●Linux內核的inotify機制
從版本2.6.13開始提供
可以監控檔案系統的變動情況,并做出通知回應
輔助軟體:inotify-tools

4.3rsync+inotify實時同步
●調整inotify內核引數
max_queue_events:監控事件佇列大小
max_user_instances:最多監控實體數
max_user_watches:每個實體最多監控檔案數
監控數要大于監控目標的總檔案數
●安裝inotify-tools輔助工具
inotifywait:用于持續監控,實時輸出結果
inotifywatch:用于短期監控,任務完成后再出結果
示例:inotifywait -mrq -e modify,create,move,delete /opt/
-m:持續進行監控
-r:遞回監控所有子物件
-q:簡化輸出資訊
-e:指定要監控哪些事件型別
modify:修改;create:創建;move:移動;delete:洗掉
●通過inotifywait觸發rsync同步操作
使用while、read持續獲取監控結果
根據結果可以作進一步判斷,決定執行何種操作
4.4rsync+inotify結合進行實時上行同步(上傳)實驗
實驗環境:

基于前面rsync同步實驗繼續
源端地址:14.0.0.10;安裝軟體:rsync
客戶端地址:14.0.0.20;安裝軟體:rsync、inotify-tools
實驗步驟:
1.源端組態檔中將只讀選項更改為no;更改組態檔后需要重啟服務,詳見下面的實驗注意事項
[root@localhost ~]# vim /etc/rsyncd.conf
read only = no
2.客戶端更改內核引數,指定佇列大小,最多監控實體數,每個實體最多監控檔案數
[root@localhost opt]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost opt]# sysctl -p
3.安裝inotify-tools工具
[root@slave ~]# tar zxf inotify-tools-3.14.tar.gz
[root@slave ~]# cd inotify-tools-3.14/
[root@slave inotify-tools-3.14]# yum install gcc gcc-c++ -y
[root@slave inotify-tools-3.14]# ./configure
[root@slave inotify-tools-3.14]# make && make install
4.測驗inotifywait監控命令是否正常使用
[root@localhost ~]# inotifywait -mrq -e modify,create,move,delete /opt/(輸完這條命令后再開一個終端在/opt目錄下進行寫入或者讀寫等操作)
/opt/ CREATE c.txt
/opt/ CREATE d.txt
5.客戶端上撰寫腳本,將inotify監控和rsync遠程同步結合起來
[root@localhost ~]# vim inotify.sh
#!/bin/bash
INOTIFY="inotifywait -mrq -e modify,create,attrib,move,delete /opt"
RSYNC="rsync -azH --delete --password-file=/etc/server.pass /opt/ backuper@14.0.0.10::wwwroot/"
$INOTIFY | while read DIRECTORY EVENT FILE #逐條讀取監控記錄
do
if [ $(pgrep rsync | wc -l) -le 0 ];then
$RSYNC
fi
done
注意:將源端/var/www/html/目錄設定權限為777,否則遠程同步無法寫入
6.運行腳本,在客戶端/opt目錄下創建檔案,查看源端/var/www/html目錄是否同步到
#在客戶端/opt目錄下創建檔案b.txt,原來有a.txt
[root@localhost opt]# touch b.txt
#查看源端/var/www/html目錄,新建的和原來就存在的檔案都成功同步,且屬主屬組均為nobody用戶
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ll
總用量 4
-rw-------. 1 nobody nobody 6 9月 10 21:07 a.txt
-rw-------. 1 nobody nobody 0 9月 10 21:07 b.txt
實驗注意事項:
1.注意:kill pid號和kill -9 pid號的區別:后者不會洗掉pid檔案,導致服務起不來
[root@localhost html]# cd /var/run/
[root@localhost run]# cat rsyncd.pid
14293
[root@localhost run]# kill 14293
[root@localhost run]# ll | grep rsyncd.pid
[root@localhost run]# rsync --daemon
[root@localhost run]# ll | grep rsyncd.pid
-rw-r--r--. 1 root root 6 9月 10 16:39 rsyncd.pid
[root@localhost run]# cat rsyncd.pid
14957
[root@localhost run]# kill -9 14957 #kill -9 不會洗掉pid檔案
[root@localhost run]# ll | grep rsyncd.pid
-rw-r--r--. 1 root root 6 9月 10 16:39 rsyncd.pid
[root@localhost run]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists
2.使用如下命令進行上行同步(上傳)時,本地目錄最后要加上/,否則會將目錄同步到對方目標檔案夾下,而不僅僅是同步本地目錄下的檔案
例如:
#在客戶端輸入以下命令
rsync -azH --delete --password-file=/etc/server.pass /var/www/html backuper@14.0.0.10::wwwroot/
#源端查看/var/www/html同步結果,會發現是整個html目錄一起同步過來了
[root@localhost html]# ls
html
[root@localhost html]# cd html/
[root@localhost html]# ls
index1.html index.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/20365.html
標籤:其他
