rsync工具介紹儡嘶、rsync常用選項、rsync通過ssh同步

目錄

一恍风、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:加上該選項,將會在傳輸過程中壓縮

選項使用練習:

  1. 建立練習用的目錄和文件
[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
  1. 使用-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
  1. 使用-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
  1. 使用-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了。

  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
  1. 使用--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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末决摧,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子凑兰,更是在濱河造成了極大的恐慌蜜徽,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件票摇,死亡現(xiàn)場離奇詭異,居然都是意外死亡砚蓬,警方通過查閱死者的電腦和手機矢门,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灰蛙,“玉大人祟剔,你說我怎么就攤上這事∧ξ啵” “怎么了物延?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長仅父。 經(jīng)常有香客問我叛薯,道長浑吟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任耗溜,我火速辦了婚禮组力,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘抖拴。我一直安慰自己燎字,他們只是感情好,可當我...
    茶點故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布阿宅。 她就那樣靜靜地躺著候衍,像睡著了一般。 火紅的嫁衣襯著肌膚如雪洒放。 梳的紋絲不亂的頭發(fā)上蛉鹿,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天,我揣著相機與錄音拉馋,去河邊找鬼榨为。 笑死,一個胖子當著我的面吹牛煌茴,可吹牛的內(nèi)容都是我干的随闺。 我是一名探鬼主播,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼蔓腐,長吁一口氣:“原來是場噩夢啊……” “哼矩乐!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起回论,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤散罕,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后傀蓉,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體欧漱,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年葬燎,在試婚紗的時候發(fā)現(xiàn)自己被綠了误甚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡谱净,死狀恐怖窑邦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情壕探,我是刑警寧澤冈钦,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站李请,受9級特大地震影響瞧筛,放射性物質(zhì)發(fā)生泄漏厉熟。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一驾窟、第九天 我趴在偏房一處隱蔽的房頂上張望庆猫。 院中可真熱鬧,春花似錦绅络、人聲如沸月培。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽杉畜。三九已至,卻和暖如春衷恭,著一層夾襖步出監(jiān)牢的瞬間此叠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工随珠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留灭袁,地道東北人。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓窗看,卻偏偏與公主長得像茸歧,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子显沈,可洞房花燭夜當晚...
    茶點故事閱讀 45,781評論 2 361

推薦閱讀更多精彩內(nèi)容