目錄
一恍风、rsync工具介紹
二蹦狂、rsync常用選項
三、rsync通過ssh同步
一朋贬、rsync工具介紹
rsync命令是一個遠程數(shù)據(jù)同步工具凯楔,可通過LAN/WAN快速同步多臺主機間的文件。
rsync不僅可以遠程同步數(shù)據(jù)(類似于scp)锦募,而且可以本地同步數(shù)據(jù)(類似于cp)摆屯,但不同于cp或scp的一點是,它不會覆蓋以前的數(shù)據(jù)(如果數(shù)據(jù)已經(jīng)存在)糠亩,而是先判斷已經(jīng)存在的數(shù)據(jù)和新數(shù)據(jù)的差異虐骑,只有數(shù)據(jù)不同時才會把不相同的部分覆蓋。
系統(tǒng)中沒有rsync赎线,執(zhí)行yum install -y rsync命令安裝rsync廷没。
- rsync 的命令格式
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
舉例:
把/etc/passwd同步到本地/tmp/目錄下,并改名為a.txt垂寥。
[root@minglinux-01 ~]# rsync -av /etc/passwd /tmp/a.txt
sending incremental file list
passwd
sent 1,184 bytes received 35 bytes 2,438.00 bytes/sec
total size is 1,092 speedup is 0.90
[root@minglinux-01 ~]# ll !$
ll /tmp/a.txt
-rw-r--r-- 1 root root 1092 10月 24 23:26 /tmp/a.txt
把/etc/passwd遠程數(shù)據(jù)備份颠黎,形式為:用戶名[@IP]:路徑另锋,比如root@192.168.162.132:/root/,對方機器也需要安裝rsync狭归,示例命令如下:
[root@minglinux-01 ~]# rsync -av /etc/passwd root@192.168.162.132:/root/a.txt
root@192.168.162.132's password:
sending incremental file list
passwd
sent 1,184 bytes received 35 bytes 221.64 bytes/sec
total size is 1,092 speedup is 0.90
[root@minglinux-02 ~]# ls //minglinux-02IP地址即192.168.162.132
anaconda-ks.cfg a.txt
[root@minglinux-02 ~]# ll a.txt
-rw-r--r--. 1 root root 1092 10月 24 23:26 a.txt
二夭坪、rsync常用選項
rsync命令常用選項:
-a:這是歸檔模式,表示以遞歸方式傳輸文件过椎,并保持所有屬性室梅,它等同于-rlptgoD。-a選項后面可以跟一個--no-OPTION潭流,表示關閉-rlptgoD中的某一個竞惋,比如-a--no-l等同于-rptgoD。
-r:表示以遞歸模式處理子目錄灰嫉。它主要是針對目錄來說的拆宛,如果單獨傳一個文件不需要加-r選項,但是傳輸目錄時必須加讼撒。
-v:表示打印一些信息浑厚,比如文件列表、文件數(shù)量等根盒。
-l:表示保留軟連接钳幅。
-L:表示像對待常規(guī)文件一樣處理軟連接。如果是SRC中有軟連接文件炎滞,則加上該選項后敢艰,將會把軟連接指向的目標文件復制到DST。
-p:表示保持文件權限册赛。
-o:表示保持文件屬主信息钠导。
-g:表示保持文件屬組信息。
-D:表示保持設備文件信息森瘪。
-t:表示保持文件時間信息牡属。
--delete:表示刪除DST中SRC沒有的文件。
--exclude=PATTERN:表示指定排除不需要傳輸?shù)奈募蟛牵忍柡竺娓募ぃ梢允侨f用字符模式(如*.txt)。
--progress:表示在同步的過程中可以看到同步的過程狀態(tài)窗宇,比如統(tǒng)計要同步的文件數(shù)量措伐、同步的文件傳輸速度等。
-u:表示把DST中比SRC還新的文件排除掉担映,不會覆蓋废士。
-z:加上該選項,將會在傳輸過程中壓縮
選項使用練習:
- 建立練習用的目錄和文件
[root@minglinux-01 ~]# mkdir rsync
[root@minglinux-01 ~]# cd rsync/
[root@minglinux-01 rsync]# mkdir test1
[root@minglinux-01 rsync]# cd test1/
[root@minglinux-01 test1]# touch 1 2 3 /root/123.tzr
[root@minglinux-01 test1]# touch 1 2 3 /root/123.txt
[root@minglinux-01 test1]# ln -s /root/123.txt ./123.txt
[root@minglinux-01 test1]# ll
總用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
- 使用-a選項
使用rsync備份目錄時要在源目錄和目標目錄后面加上斜杠/蝇完,否則達不到復制效果官硝。
示例命令如下:
[root@minglinux-01 rsync]# rsync -a test1/ test2/
[root@minglinux-01 rsync]# ll test2/
總用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
lrwxrwxrwx 1 root root 13 10月 31 22:48 123.txt -> /root/123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
-a選項等同于-rlptgoD矗蕊,且-a還可以和--no-OPTIN一并使用。
使用--no-l不備份鏈接文件氢架,示例命令如下:
[root@minglinux-01 rsync]# rm -rf test2
[root@minglinux-01 rsync]# ls
test1
[root@minglinux-01 rsync]# rsync -av --no-l test1/ test2/
sending incremental file list
created directory test2
skipping non-regular file "123.txt"
./
1
2
3
sent 229 bytes received 144 bytes 746.00 bytes/sec
total size is 13 speedup is 0.03
- 使用-L選項
加上-L選項就可以把SRC中軟連接的目標文件復制到DST傻咖。示例命令如下:
[root@minglinux-01 rsync]# rm -rf test2
[root@minglinux-01 rsync]# rsync -avL test1/ test2/
sending incremental file list
created directory test2
./
1
123.txt
2
3
sent 260 bytes received 123 bytes 766.00 bytes/sec
total size is 0 speedup is 0.00
[root@minglinux-01 rsync]# ll test2/
總用量 0
-rw-r--r-- 1 root root 0 10月 31 22:47 1
-rw-r--r-- 1 root root 0 10月 31 22:47 123.txt
-rw-r--r-- 1 root root 0 10月 31 22:47 2
-rw-r--r-- 1 root root 0 10月 31 22:47 3
- 使用-u選項
首先查看test1/1和test2/1的創(chuàng)建時間是一樣的:
[root@minglinux-01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1
面修改test2/1的創(chuàng)建時間,然后使用rsync不加-u同步:
[root@minglinux-01 rsync]# echo "1" > test2/1
[root@minglinux-01 rsync]# ll !$
ll test2/1
-rw-r--r-- 1 root root 2 10月 31 23:09 test2/1
[root@minglinux-01 rsync]# rsync -a test1/1 test2/
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test2/1
上面test2/1的創(chuàng)建時間還是和test1/1一樣岖研。下面加上-u選項:
[root@minglinux-01 rsync]# echo "123" > test2/1
[root@minglinux-01 rsync]# ll test2/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1
[root@minglinux-01 rsync]# rsy
rsync rsyslogd rsyslog-recover-qi.pl
[root@minglinux-01 rsync]# rsync -avu test1/ test2/
sending incremental file list
./
123.txt -> /root/123.txt
sent 129 bytes received 22 bytes 302.00 bytes/sec
total size is 13 speedup is 0.09
[root@minglinux-01 rsync]# ll test1/1 test2/1
-rw-r--r-- 1 root root 0 10月 31 22:47 test1/1
-rw-r--r-- 1 root root 4 10月 31 23:12 test2/1
加上-u選項后卿操,不會再把test1/1同步為test2/1了。
- 使用--delete選項
首先刪除test1/123.txt孙援,然后把test1/目錄同步到test2/目錄下:
[root@minglinux-01 rsync]# rm -f test1/123.txt
[root@minglinux-01 rsync]# ls test1/
1 2 3
[root@minglinux-01 rsync]# rsync -av test1/ test2/
sending incremental file list
./
1
sent 127 bytes received 38 bytes 330.00 bytes/sec
total size is 0 speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1 123.txt 2 3
上例中害淤,test2/目錄并沒有刪除test1目錄中沒有的123.txt。下面加上--delete選項拓售,示例如下:
[root@minglinux-01 rsync]# rsync -av --delete test1/ test2/
sending incremental file list
deleting 123.txt
sent 81 bytes received 23 bytes 208.00 bytes/sec
total size is 0 speedup is 0.00
[root@minglinux-01 rsync]# ls test2/
1 2 3
這樣test2/目錄下的123.txt也被刪除了窥摄。
如果在DST中增加文件了,而SRC當中沒有這些文件础淤,同步時加上
--delete選項后同樣會刪除新增的文件崭放。如下所示:
[root@minglinux-01 rsync]# ls test2/
1 2 3
[root@minglinux-01 rsync]# touch test2/4
[root@minglinux-01 rsync]# ls test1/
1 2 3
[root@minglinux-01 rsync]# ls test2/
1 2 3 4
[root@minglinux-01 rsync]# rsync -a --delete test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1 2 3
test2/:
1 2 3
- 使用--exclude選項
[root@minglinux-01 rsync]# touch test1/4
[root@minglinux-01 rsync]# rsync -a --exclude="4" test1/ test2/
[root@minglinux-01 rsync]# ls test1/ test2/
test1/:
1 2 3 4
test2/:
1 2 3
三、rsync通過ssh同步
前面介紹過的rsync的5種命令格式中如下兩種就屬于通過ssh的方式備份數(shù)據(jù)鸽凶。這種方式其實就是讓用戶登錄到遠程機器币砂,然后執(zhí)行rsync的任務。
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
第一種方式已經(jīng)實現(xiàn)過了玻侥,下面試試第二種方式:
[root@minglinux-01 rsync]# rsync -avL 192.168.162.132:/root/a.txt ./test3
root@192.168.162.132's password:
receiving incremental file list
a.txt
sent 43 bytes received 1,183 bytes 272.44 bytes/sec
total size is 1,092 speedup is 0.89