八周二次课 rsync工具介绍,常用参数选项以及和ssh同步
linux文件同步工具-rsync
rsync,它是一個同步工具。他非常實用,也非常重要。我們幾乎每天都要用它,比如傳輸文件,遠程備份數據。他也可以在本機備份,作用類似與命令cp,但又區別于cp。比如我們要拷貝一個文件A,將它命名為B。但A每小時更新一次,如果用cp命令將會很麻煩。這時我們用rsync就可以 很好的解決這個問題,他可以增量拷貝,它只拷貝新增的文件。
本機同步拷貝
我們要將passwd這個文件同步到/tmp/1.txt中
[root@linletao-001 ~]# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1233 bytes received 31 bytes 2528.00 bytes/sec
total size is 1159 speedup is 0.92
這樣就完成了。
遠程同步拷貝
我們要將passwd這個文件遠程同步到root用戶下的/tmp/1.txt中
[root@linletao-001 ~]# rsync -av /etc/passwd root@192.168.1.106:/tmp/1.txt
root@192.168.1.106's password:
sending incremental file list
sent 31 bytes received 12 bytes 9.56 bytes/sec
total size is 1159 speedup is 26.95
這就是遠程同步的格式,我們需要在目標文件前面加上用戶名和IP,在用戶名和IP之間用“@”隔開。IP和目標文件之間要用“:”隔開
rsync還有其他格式
rsync [OPTION](選項) … SRC(源目錄) DEST(目標文件或者目錄)
rsync [OPTION] … SRC(源目錄) [user@]host:DEST(源目錄),其中[user@]是用戶名,host是目標IP。我們也可以簡寫。不加用戶名。,這樣就是當前用戶。
rsync [OPTION] … [user@]host:SRC DEST
這是將遠程的目標文件拷貝到本機上。
rsync常用選項
-a 包含-rtplgoD 平時我們加-a就足夠了
-r 同步目錄時要加上,類似cp時的-r選項
-v 可視化
-l 保留軟連接。如果源目錄有軟連接文件,加上“l”的話就會把軟連接文件本身拷貝到目標文件,兩邊相同。
-L 同步軟鏈接時會把源文件給同步
-p保持文件屬性
-o保持文件的屬主
-g保持文件屬組
-D 保持設備文件信息
-t 保持文件的時間屬性
--delete 刪除DEST中SRC沒有的文件。刪除目標目錄中源目錄沒有的文件。比如同步a文件到b文件,如果要這兩個要保持一模一樣得到狀態,假如b設備內有一些a目錄沒有的文件,那么我們就要加--delete,來讓他們同步。
--exclude 過濾指定文件,如--exclude “logs”會把文件名包含logs的文件或者目錄過濾掉,不同步。他也支持通配。
-P 顯示同步過程,比如速率,比-v更加詳細。
-u 加上該選項后,如果DEST(目標目錄)中的文件比SRC(源目錄)新,則不同步。比如a設備中的1文件比b設備的1文件新,那么就不會同步。
-z 傳輸時壓縮。這樣節省帶寬。
舉例說明
將root下的111目錄同步到/tmp下并且改名字叫111_dest。
rsync -av /root/111/ /tmp/111_dest/,這里提一個要求,以后要同步目錄的話就在目錄的最后面加一個"/",目標后面也要加“/”。
[root@linletao-001 111]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
.111
1.txt
2.txt
llt.txt -> test
sent 127 bytes received 18 bytes 290.00 bytes/sec
total size is 3248 speedup is 22.40
然后我們在參數中加L
[root@linletao-001 ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
sent 116 bytes received 12 bytes 256.00 bytes/sec
total size is 3244 speedup is 25.34
L它的作用就是將軟連接所指向的源文件復制過來
我們要將111目錄中的文件同步到/tmp/111_dest,但是/tmp/111_dest多了一個new.txt文件,如果要使兩邊同步,就要加參數--delete。
首先我們先查看一下兩邊的文件是否同步
[root@linletao-001 ~]# ls 111/
1.txt 2.txt llt.txt test.txt
[root@linletao-001 ~]# ls /tmp/111_dest/
1.txt 2.txt llt.txt new.txt test.txt
我們發現/111_dest/多了一個new.txt文件
然后我們開始同步,加參數--delete。讓兩邊同步
[root@linletao-001 ~]# rsync -avL --delete /root/111/ /tmp/111_dest/
sending incremental file list
./
deleting new.txt
sent 119 bytes received 15 bytes 268.00 bytes/sec
total size is 3278 speedup is 24.46
我們發現同步的過程已經刪除了new.txt這個文件。
然后我們查詢一下
[root@linletao-001 ~]# ls /tmp/111_dest/
1.txt 2.txt llt.txt test.txt
我們發現/tmp/111_dest中的new.txt文件也被刪除了。這就是--delete的作用。
我們要將111中格式為txt文件給過濾出來
[root@linletao-001 111]# rsync -avL --exclude "*.txt" /root/111/ /tmp/111_dest/
sending incremental file list
./
456
789/
sent 128 bytes received 38 bytes 332.00 bytes/sec
total size is 1065 speedup is 6.42
這樣就將格式為txt的文件給過濾出來的。而且它支持寫多個--exclude。
顯示同步的詳細過程
[root@linletao-001 ~]# rsync -avP /root/111/ /tmp/111_dest/
sending incremental file list
./
1.txt
1159 100% 0.00kB/s 0:00:00 (xfer#1, to-check=5/8)
2.txt
1020 100% 996.09kB/s 0:00:00 (xfer#2, to-check=4/8)
456
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=3/8)
llt.txt -> test.txt
test.txt
17 100% 16.60kB/s 0:00:00 (xfer#4, to-check=1/8)
789/
sent 2538 bytes received 98 bytes 5272.00 bytes/sec
total size is 3269 speedup is 1.24
這樣就將每一個文件同步的詳細過程給列出來了。
還有一種情況,我們的目標文件要新于源文件,那么在同步的是后就會將源文件同時更改,如果這樣,也許會造成源文件損壞而無法使用功能,這樣的情況下我們在同步時加一個參數u就可以保護我們的源文件了。
rsync -avPu /root/111/ /tmp/111_dest/
在我們遠程同步比較大的文件時加一個z,這樣可以在同步過程中壓縮文件,這樣可以節省帶寬
rsync -avPz /root/111/ /tmp/111_dest/
rsync通過ssh方式同步
比如我們要將a設備的/etc/passwd同步到b設備的/tmp/aming.txt中。條件是b設備也要安裝rsync這個工具。
[root@linletao-001 ~]# rsync -av /etc/passwd 192.168.1.109:/tmp/aming.txt
root@192.168.1.109's password:(輸入目標設備的root密碼)
sending incremental file list
passwd
sent 1233 bytes received 31 bytes 280.89 bytes/sec
total size is 1159 speedup is 0.92
這就可以了,這叫做推文件。
我們也可以從b設備的文件同步到a設備,格式就是先將b設備的ip和要同步的目錄寫到前面,而本機不用寫ip,直接寫目標目錄即可。
[root@linletao-001 ~]# rsync -avP 192.168.1.109:/tmp/aming.txt /tmp/123.txt
root@192.168.1.109's password:(輸入本機root密碼)
receiving incremental file list
aming.txt
1159 100% 1.11MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 30 bytes received 1241 bytes 363.14 bytes/sec
total size is 1159 speedup is 0.91
這樣就可以了。這也叫做拉文件。
還有一種情況,對方的端口并不是22,這時候我們加一個選項-e “ssh -p 22”,這樣就可以指定端22口了。
[root@linletao-001 ~]# rsync -avP -e "ssh -p 22" 192.168.1.109:/tmp/aming.txt /tmp/123.txt
root@192.168.1.109's password:
receiving incremental file list
sent 11 bytes received 39 bytes 7.69 bytes/sec
total size is 1159 speedup is 23.18
通過ssh -p加對方ip即可連接對方
[root@linletao-001 ~]# ssh -p 22 192.168.1.109
root@192.168.1.109's password:
Last login: Tue Mar 27 20:53:10 2018 from 192.168.1.106
轉載于:https://blog.51cto.com/13067688/2091844
總結
以上是生活随笔為你收集整理的八周二次课 rsync工具介绍,常用参数选项以及和ssh同步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP7.0 Window10 Red
- 下一篇: 程序员第一次相亲,因请女方吃肯德基而被怒