rsync遠端同步(配置rsync下行同步、rsync+inotify實時同步)
一、rsync簡介
rsync(Remote Sync,遠端同步)
- 一款開源的快速備份工具
- 支援本地複制
- 也可以在不同主機(例如:其他SSH、rsync主機)之間鏡像同步整個目錄樹,支援增量備份,并保持鉗接和權限。
- 采用優化的同步算法,傳輸前執行壓縮,,是以非常适用于異地備份、鏡像伺服器等應用。
二、rsync同步源
在遠端同步任務中,負責發起rsync司步操作的客戶機稱為發起端,而負責響應來自客戶機的rsync同步操作的伺服器稱為同步源(備份源)。在同步過程中,同步源負責提供檔案的原始位置,發起端應對該位置具有讀取權限。
例:
A伺服器同步B伺服器的資料,B伺服器就是備份源
反過來,B伺服器同步A伺服器的資料,那麼A伺服器就是備份源

三、配置rsync源
1、基本思路
- 建立rsyncd.conf配置檔案、獨立的rsunc賬号檔案
- 配置檔案rsyncd.conf
- 需手動配置,文法類似于Samba配置
- 認證配置auth users、secrets file,不加則為匿名
- rsync賬号檔案
- 采用“使用者名:密碼”的格式記錄,每行一個使用者記錄
- 獨立的賬号資料,不依賴系統賬号
- 啟用rsync服務
- 通過 --daemon獨自提供服務:rsync --daemon
- 可以通過執行kill $(cat /var/run/rsyncd.pid)關閉服務
2、rsync指令
#指令的用法
rsync [選項] 原始位置 目标位置
#----------常用選項--------------------------
-r:遞歸模式,包含目錄及子目錄中的所有檔案。
-l:對于符号連結檔案仍然複制為符号連結檔案。
-v:顯示同步過程的詳細(verbose)資訊。
-z:在傳輸檔案時進行壓縮(compress)。
-a:歸檔模式,保留檔案的權限、屬性等資訊,等同于組合選項“-rlptgoD”。
-p:保留檔案的權限标記。
-t:保留檔案的時間标記。
-g:保留檔案的屬組标記(僅超級使用者使用)。
-o:保留檔案的屬主标記(僅超級使用者使用)。
-H:保留硬連接配接檔案。
-A:保留 ACL 屬性資訊。
-D:保留裝置檔案及其他特殊檔案。
--delete:删除目标位置有而原始位置沒有的檔案,即删除差異檔案,保留一緻性。
--checksum:根據校驗和(而不是檔案大小、修改時間)來決定是否跳過檔案。
--password-file=file:從file中得到密碼,用于免互動處理,file檔案的權限要是600
3、配置源的兩種表達方式
将指定的資源下載下傳到本地/root 目錄下進行備份。
格式一:
使用者名@主機位址::共享子產品名
例如:
[email protected]::wwwroot /opt
格式二:
rsync://使用者名@主機位址/共享子產品名
例如:
rsync://[email protected]/wwwroot /opt
4、免互動格式
echo "mi碼" > /etc/mi碼檔案
chmod 600 /etc/mi碼檔案
#設定周期性任務
crontab -e
30 22 * * * /usr/bin/rsync -az --delete --password-file=/etc/mi碼檔案 [email protected]::wwwroot /opt
systemctl restart crond
systemctl enable crond
四、inotify簡介
Inotify 是一個 Linux核心的特性,可以監控檔案系統的變動情況,并做出通知響應,輔助軟體:inotify-tools
1、調整inotify核心參數(優化)
- /etc/sysctl.conf(核心參數配置檔案),需要配置
- max_queue_events :監控事件隊列大小
- max_user_instances :最多監控執行個體
- max_user_watches :每個執行個體最多監控檔案數
- 配置的監控數量應該大于監控目标的總檔案數
例如:
vim /etc/sysctl.conf
max_queue_events = 16384
max_user_instances = 1024
max_user_watches = 1048576
2、使用inotify-tools輔助工具
- inotifywait:用于持續監控,實時輸出結果
- 可監控modify(修改)、create(建立)、move(移動)、delete(删除)、attrib(屬性更改)等各種事件,一有變動立即輸出結果。
- inotifywatch:用于短期監控,任務完成後再輸出結果
- 可用來收集檔案系統變動情況,并在運作結束後輸出彙總的變化情況。
例:
inotifywait -mrq -e modify,create,attrib,move,delete 檔案或目錄
#---------參數解釋------------
-m 持續進行監控
-r 遞歸監控所有子對象
-q 簡化輸出資訊
-e 指定要監控哪些事件類型
modify 修改
create 建立
attrib 屬性更改
move 移動
deletc 删除
3、編寫同步腳本
編寫思路:
(1)先設定兩個變量:監控和執行備份
(2)使用while 、read持續擷取監控結果
(3)根據結果判斷,執行不同的操作
vim /opt/inotify_rsynx.sh
#!/bin/bash
#定義兩個變量:監控檔案,執行備份
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete 需要監控的目錄或檔案"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/密碼檔案 剛才監控的目錄或檔案 使用者名@主機位址::共享子產品名"
#while read擷取監控結果
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
#如果rsync沒有運作,執行rsync進行備份操作
if [ $(pgrep rsync | wc -l) -eq 0 ] ; then
$RSYNC_CMD
fi
done
五、配置rsync下行同步
下行同步:将master伺服器資料備份到slave伺服器
環境配置
主機 | 作業系統 | IP位址 | 安裝包 |
---|---|---|---|
Master | CentOS7 | 192.168.56.72 | rsync |
Slave | CentOS7 | 192.168.56.73 | rsync / inotify-tools-3.14.tar.gz |
1、Master(192.168.56.72)
(1)關防火牆、安裝相應的軟體
systemctl stop firewalld.service
setenforce 0
#檢查是否安裝,一般系統已預設安裝rsync
rpm -q rsync
yum -y install rsync
(2)建立/etc/rsyncd.conf 配置檔案
- 如采用匿名的方式,隻要将下面配置中的“auth users”和“secrets file”配置項去掉即可。
vim /etc/rsyncd.conf
uid = root
gid = root
use chroot = yes
address = 192.168.56.72
port 873
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 192.168.56.0/24
[wwwroot]
path = /var/www/html
comment = Document Root of www.test.com
read only = yes
dont comperss = *.gz *.bz2 *.tgz *.zip *.rar *.z
auth users = ww
secrets file = /etc/rsyncd_users.db
#---------配置解釋----------------------------------------------
uid = root
gid = root
use chroot = yes #禁锢在源目錄
address = 192.168.56.72 #監聽位址,監聽本機位址
port 873 #監聽端口 tcp/udp 873,可通過cat /etc/services | grep rsync檢視
log file = /var/log/rsyncd.log #日志檔案位置
pid file = /var/run/rsyncd.pid #存放程序 ID 的檔案位置
hosts allow = 192.168.56.0/24 #允許同步的客戶機網段
[wwwroot] #共享子產品名稱
path = /var/www/html #源目錄的實際路徑(同步的目錄)
comment = Document Root of www.test.com
read only = yes #是否為隻讀
dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z #同步時不再壓縮的檔案類型
auth users = ww #授權賬戶,多個賬号以空格分隔
secrets file = /etc/rsyncd_users.db #存放賬戶資訊的資料檔案
(3)為備份賬戶建立資料檔案
- 無須建立同名系統使用者
vim /etc/rsyncd_users.db
ww:666zjw
chmod 600 /etc/rsyncd_users.db
(4)保證所有使用者對源目錄/var/www/html(需要備份的檔案目錄)都有讀取權限
yum -y install httpd
chmod +r /var/www/html
ls -ld /var/www/html
(5)啟動 rsync 服務程式
- 啟動 rsync 服務,以獨立監聽服務的方式(守護程序)運作
rsync --daemon
netstat -natp | grep rsync
關閉 rsync 服務
kill $(cat /var/run/rsyncd.pid)
rm -rf /var/run/rsyncd.pid
2、Slave(192.168.56.73)
systemctl stop firewalld.service
setenforce 0
yum -y install rsync
cd /opt
mkdir abc
chmod 777 abc
vim /etc/server.pass
666zjw
chmod 600 /etc/server.pass
3、驗證
Master(192.168.56.72)
cd /var/www/html/
vim 1.html
Slave(192.168.56.73)
rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/abc
ls abc
- 企業中不可能手動執行,一般都要使用周期性任務
#設定周期性任務
crontab -e
0 2 * * * /usr/bin/rsync -az --delete --password-file=/etc/server.pass [email protected]::wwwroot /opt/abc
systemctl restart crond
systemctl enable crond
六、rsync+inotify實時同步
1、Master(192.168.56.72)
- 修改rsync源伺服器配置檔案
vim /etc/rsyncd.conf
#關閉隻讀,上行同步需要可以寫
read only = no
#重新開機服務
kill `cat /var/run/rsyncd.pid`
netstat -natp | grep rsync
rsync --daemon
netstat -natp | grep rsync
chmod 777 /var/www/html
2、Slave(192.168.56.73)
(1)調整 inotify 核心參數
- max_queue_events(監控事件隊列,預設值為16384)
- max_user_instances(最多監控執行個體數,預設值為128)
- max_user_watches(每個執行個體最多監控檔案數,預設值為8192)
- 當要監控的目錄、檔案數量較多或者變化較頻繁時,建議加大這三個參數的值。
cat /proc/sys/fs/inotify/max_queued_events
cat /proc/sys/fs/inotify/max_user_instances
cat /proc/sys/fs/inotify/max_user_watches
vim /etc/sysctl.conf
fs.inotify.max_queued_events = 17000
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
sysctl -p
(2)安裝 inotify-tools
yum -y install gcc gcc-c++ make
#放入安裝包
tar zxvf inotify-tools-3.14.tar.gz -C /opt
cd /opt/inotify-tools-3.14/
./configure && make && make install
(3)編寫觸發式同步腳本
vim /opt/inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib /opt/abc/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /opt/abc/ [email protected]::wwwroot"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
$RSYNC_CMD
fi
done
cd /opt/
chmod +x inotify.sh
./opt/inotify.sh &
#加入開機自動執行
chmod +x /etc/rc.d/rc.local
echo '/opt/inotify.sh' >> /etc/rc.d/rc.local
3、驗證
cd /opt/abc
touch 2.html
ls
rm -rf 1.html
ls
cd /var/www/html
ls