rsync遠程同步(實例!!!)

rsync 同步概述:

Remote Sync ----- 遠程同步,支持本地複製,或者與其他SSH 、rsync主機同步,功能類似於scp,但是要比scp豐富。
官方網站:http://rsync.samba.org

rsync 同步特點:

1、可以鏡像保存整個目錄樹和文件系統。

2、可以很容易做到保持原來文件的權限、時間、軟硬鏈接等等,無須特殊權限即可安裝。

3、快速:第一次同步時 rsync 會複製全部內容,但在下一次只傳輸修改過的文件。rsync 在傳輸數據的過程中可以實行壓縮及解壓縮操作,因此可以使用更少的帶寬。

4、安全:可以使用scp、ssh等方式來傳輸文件,當然也可以通過直接的socket連接。
5、支持匿名傳輸,以方便進行網站鏡像。

在這裏插入圖片描述


實例演示

第一步:配置rsync源服務器

rsync 是系統內核自帶的,rpm - q rsync查看包 ,無需額外安裝.如果是最小化安裝的話,使用 yum安裝一下即可

1.修改rsyncd.conf配置文件

[root@server ~]# vim /etc/rsyncd.conf 
#7、8、9行,
uid = nobody
gid = nobody
use chroot = yes

#11行,
pid file = /var/run/rsyncd.pid

#16行,
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

#追加端口號
port 873

#追加日誌文件路徑
log file = /var/log/rsyncd.log

#追加授權訪問地址段
hosts allow = 192.168.142.0/24

#添加共享模塊
#模塊名稱
[wwwroot]

#源共享目錄路徑
path = /var/www/html

#網站說明
comment = www.bdqn.cn

#是否只讀
read only = yes

#認證用戶名
auth users = backuper

#認證用戶密碼文件路徑
secrets file = /etc/rsyncd_users.db

2.創建認證用戶密碼文件

[root@server ~]# vim /etc/rsyncd_users.db
#寫入認證用戶名與密碼
backuper:abc123

3.授權僅屬主的最大權限

[root@server ~]# chmod 600 /etc/rsyncd_users.db

4.安裝HTTP的服務

[root@server ~]# yum install httpd -y

5.創建共享內容

[root@server ~]# cd /var/www/html
[root@server html]# echo "this is test web" > index.html

6.開啓服務

[root@server html]# rsync --daemon

7.查看服務狀態

[root@server html]# netstat -ntap | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      60268/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      60268/rsync         

8.關閉防火牆及安全功能

[root@server html]# systemctl stop firewalld.service 
[root@server html]# setenforce 0

第二步:配置客戶發起端

1.關閉防火牆及安全功能

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# setenforce 0

2.安裝http服務

[root@localhost ~]# yum install httpd -y

3.客戶發起端配置方式

#配置源方式一,用戶名@主機地址::共享模塊名
[root@localhost ~]# rsync -avz [email protected]::wwwroot /var/www/html
Password: #輸入用戶密碼
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  72.86 bytes/sec
total size is 17  speedup is 0.07

#查看共享到的內容
[root@localhost ~]# cat /var/www/html/index.html 
this is test web
#配置源方式二,rsync://用戶名@主機地址/共享模塊名
[root@localhost ~]# rsync -avz rsync://[email protected]/wwwroot /var/www/html
Password: 
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  56.67 bytes/sec
total size is 17  speedup is 0.07

#查看共享到的內容
[root@localhost ~]# cat /var/www/html/index.html 
this is test web

4.創建免交互密碼文件

[root@localhost ~]# vim /etc/server.pass
abc123

[root@localhost ~]# chmod 600 /etc/server.pass

#免交互配置源方式
[root@localhost ~]# rsync -avz --delete --password-file=/etc/server.pass [email protected]::wwwroot /var/www/html
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  510.00 bytes/sec
total size is 17  speedup is 0.07

#查看共享到的內容
[root@localhost ~]# cat /var/www/html/index.html 
this is test web

配合inotify工具使用,實現rsync實時同步

配置rsync實時同步:

1.定期同步的不足:

執行備份的時間固定,延遲明細,實時性差;
當同步源長期不變化時,密集的定期任務是不必要的

2.實時同步的優點:

一旦同步源出現變化,立即啓用備份;
只要同步源不變化,則不執行備份

關於 inotify:

Inotify 是一個 Linux特性,它監控文件系統操作,比如讀取、寫入和創建。Inotify 反應靈敏,用法非常簡單,並且比 cron 任務的繁忙輪詢高效得多。

從版本 2.6.13 開始提供;
可以監控文件系統的變化情況,並作出通知響應;
輔助軟件:inotify-tools

在這裏插入圖片描述

第一步: 配置rsync+inotify實時同步

1.配置rsync源服務器,修改rsyncd.conf配置文件

[root@server ~]# vim /etc/rsyncd.conf 
#關閉只讀
read only = no

2.調整客戶端的inotify內核參數

[root@client ~]# vim /etc/sysctl.conf
#監控隊列大小
fs.inotify.max_queued_events = 16384

#最多監控實例數
fs.inotify.max_user_instances = 1024

#每個實例最多監控文件數
fs.inotify.max_user_watches = 1048576

3.生效內核參數

[root@client ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

4.安裝編譯環境

[root@client ~]# yum install -y gcc gcc-c++ make

5.遠程獲取資源包

[root@client ~]# mount.cifs //192.168.142.1/inotify /mnt
[root@sclient ~]# cd /mnt
[root@client mnt]# ls
inotify-tools-3.14.tar.gz

6.解壓資源包

[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt

7.配置inotify

[root@client mnt]# cd /opt/inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure

8.編譯安裝

[root@client inotify-tools-3.14]# make && make install

9.安裝inotify-tools輔助工具

[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/

#-m表示持續進行監控,-r表示遞歸監控所有子對象,-q表示簡化輸出信息,-e表示要監控哪些時間類型

10.重開一個終端登錄,增刪文件

#創建文件
[root@client html]# touch abc
[root@client html]# ls
abc  index.html

#刪除文件
[root@client html]# rm -rf abc
[root@client html]# ls
index.html

11.返回監控端,驗證同步效果

[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE abc       #創建記錄
/var/www/html/ DELETE abc       #刪除記錄

12.通過inotifywait觸發rsync同步操作腳本

vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNCLCMD="rsyne -azH --delete --password-file=/etc/server.pass /var/www/htm1/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
#讀取輸出的監控記錄
do
if [ $(pgrep rsync | wc -l) -le 0 ] ; then
#若rsync爲執行,則立即啓動
$RSYNC_CMD
fi
done

13.源端於客戶端都需要html目錄最高授權

[root@server www]# chmod 777 html/
[root@client www]# chmod 777 html/

14.執行腳本

[root@client opt]# source inotify.sh

15.重開終端,並切入共享目錄

[root@client opt]# cd /var/www/html/

16.寫入新的內容

[root@client html]# echo "this is my update" > test.txt

第二步:驗證實時同步

**1.回到源端查看同步數據包**
[root@server html]# ls
index.html  test.txt

2.查看同步數據

[root@server html]# cat test.txt 
this is my update

謝謝閱讀!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章