1案例1:rsync基本用法
1.1 問題
本例要求掌握遠程同步的基本操作,使用rsync命令完成下列任務:
- 將目錄/boot同步到目錄/todir下
- 將目錄/boot下的文檔同步到目錄/todir下
- 在目錄/boot下新增文件a.txt,刪除/todir下的子目錄grub2,再次同步使/todir與/boot一致
- 驗證-a、-n、-v、–delete選項的含義
1.2方案
本地同步操作:
- rsync [選項…] 本地目錄1 本地目錄2
- rsync [選項…] 本地目錄1/ 本地目錄2
rsync同步工具的常用選項: - -n:測試同步過程,不做實際修改
- –delete:刪除目標文件夾內多餘的文檔
- -a:歸檔模式,相當於-rlptgoD
- -v:顯示詳細操作信息
- -z:傳輸過程中啓用壓縮/解壓
1.3步驟
實現此案例需要按照如下步驟進行。
步驟一:rsync同步基本操作
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