文章目錄
- 一、概念
- 二、配置rsync 源服務器
- 2.1 、rsync 同步源
- 2.2 、配置rsync源
- 2.3、命令用法
- 2.4、配置源表示方法
- 三、實驗
- 3.1、實驗環境
- 3.2、實驗程序
- 3.2.1、修改組態檔
- 3.2.2、創建于用戶密碼檔案
- 3.2.3、啟動rsync 服務
- 3.2.4、發起端配置
- 3.2.5、設定周期性計劃任務
- 3.2.6、rsync + inotify
- 3.2.7、通過inotifywai t觸發rsync 同步操作
一、概念
rsync,全稱為:Remote Sync(遠程同步),是一款開源的快速增量備份工具,可以在不同主機之間鏡像同步整個目錄樹
支持本地復制,增量備份、保持連接和權限,或者與其他SSH,rsync主機同步
采用優化的同步演算法,傳輸前執行壓縮,因此非常適用于異地備份、鏡像服務器等應用
二、配置rsync 源服務器
2.1 、rsync 同步源
指備份操作的遠程服務器,也稱為備份源

2.2 、配置rsync源
基本思路
建立rsyncd.conf組態檔、獨立的賬號檔案
啟用rsync——daemon模式
應用示例
用戶gsybk,允許下行同步
操作的目錄為/var/www/html
組態檔
需要手動建立,語法類似samba配置
認證配置auth users、secrets file ,不加則默認為匿名
rsync賬號檔案
采用“用戶名:密碼”的格式記錄,每行一個用戶記錄
獨立的賬號資料,不依賴系統賬號
啟動rsync服務
通過–daemon獨自提供服務
執行 kill $(cat /var/run/rsyncd.pid)關閉rsync服務
2.3、命令用法
rsync [選項] 原始位置 目標位置
●-a:歸檔模式,遞回并保留物件屬性,等同于 -rlptgoD
●-v:顯示同步程序的詳細(verbose)資訊
●-z:在傳輸檔案時進行壓縮(compress)
●-H:保留硬連接檔案
●-A:保留ACL屬性資訊
●–delete:洗掉目標位置有而原始位置沒有的檔案
●–checksum:根據物件的校驗和來決定是否跳過檔案
2.4、配置源表示方法
格式1:用戶名@主機地址::共享模塊名
格式2:rsync://用戶名@主機地址/共享模塊名
三、實驗
3.1、實驗環境
VMware軟體 兩臺centos7.6版本虛擬機
一臺源端 ip地址為20.0.0.21 需要安裝服務 rsync,httpd
一臺發起端 ip地址為20.0.0.22 需要安裝服務 rsync,inotify-tools
均為防火墻,內核防護關閉
3.2、實驗程序
3.2.1、修改組態檔
vim /etc/rsyncd.conf
uid = nobody
gid = nobody
use chroot = yes
address = 20.0.0.21
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 20.0.0.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.51xit.com
read only = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
auth users = backuper
secrets file = /etc/rsyncd_users.db
3.2.2、創建于用戶密碼檔案
vi /etc/rsyncd_users.db
backuper:abc123
chmod 600 /etc/rsyncd_users.db
3.2.3、啟動rsync 服務
rsync --daemon
netstat -anpt |grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 20917/rsync
在 /var/www/html目錄中,新建檔案
echo “this is 110” > /var/www/html/1.txt
3.2.4、發起端配置
測驗有密碼拉取檔案
方法1:
rsync -avz backuper@20.0.0.21::wwwroot /opt
方法2:
rsync -avz rsync://backuper@20.0.0.21/wwwroot /opt
查看拉取的檔案
cat /opt/1.txt
this is 110
rsync源的免密互動處理
在源端上:
cd /opt
rm -rf 1.txt
vi /etc/server.pass
abc123
chmod 600 /etc/server.pass
rsync -az --delete --password-file=/etc/server.pass backuper@20.0.0.21::wwwroot /opt/
3.2.5、設定周期性計劃任務
mkdir /webbak
crontab -e
30 22 * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.pass backuper@20.0.0.10::wwwroot /webbak
systemctl restart crond
systemctl enable crond
3.2.6、rsync + inotify
修改inotify內核引數
源端上:
vi /etc/sysctl.conf
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
fs.inotify.max_queued_events = 32768
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
安裝http服務
yum -y install httpd
systemctl start httpd
systemctl enable httpd
安裝inotify-tools輔助工具
yum -y install gcc gcc-c++
上傳inotify-tools壓縮包到/opt目錄下
cd /opt
tar zxvf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14/
./configure
make && make install
cd ~
mkdir /web
inotifywait -mrq -e modify,create,move,delete /web
新開啟一個發起端(1)的終端
cd /web
touch 1.txt
回傳在發起端查看
/web CREATE 1.txt 會看到有此資訊更新
3.2.7、通過inotifywai t觸發rsync 同步操作
cd /opt
vi inotify.sh
#!/bin/bash
INOTIFY_CMD=“inotifywait -mrq -e create,delete,move,modify,attrib /web”
RSYNC_CMD=“rsync -azH --delete --password-file=/etc/server.pass /web backuper@20.0.0.21::wwwroot”
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
chmod +x inotify.sh
在接收端上:
vi /etc/rsyncd.conf
read only = no
//設定只讀模式
netstat -anpt | grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 20917/rsync
kill -9 20917
netstat -anpt | grep rsync
//已經關閉服務
rsync --daemon
//重啟服務,報錯
failed to create pid file /var/run/rsyncd.pid: File exists
cd /run/
rm -rf rsyncd.pid
rsync --daemon
//啟動成功
netstat -anpt | grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 26021/rsync
注意:發起端的/web和接收端的/var/www/html權限設定為777
接收端:chmod 777 /var/www/html/
發起端:chmod 777 /web
在發起端上:
./inotify.sh
新開啟終端發起端(1):
cd /web
echo “this is t” >text.txt
ll
-rw-r–r-- 1 root root 13 Oct 23 03:12 text.txt
在接收端上查看:
cd /var/www/html/web
ll
-rw-r–r-- 1 root root 13 Oct 23 15:08 text.txt
在發起端(1)上查看:
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
rsync: chgrp “/web” (in wwwroot) failed: Operation not permitted (1)
rsync: chgrp “/web/.text.txt.veXjy3” (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
測驗,發現可以將發起端創建的資料檔案同步到接收端,但是發起端會有報錯
解決方法:將接收的uid和gid修改為root
vi /etc/rsyncd.conf
uid = root
gid = root
cd /run
rm -rf rsyncd.pid
rsync --daemon
netstat -anpt | grep rsync
tcp 0 0 20.0.0.21:873 0.0.0.0:* LISTEN 20987/rsync
回傳發起端:
重新運行腳本
./inotify.sh &
//可以放到后臺運行
重新打開發起端(1):
cd /web
touch aa
到接收端:
cd /var/www/html/web
ll
aa
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/196349.html
標籤:java
下一篇:高可用負載均衡(hproxy)
