溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

rsync遠程同步——(實戰?。?/h1>
發布時間:2020-07-23 05:38:06 來源:網絡 閱讀:401 作者:一拳超人007 欄目:系統運維

關于rsync

一款快速增量備份工具

Remote Sync,遠程同步
支持本地復制,或者與其他SSH,rsync主機同步

配置rsync源服務器

rsync同步源

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

rsync遠程同步——(實戰?。?></p>
<h2>配置rsync源</h2>
<h4>基本思路</h4>
<pre><code>建立rsync.conf配置文件,獨立的賬號文件
啟用rsync的--daemon模式</code></pre>
<h4>應用示例</h4>
<pre><code>用戶backuper,允許下行同步
操作的目錄為/var/www/html</code></pre>
<h4>配置文件rsyncd.conf</h4>
<pre><code>需手動建立,語法類似于Samba配置
認證配置auth users,secrets file,不加則為匿名</code></pre>
<h4>rsync賬號文件</h4>
<pre><code>采用“用戶名:密碼”的記錄格式,每行一個用戶記錄
獨立的賬號數據,不依賴于系統賬號</code></pre>
<h4>啟用rsync服務</h4>
<pre><code>通過--daemon獨自提供服務
執行kill $(cat /var/run/rsync.pid)關閉rsync服務</code></pre>
<h2>使用rsync備份工具</h2>
<h4>rsync命令的用法</h4>
<pre><code>rsync [選項] 原始位置 目標位置</code></pre>
<h4>常用選項</h4>
<pre><code>-a:歸檔模式,遞歸并保留對象屬性,等用于-rlptgoD
-v:顯示同步過程的詳細信息
-z:在傳輸文件時進行壓縮
-H:保留硬連接文件
-A:保留ACL屬性信息
--delete:刪除目標位置有而原始位置沒有的文件
--checksum:根據對象的校驗和來決定是否跳過文件</code></pre>
<h4>配置源的兩種表示方法</h4>
<pre><code>格式1:用戶名@主機地址::共享模塊名
格式2:rsync://用戶名@主機地址/共享模塊名</code></pre>
<h2>rsync實時同步</h2>
<h4>定期同步的不足</h4>
<pre><code>執行備份的時間固定,延遲明顯,實時性差
當同步源長期不變化時,密集的定期任務是不必要的</code></pre>
<h4>實時同步的優點</h4>
<pre><code>一旦同步源出現變化,立即啟動備份
只要同步源無變化,則不執行備份</code></pre>
<h2>關于 inotify(安裝在發起端的)</h2>
<pre><code>Inotify 是一個 Linux特性,它監控文件系統操作,比如讀取、寫入和創建。Inotify 反應靈敏,用法非常簡單,并且比 cron 任務的繁忙輪詢高效得多。
可以監控文件系統的變化情況,并作出通知響應;
輔助軟件:inotify-tools</code></pre>
<p><img src=

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

[root@client ~]# systemctl stop firewalld.service  ##關閉防火墻
[root@client ~]# setenforce 0
[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.13.128::wwwroot /var/www/html/ 
##拉取共享模塊
Password:   ##輸入密碼  
[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.13.128::wwwroot /var/www/html/ 
##指定本地密碼文件,刪除目標位置有而原始位置沒有的文件,實現免交互

3,在客戶機上安裝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.3/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

4,在客戶機創建腳本,通過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.13.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  ##給執行權限

##確保服務端和客戶端的權限都為最大

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

[root@rsyncd www]# vim /etc/rsyncd.conf
read only = no  ##關閉只讀
[root@rsyncd www]# netstat -natp | grep rsync
tcp     0    0 192.168.13.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服務

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

[root@client opt]# ./inotify.sh
##重新開啟一個客戶機終端
[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

7,在rsync服務器上查看

[root@rsyncd www]# cd html/
[root@rsyncd html]# ls
index.html  test.txt   ##同步完成
##刪除也是同步的

謝謝閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女