rsync基本用法
本地同步
- rsync [選項…] 本地目錄A 本地目錄B(同步整個檔案夾)
- rsync [選項…] 本地目錄A/ 本地目錄B(隻同步目錄下的資料)
常用選項:
- -a : 歸檔模式,表示以遞歸方式傳輸檔案,并保持所有檔案屬性,等于-rlptgoD
- -v : 詳細模式輸出
- -q : 精簡輸出模式
- -z : 傳輸過程中啟用壓縮/解壓操作
- –delete:删除目标檔案夾内多餘的文檔
- -n : 檢測同步過程,不做實際修改操作(與-v合用效果更佳)
遠端同步
rsync […] [email protected]:遠端目錄 本地目錄
rsync […] 本地目錄 [email protected]:遠端目錄
-
案例1:
列出遠端主機的/目錄下有那些目錄
[[email protected] ~]# rsync [email protected]:/
[email protected]'s password:
dr-xr-xr-x 255 2019/04/16 15:48:27 .
-rw-r--r-- 0 2018/05/28 19:52:07 .autorelabel
lrwxrwxrwx 7 2018/05/29 03:45:41 bin
lrwxrwxrwx 7 2018/05/29 03:45:41 lib
lrwxrwxrwx 9 2018/05/29 03:45:41 lib64
lrwxrwxrwx 8 2018/05/29 03:45:41 sbin
dr-xr-xr-x 4096 2018/05/30 20:19:05 boot
drwxr-xr-x 2960 2019/04/27 13:59:18 dev
drwxr-xr-x 8192 2019/04/16 15:48:54 etc
drwxr-xr-x 6 2016/11/05 23:38:36 home
drwxr-xr-x 6 2016/11/05 23:38:36 media
drwxr-xr-x 6 2016/11/05 23:38:36 mnt
drwxr-xr-x 6 2016/11/05 23:38:36 opt
dr-xr-xr-x 0 2019/04/27 08:49:31 proc
dr-xr-x--- 234 2019/04/16 16:07:33 root
drwxr-xr-x 780 2019/04/27 08:49:59 run
drwxr-xr-x 6 2016/11/05 23:38:36 srv
dr-xr-xr-x 0 2019/04/27 16:49:31 sys
drwxrwxrwt 251 2019/04/27 09:17:01 tmp
drwxr-xr-x 155 2018/05/29 03:45:41 usr
drwxr-xr-x 265 2019/04/15 17:31:33 var
drwxr-xr-x 6 2019/04/16 15:48:27 web
-
案例2:
将遠端主機/etc/passwd檔案同步到目前目錄
[[email protected] etc]# rsync -avz [email protected]:/etc/passwd ./
[email protected]'s password:
receiving incremental file list
sent 11 bytes received 36 bytes 4.48 bytes/sec
total size is 1170 speedup is 24.89
-
案例3:
将本機的/etc目錄同步到遠端主機的/opt下
[[email protected] etc]# rsync -az /etc [email protected]:/opt
[email protected]'s password:
inotify-tools檔案監控工具
-
源碼包安裝
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
wget http://deb.debian.org/debian/pool/main/i/inotify-tools/inotify-tools_3.14.orig.tar.gz #兩個網絡源都可
[[email protected] ~]# wget http://deb.debian.org/debian/pool/main/i/inotify-tools/inotify-tools_3.14.orig.tar.gz #下載下傳源碼包
[[email protected] ~]# tar -xf inotify-tools_3.14.orig.tar.gz #解壓
[[email protected] ~]# cd inotify-tools-3.14
[[email protected] ~]# yum -y install gcc c++ #編譯安裝源碼包依賴環境
[[email protected] ~]# ./configure #安裝源碼包
[[email protected] ~]# make && make install #編譯安裝
[[email protected] ~]# ls /usr/local/bin/inotifywait
/usr/local/bin/inotifywait #驗證安裝
[[email protected] ~]# inotifywait --help #驗證安裝
inotifywait常用指令選項:
- -m,持續監控
- -r,遞歸監控、包括子目錄及檔案
- -q,減少螢幕輸出資訊
- -e,指定監視的 modify、move、create、delete、attrib 等事件類别
-
案例1:
持續監控/etc/目錄下所有變化
[[email protected] ~]# inotifywait -rmq /etc/ &
[1] 3961
-
案例2:
結合rsync實作遠端實時同步
[[email protected] ~]# ssh-keygen -N "" -f ~/.ssh/id_rsa #生成密鑰對(公私鑰)
[[email protected] ~]# ssh-copy-id [email protected] #傳遞公鑰
[[email protected] ~]# ssh [email protected] #驗證免密
[[email protected] ~]# vim rsync.sh #編寫監控腳本
#!/bin/bash
while inotifywait -rqq /var/www/html
do
rsync -az /var/www/html/ [email protected]:/var/www/html/
done &
[[email protected] ~]# chmod +x /root/rsync.sh #添加執行權限
[[email protected] ~]# pgrep -l rsync #檢索監控程序
21515 rsync.sh
[[email protected] ~]# kill 21515 #殺死程序,關閉實時監控(開機自啟可将腳本内容直接寫入/etc/rc.d/rc.local下)