案例1:rsync基本用法
案例2:rsync+SSH同步
案例3:使用inotifywait工具
案例4:配置Web鏡像同步
1 案例1:rsync基本用法
1.1 問題html
本例要求掌握遠程同步的基本操做,使用rsync命令完成下列任務:
將目錄 /boot 同步到目錄 /todir 下
將目錄 /boot 下的文檔同步到目錄 /todir 下
在目錄 /boot 下新增文件 a.txt,刪除 /todir 下的子目錄 grub2,再次同步使 /todir 與 /boot 一致
驗證 -a、-n、-v、--delete 選項的含義
1.2 方案linux
本地同步操做:
rsync [選項...] 本地目錄1 本地目錄2
rsync [選項...] 本地目錄1/ 本地目錄2
rsync同步工具的經常使用選項:
-n:測試同步過程,不作實際修改
--delete:刪除目標文件夾內多餘的文檔
-a:歸檔模式,至關於-rlptgoD
-v:顯示詳細操做信息
-z:傳輸過程當中啓用壓縮/解壓
1.3 步驟vim
實現此案例須要按照以下步驟進行。
步驟一:rsync同步基本操做bash
1)將目錄 /boot 同步到目錄 /todir 下
[root@svr7 ~]# ls -l /todir //同步前
ls: 沒法訪問/todir: 沒有那個文件或目錄
[root@svr7 ~]# rsync -a /boot /todir //將目錄1做爲目錄2的子目錄
[root@svr7 ~]# ls -l /todir //檢查同步結果
總用量 4
dr-xr-xr-x. 4 root root 4096 11月 30 18:50 boot
2)將目錄 /boot 下的文檔同步到目錄 /todir 下
[root@svr7 ~]# rm -rf /todir //清理掉目錄2
[root@svr7 ~]# rsync -a /boot/ /todir //將目錄1下的文檔同步到目錄2下
[root@svr7 ~]# ls -l /todir //檢查同步結果
總用量 126708
-rw-r--r--. 1 root root 126426 10月 30 2015 config-3.10.0-327.el7.x86_64
drwxr-xr-x. 2 root root 4096 11月 30 18:50 extlinux
drwx------. 6 root root 104 12月 9 09:58 grub2
.. ..
3)同步效果測試
在目錄/boot下新增文件a.txt,刪除/todir下的子目錄 grub2:
[root@svr7 ~]# touch /boot/a.txt
[root@svr7 ~]# rm -rf /todir/grub2/
如今目錄/boot和/todir目錄下的內容已經不一致了:
[root@svr7 ~]# ls -ld /boot/a.txt /todir/a.txt
ls: 沒法訪問/todir/a.txt: 沒有那個文件或目錄
-rw-r--r--. 1 root root 0 1月 11 21:09 /boot/a.txt
[root@svr7 ~]# ls -ld /boot/grub2 /todir/grub2
ls: 沒法訪問/todir/grub2: 沒有那個文件或目錄
drwx------. 6 root root 104 12月 9 09:58 /boot/grub2
再次同步使/todir與/boot一致:
[root@svr7 ~]# rsync -a /boot/ /todir/
確認同步結果:
[root@svr7 ~]# ls -ld /boot/a.txt /todir/a.txt
-rw-r--r--. 1 root root 0 1月 11 21:09 /boot/a.txt
-rw-r--r--. 1 root root 0 1月 11 21:09 /todir/a.txt
[root@svr7 ~]# ls -ld /boot/grub2 /todir/grub2
drwx------. 6 root root 104 12月 9 09:58 /boot/grub2
drwx------. 6 root root 104 12月 9 09:58 /todir/grub2
步驟二:驗證 -a、-v、-n、--delete 選項的含義服務器
1)驗證-a選項
當目錄1包含文件夾時,若缺乏-a或-r選項則文件夾會被忽略:
[root@svr7 ~]# rsync /home /testa
skipping directory home
[root@svr7 ~]# ls -ld /testa
ls: 沒法訪問/testa: 沒有那個文件或目錄
添加-a後纔會執行同步:
[root@svr7 ~]# rsync -a /home/ /testa
[root@svr7 ~]# ls -ld /testa
drwxr-xr-x. 4 root root 31 1月 6 17:33 /testa
相似的狀況,當目錄1中的數據出現權限、歸屬、修改時間等變化時,若文件內容不變默認不會同步,若但願目錄2也同步這些變化,也須要-a選項。
2)驗證-v選項
建立測試目錄及文檔:
[root@svr7 ~]# mkdir /fdir
[root@svr7 ~]# touch /fdir/1.txt
添加-v選項時,能夠看到操做細節信息,好比第一次同步時:
[root@svr7 ~]# rsync -av /fdir/ /tdir
sending incremental file list
created directory /tdir
./
1.txt //傳輸文檔列表
sent 82 bytes received 34 bytes 232.00 bytes/sec
total size is 0 speedup is 0.00
在目錄/fdir/添加文件2.txt,再次跟蹤同步信息:
[root@svr7 ~]# touch /fdir/2.txt
sending incremental file list
./
2.txt //傳輸文檔列表
sent 100 bytes received 34 bytes 268.00 bytes/sec
total size is 0 speedup is 0.00
確認目錄1和目錄2的內容已經一致:
[root@svr7 ~]# ls /fdir/ /tdir/
/fdir/:
1.txt 2.txt
/tdir/:
1.txt 2.txt
再次跟蹤同步信息,已經無需傳輸文件:
[root@svr7 ~]# rsync -av /fdir/ /tdir
sending incremental file list
sent 58 bytes received 12 bytes 140.00 bytes/sec
total size is 0 speedup is 0.00
3)驗證-n選項
將-n、-v選項合用,能夠模擬同步過程,顯示須要作哪些操做(但並不真的同步)。
在目錄/fdir下新建文件3.txt,測試同步操做:
[root@svr7 ~]# touch /fdir/3.txt
[root@svr7 ~]# rsync -avn /fdir/ /tdir/
sending incremental file list
./
3.txt //提示同步時會傳輸哪些文件
sent 78 bytes received 18 bytes 192.00 bytes/sec
total size is 0 speedup is 0.00 (DRY RUN)
[root@svr7 ~]# ls -l /tdir/3.txt //但實際並未真的同步
ls: 沒法訪問/tdir/3.txt: 沒有那個文件或目錄
去掉-n選項纔會真正同步:
[root@svr7 ~]# rsync -av /fdir/ /tdir/
sending incremental file list
./
3.txt
sent 114 bytes received 34 bytes 296.00 bytes/sec
total size is 0 speedup is 0.00
[root@svr7 ~]# ls -l /tdir/3.txt
-rw-r--r--. 1 root root 0 1月 11 21:46 /tdir/3.txt
4)驗證--delete選項
rsync同步操做默認只是將目錄1的數據同步到目錄2,但若是目錄2存在多餘的文件卻並不會去除,除非添加—delete選項。
在目錄/fdir、/tdir已經完成同步後,刪除/tdir/2.txt文件,再次同步:
[root@svr7 ~]# rm -rf /fdir/2.txt
[root@svr7 ~]# rsync -a /fdir/ /tdir/
檢查發現目標文件夾/tdir下的2.txt文件還在:
[root@svr7 ~]# ls /fdir/ /tdir/
/fdir/:
1.txt 3.txt
/tdir/:
1.txt 2.txt 3.txt
這種狀況下添加--delete選項再次執行同步,兩個目錄的內容就一致了:
[root@svr7 ~]# rsync -a --delete /fdir/ /tdir/
[root@svr7 ~]# ls /fdir/ /tdir/
/fdir/:
1.txt 3.txt
/tdir/:
1.txt 3.txt
2 案例2:rsync+SSH同步
2.1 問題dom
本例要求掌握rsync與遠程SSH資源的同步操做,使用rsync命令訪問遠程主機svr7,完成下列任務:
查看遠程主機的 / 目錄下有哪些子目錄
從遠程主機下載 /etc/passwd 文件到當前目錄
將遠程主機的 /boot/ 目錄同步爲本地的 /fromssh
將本機的 /etc 目錄同步到遠程主機的 /opt/下
2.2 方案ssh
列出 SSH 服務端資源
rsync user@host:遠程目錄/
rsync+SSH遠程同步操做:
rsync [...] user@host:遠程目錄 本地目錄
rsync [...] 本地目錄 user@host:遠程目錄
2.3 步驟ide
實現此案例須要按照以下步驟進行。
步驟一:列出遠程主機的SSH資源工具
查看遠程主機svr7的/目錄下有哪些子目錄:
[root@pc207 ~]# rsync root@192.168.4.7:/
root@192.168.4.7's password: //驗證對方的密碼
dr-xr-xr-x 4096 2016/12/15 10:39:34 .
lrwxrwxrwx 7 2016/12/07 09:21:50 bin
lrwxrwxrwx 7 2016/12/07 09:21:50 lib
lrwxrwxrwx 9 2016/12/07 09:21:50 lib64
lrwxrwxrwx 8 2016/12/07 09:21:50 sbin
dr-xr-xr-x 4096 2016/12/07 11:25:29 boot
drwxr-xr-x 6 2016/12/07 09:21:14 data
drwxr-xr-x 3200 2016/12/15 10:46:15 dev
drwxr-xr-x 8192 2016/12/20 17:01:02 etc
步驟二:rsync+SSH同步操做測試
1)從遠程主機svr7下載/etc/passwd文件到當前目錄
[root@pc207 ~]# rsync root@192.168.4.7:/etc/passwd ./
root@192.168.4.7's password: //驗證對方的密碼
[root@pc207 ~]# cat passwd //檢查同步結果
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
.. ..
2)將遠程主機svr7的/boot/目錄同步爲本地的/fromssh
[root@pc207 ~]# rsync -a root@192.168.4.7:/boot/ /fromssh
root@192.168.4.7's password: //驗證對方的密碼
[root@pc207 ~]# ls /fromssh/ //檢查同步結果
config-3.10.0-327.el7.x86_64
extlinux
grub2
initramfs-0-rescue-a19921505cc7e19d20dfcd5cea7d8aa2.img
initramfs-3.10.0-327.el7.x86_64.img
initramfs-3.10.0-327.el7.x86_64kdump.img
.. ..
3)將本機的/etc目錄同步到遠程主機svr7的/opt/下
確認目錄大小:
[root@pc207 ~]# du -sh /etc
35M /etc
上行同步到遠程主機svr7上:
[root@pc207 ~]# rsync -a /etc root@192.168.4.7:/opt/
root@192.168.4.7's password:
在遠程主機上檢查同步結果:
[root@svr7 ~]# du -sh /opt/etc
35M /opt/etc
3 案例3:使用inotifywait工具
3.1 問題
本例要求安裝inotify-tools工具,並針對文件夾 /opt 啓用 inotifywait 監控,完成下列任務:
當此目錄下出現新建、修改、更改權限、刪除文件等事件時能給出提示
驗證上述監控事件的效果
3.2 方案
inotifywait監控操做:
inotifywait [選項] 目標文件夾
inotifywait經常使用命令選項:
-m,持續監控(捕獲一個事件後不退出)
-r,遞歸監控、包括子目錄及文件
-q,減小屏幕輸出信息
-e,指定監視的 modify、move、create、delete、attrib 等事件類別
3.3 步驟
實現此案例須要按照以下步驟進行。
步驟一:安裝inotify-tools軟件包
1)解包
[root@svr7 ~]# tar xf inotify-tools-3.13.tar.gz -C /usr/src/
2)配置
[root@svr7 ~]# cd /usr/src/inotify-tools-3.13/
[root@svr7 inotify-tools-3.13]# ./configure
3)編譯
[root@svr7 inotify-tools-3.13]# make
4)安裝
[root@svr7 inotify-tools-3.13]# make
5)檢查安裝結果(inotifywait程序可用)
[root@svr7 ~]# inotifywait --help
inotifywait 3.13
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
.. ..
步驟二:測試inotifywait監控
1)開啓監控任務,置入後臺
[root@svr7 ~]# inotifywait -mrq -e create,modify,move,attrib,delete /opt &
[1] 55564
2)測試/opt/目錄下的新建、修改、更名、更改權限、刪除文件等事件的響應消息
觀察新建文件時的監控信息:
[root@svr7 ~]# touch /opt/a.txt
/opt/ CREATE a.txt
/opt/ ATTRIB a.txt
觀察修改文件內容時的監控信息:
[root@svr7 ~]# echo Hello > /opt/a.txt
[root@svr7 ~]# /opt/ MODIFY a.txt
/opt/ MODIFY a.txt
觀察將文件更名時的監控信息:
[root@svr7 ~]# mv /opt/a.txt /opt/b.txt
/opt/ MOVED_FROM a.txt
/opt/ MOVED_TO b.txt
觀察修改文件權限時的監控信息:
[root@svr7 ~]# chmod 600 /opt/b.txt
/opt/ ATTRIB b.txt
觀察刪除文件時的監控信息:
[root@svr7 ~]# rm -rf /opt/b.txt
/opt/ DELETE b.txt
3)中止監控任務
[root@svr7 ~]# kill -9 %1
[1]+ 已殺死 inotifywait -mr -e create,modify,move,attrib,delete /opt
4 案例4:配置Web鏡像同步
4.1 問題
本例要求爲兩臺Web服務器svr七、pc207的網頁文檔目錄配置鏡像同步,主要基於inotifywait監控技術實現實時觸發操做,須要完成下列任務:
以 svr7 爲發起方,原始目錄爲 /var/www/html/
以 pc207 爲同步目標,基於SSH免密驗證
編寫 inotify+rsync 同步腳本,驗證明時同步效果
4.2 方案
inotifywait與rsync的結合,主要思路:
while inotifywait監控操做
do
須要執行的rsync同步操做
done
4.3 步驟
實現此案例須要按照以下步驟進行。
步驟一:爲主機svr七、pc207部署同步目錄
雙方的目錄均爲/var/www/html/,若是安裝了httpd,此目錄會自動出現。
1)確認svr7的目錄內容
[root@svr7 ~]# yum -y install httpd
.. ..
[root@svr7 ~]# ls /var/www/html/ //向目錄下提供一些測試文件
libreoffice
2)確認pc207的目錄內容
[root@pc207 ~]# yum -y install httpd
.. ..
[root@pc207 ~]# ls /var/www/html //初始目錄無數據
[root@pc207 ~]#
步驟二:爲svr7配置到pc207的SSH密鑰對驗證,實現免密碼交互
1)檢查當前用戶是否已經有可用的SSH密鑰對文件
[root@svr7 ~]# ls ~/.ssh/id_*
/root/.ssh/id_rsa /root/.ssh/id_rsa.pub
若是找不到id_rsa、id_rsa.pub密鑰對文件,則須要執行下列操做建立:
[root@svr7 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): //按回車,確認存放位置
Enter passphrase (empty for no passphrase): //按回車,確認不要密碼
Enter same passphrase again: //再次按回車,確認
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
00:a7:cb:2d:9d:b8:8a:df:f5:ff:5b:ed:bd:04:10:fe root@svr7
The key's randomart image is:
+--[ RSA 2048]----+
| . . . |
| + . . |
| . . o |
| . = o o |
| = + S E |
| o .. |
| . . ...|
| . o . . ....|
|..o . ....o. .+|
+-----------------+
2)將當前用戶的SSH公鑰部署到遠程主機
[root@svr7 ~]# ssh-copy-id root@192.168.4.207
The authenticity of host '192.168.4.207 (192.168.4.207)' can't be established.
ECDSA key fingerprint is d3:16:2c:9a:9d:91:28:c8:74:9c:af:2d:04:82:c9:66.
Are you sure you want to continue connecting (yes/no)? yes //首次連yes確認
root@192.168.4.207's password: //驗證對方的密碼
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'root@192.168.4.207'"
and check to make sure that only the key(s) you wanted were added.
3)驗證免密碼登陸效果
[root@svr7 ~]# ssh root@192.168.4.207
Last login: Fri Jan 13 09:52:08 2017 from 192.168.4.110
[root@pc207 ~]# //確認已免密碼連入遠程主機
[root@pc207 ~]# exit //退出SSH登陸環境
登出
Connection to 192.168.4.207 closed.
[root@svr7 ~]# //已反對原客戶機
步驟三:編寫鏡像同步腳本並測試效果
1)編寫腳本文件/root/isync.sh[root@svr7 ~]# vim /root/isync.sh#!/bin/bashFROM_DIR="/var/www/html/" RSYNC_CMD="rsync -az --delete $FROM_DIR root@192.168.4.207:/var/www/html" while inotifywait -rqq -e modify,move,create,delete,attrib $FROM_DIR do$RSYNC_CMDdone &[root@svr7 ~]# chmod +x /root/isync.sh 2)運行腳本[root@svr7 ~]# /root/isync.sh[root@svr7 ~]# pgrep -l inotify //確認任務在運行56494 inotifywait3)測試同步效果在svr7上向/var/www/html/目錄下添加一個測試網頁(觸發同步):[root@svr7 ~]# touch /var/www/html/a.txt[root@svr7 ~]# ls /var/www/html/a.txt libreoffice在pc207上檢查/var/www/html/目錄,內容應該已經與svr7上的同名目錄一致:[root@pc207 ~]# ls /var/www/htmla.txt libreoffice4)結束測試後,在svr7上中止監控任務[root@svr7 ~]# pkill -9 inotify[root@svr7 ~]# pgrep -l inotify //確認已沒有監控任務[root@svr7 ~]#