一、rsync介紹
二、關于inotify
三、rsync配置實例
一、rsync介紹
1、關于rsync
一款快速增量備份工具

Remote Sync,遠程同步
支持本地復制,或者與其他SsH、rsync 主機同步
官方網站: http://rsync.samba.org

2、配置rsync源服務器
rsync同步源

指備份操作的遠程服務器,也稱為備份源

配置rsync源

基本思路

建立rsyncd.conf配置文件、獨立的賬號文件
啟用rsync的-daemon模式

應用示例

用戶backuper, 允許下行同步
操作的目錄為/var/www/html/

配置文件rsyncd.conf

需手動建立,語法類似于Samba配置
認證配置auth users、secrets file, 不加則為匿名

rsync賬號文件

采用"用戶名:密碼”的記錄格式,每行一個用戶記錄
獨立的賬號數據,不依賴于系統賬號

啟用rsync服務

通過–daemon獨自提供服務
執行kill $(cat /var/run/rsyncd.pid)關閉rsync服務

3、使用rsync備份工具
rsync命令用法

rsync [選項] 原始位置 目標位置
常用選項

-a:歸檔模式,遞歸并保留對象屬性,等同于-rlptgoD
-v:示同步過程的詳細(verbose)信息
-z:在傳輸文件時進行壓縮(compress)
-H:保留硬連接文件
-A:保留ACL屬性信息
–delete:刪除目標位置有而原始位置沒有的文件
–checksum:根據對象的校驗和來決定是否跳過文件

配置源的兩種表示方法

格式1: 用戶名@主機地址::共享模塊名
格式2: rsync://用戶名@主機地址/共享模塊名

4、rsync實時同步
定期同步的不足

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

實時同步的優點

一旦同步源出現變化,立即啟動備份
只要同步源無變化,則不執行備份

二、關于inotify
Linux內核的inotify機制

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

rsync inotify實時同步

調整inotify內核參數

max_queue_events: 監控隊列大小
max_user_instances: 最多監控實例數
max_user_watches:每個實例最多監控文件數,配置時應大于監控目標的總文件數

安裝inotify-tools輔助工具

inotifywait:用于持續監控,實時輸出結果
常用選項
-m:持續進行監控
-r:遞歸監控所有子對象
-q:簡化輸出信息
-e:指定要監控哪些事件類型
inotifwatch:用于短期監控,任務完成后再出結果

通過inotifywait觸發rsync同步操作

使用while、read持續獲取監控結果
根據結果可以作進一 步判斷,決定執行何種操作

三、配置實例
實驗環境

rsyncd服務器IP地址:192.168.144.128
client客戶端IP地址:192.168.144.129

在rsyncd服務器上修改配置文件

[root@rsyncd ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64
[root@rsyncd ~]# vim /etc/rsyncd.conf

uid = nobody           //匿名用戶
gid = nobody
use chroot = yes         //禁錮家目錄
pid file = /var/run/rsyncd.pid        //pid文件路徑
address = 192.168.144.128                //配置監聽地址
port = 873                        //端口號
log file = /var/log/rsyncd.log      //日志文件路徑
hosts allow = 192.168.144.0/24         //允許地址段訪問
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   //不需要壓縮的類型

[wwwroot]           //共享模塊名
path = /var/www/html        //共享文件路徑
comment = www.kgc.com      //定義名稱
read only = yes            //只讀權限
auth users = backuper       //身份驗證用戶名
secrets file = /etc/rsyncd_users.db        //密碼文件
:wq
[root@rsyncd ~]# vim /etc/rsyncd_users.db      //創建密碼文件
backuper:123123       //編輯用戶名:密碼
:wq
[root@rsyncd ~]# chmod 600 /etc/rsyncd_users.db       //更改權限
[root@rsyncd ~]# rsync --daemon                    //開啟rsync服務
[root@rsyncd ~]# netstat -ntap | grep rsync         //查看端口
tcp     0     0 192.168.144.128:873      0.0.0.0:*    LISTEN    36346/rsync 
[root@rsyncd ~]# systemctl stop firewalld.service       //關閉防火墻
[root@rsyncd ~]# setenforce 0
[root@rsyncd ~]# yum install httpd -y           //安裝httpd服務
[root@rsyncd ~]# cd /var/www/html/
[root@rsyncd html]# echo this is test web > index.html      //編輯網頁信息
[root@rsyncd html]# cd ../
[root@rsyncd www]# chmod 777 html/          //放開目錄權限,方便用戶操作

在客戶端服務器上,拉取同步源rsyncd

[root@client ~]# systemctl stop firewalld.service      //關閉防火墻
[root@client ~]# setenforce 0                      //關閉selinux
[root@client ~]# rpm -q rsync               //檢查是否安裝rsync服務
rsync-3.0.9-18.el7.x86_64
[root@client ~]# yum install httpd -y         //安裝httpd服務
[root@client ~]# cd /var/www/
[root@client www]# chmod 777 html/               //放開目錄權限
[root@client www]# rsync -avz backuper@192.168.144.128::wwwroot /var/www/html      //拉取共享模塊
//輸入密碼  
[root@client www]# cat html/index.html          //查看是否同步信息
this is test web
[root@client www]# rm -rf html/index.html 
[root@client www]# vim /etc/server.pass      //創建本地的密碼文件
123123
[root@client www]# chmod 600 /etc/server.pass       //更改權限
[root@client www]# rsync -avz --delete --password-file=/etc/server.pass backuper@192.168.144.128::wwwroot /var/www/html/         //指定本地密碼文件,刪除目標位置有而原始位置沒有的文件,實現免交互

在client客戶端上安裝inotify(監控)

[root@client www]# vim /etc/sysctl.conf        //修改內核參數文件
fs.inotify.max_queued_events = 16384       //隊列
fs.inotify.max_user_instances = 1024        //每個隊列中的實例數
fs.inotify.max_user_watches = 1048576      //每個實例中的文件數
[root@client www]# sysctl -p  ##加載
[root@client www]# mount.cifs //192.168.100.8/LNMP-C7 /mnt/    //掛載
Password for root@//192.168.100.3/LNMP-C7:  
[root@client www]# cd /mnt/
[root@client mnt]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/     //解壓inotify到/opt下
[root@client mnt]# cd /opt/
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# yum install gcc gcc-c   make -y    //安裝環境
[root@client inotify-tools-3.14]# ./configure              //配置
[root@client inotify-tools-3.14]# make && make install     //編譯安裝
[root@client inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/          //啟動監控 

重新開啟一個客戶機的終端

[root@client ~]# cd /var/www/html/
[root@client html]# touch abc
[root@client html]# rm -rf abc

回到開啟監控的終端查看

/var/www/html/ CREATE abc
/var/www/html/ DELETE abc    //顯示監控信息

在client客戶機創建腳本,通過inotifywait觸發rsync同步操作腳本

[root@client inotify-tools-3.14]# cd /opt/
[root@client opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD=inotifywait -mrq -e modify,create,move,delete /var/www/html/
RSYNC_CMD=rsync -avz --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.144.128::wwwroot/
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
 do
     if [ $(pgrep rsync | wc -l) -le 0 ]; then
            $RSYNC_CMD
     fi
done
[root@client opt]# chmod  x inotify.sh    //添加執行權限

在rsyncd服務器上修改配置文件

[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no         //關閉只讀權限
[root@rsyncd www]# netstat -natp | grep rsync
tcp     0    0 192.168.144.128:873    0.0.0.0:*      LISTEN      36346/rsync         
[root@rsyncd www]# kill -9 36346       //關閉服務
[root@rsyncd www]# netstat -natp | grep rsync
[root@rsyncd www]# rm -rf /var/run/rsyncd.pid      //刪除pid文件
[root@rsyncd www]# rsync --daemon              //重新開啟rsync服務

在client客戶機上執行inotify腳本文件

[root@client opt]# ./inotify.sh

重新開啟一個client客戶機終端

[root@client html]# echo this is test > test.txt      //添加文本

查看開啟監控服務終端信息

[root@client opt]# ./inotify.sh 
sending incremental file list
./
rsync: failed to set times on /. (in wwwroot): Operation not permitted (1)
test.txt

sent 121 bytes  received 30 bytes  302.00 bytes/sec
total size is 30  speedup is 0.20
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
sending incremental file list

sent 66 bytes  received 8 bytes  148.00 bytes/sec
total size is 30  speedup is 0.41

在rsync服務器上查看

[root@rsyncd www]# cd html/
[root@rsyncd html]# ls
index.html  test.txt       //實現同步

更多關于云服務器域名注冊,虛擬主機的問題,請訪問三五互聯官網:m.shinetop.cn

贊(0)
聲明:本網站發布的內容(圖片、視頻和文字)以原創、轉載和分享網絡內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。郵箱:3140448839@qq.com。本站原創內容未經允許不得轉載,或轉載時需注明出處:三五互聯知識庫 » rsync 遠程同步

登錄

找回密碼

注冊

主站蜘蛛池模板: 韩国av无码| 人妻聚色窝窝人体WWW一区| 亚洲av无码片在线播放| 久爱无码精品免费视频在线观看| 日本高清视频网站www| 国产区图片区小说区亚洲区| 精品国产熟女一区二区三区| 亚洲精品国产第一区二区| 国产精品免费视频网站| 成人国产精品一区二区不卡| 好深好湿好硬顶到了好爽| 成人看的污污超级黄网站免费| 一区二区三区鲁丝不卡| 97在线精品视频免费| 大地资源网中文第五页| 亚洲综合av一区二区三区| 国内精品卡一卡二卡三| 国产精品系列在线免费看| 亚洲色一色噜一噜噜噜| 三级网站视频在在线播放| 国产乱弄免费视频观看| 日韩精品一区二区亚洲专区| 亚洲中文字幕精品第三区| 欧美老少配性行为| 亚洲国产成人精品女久久| 亚洲日韩国产成网在线观看| 精品人妻一区二区三区蜜臀| 曰韩无码av一区二区免费| 国产色悠悠综合在线观看| 99久久精品久久久久久婷婷| 欧美颜射内射中出口爆在线| 亚洲区激情区无码区日韩区 | 7777精品久久久大香线蕉| 色综合久久久久综合体桃花网| 上司人妻互换中文字幕| 久久不见久久见免费视频| 桃江县| 自拍偷在线精品自拍偷99| 亚洲精品一区二区天堂| 综合色一色综合久久网| 乱人伦人妻中文字幕不卡|