rsync工具的常用選項、ssh同步介紹
rsync是數據備份工具(字面意思可以理解為遠程同步),不僅可以遠程同步數據,而且可以本地同步數據(類似與cp),但不同於cp或scp的一點是,它不會覆蓋以前的數據(如果數據已經存在),而是先判斷已經存在的數據和新數據的差異,只有數據不同時才會把不相同的部分覆蓋。
安裝rsync命令:#yum install -y rsync
講解rsync的用法
舉例將/etc/passwd同步到/tmp/目錄下,並改名為1.txt,操作如下:
# rsync -av /etc/passwd /tmp/1.txt
sending incremental file list
passwd
sent 1460 bytes received 31 bytes 2982.00 bytes/sec
total size is 1386 speedup is 0.93
如果是遠程復制,數據備份的形式就是這樣的形式——IP:path,比如172.16.111.110:/root/,具體用法如下:
# rsync -av /tmp/1.txt 172.16.111.110:/tmp/2.txt
The authenticity of host '172.16.111.110 (172.16.111.110)' can't be established.
ECDSA key fingerprint is 09:6d:70:42:42:9a:12:69:51:9b:ad:e5:73:98:b9:c0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.111.110' (ECDSA) to the list of known hosts.
[email protected]'s password:
sending incremental file list
1.txt
sent 1459 bytes received 31 bytes 270.91 bytes/sec
total size is 1386 speedup is 0.93
rsync的命令格式
rsync [OPTION] … SRC DEST
rsync [OPTION] … SRC [user@]host:DEST
rsync [OPTION] … [user@]host:SRC DEST
rsync [OPTION] … SRC [user@]host::DEST
rsync [OPTION] … [user@]host::SRC DEST
解釋:在前面的例子中,第一個例子為第一種格式,第二個例子為第二種格式,但不同的是並沒有加用戶名user@host,如果不加默認指的是root。第三種格式是用遠程目錄同步數據到本地。第四種和第五種格式使用了兩個冒號,這種格式和其他格式的驗證方式不同。
2. rsync常用選項
rsync常用選項
-a 這是歸檔模式,表示以遞歸方式傳輸文件,並保持所有屬性,它等同於-rlptgoD,-a選項後面可以跟一個--no-OPTION,表示關閉-rlptgoD中的某一個,比如-a--no-l等同於-rlptgoD。
-r 表示以遞歸模式處理子目錄。它主要是針對目錄來說的,如果單獨傳一個文件不需要加-r選項,但是傳輸目錄時必須加。
-v 表示打印一些信息,如文件列表,文件數量等
-l 表示保留軟鏈接
-L 表示像對待常規文件一樣處理軟鏈接,如果SRC文件中有軟鏈接時,則加上該選項後,將會把軟鏈接指向的目標文件一起復制到DEST。
-p 表示保持文件權限
-o 表示保持文件屬主信息
-g 表示保持文件屬組信息
-D 表示保持設備文件信息
-t 表示保持文件時間信息
--delete 表示刪除DEST中SRC中沒有的文件
--exclude=PATTERN 表示指定排除SRC中不需要傳輸的文件,等號後面跟文件名,可以用通配符如*.txt
--progress 在同步的過程中可以看到同步的過程狀態,比如統計要同步的文件數量、同步的文件傳輸速度等。
-u 表示把dest中比src還新的文件排除掉,不會覆蓋
-z 加上該選項,將會在傳輸過程中壓縮
但是常用的選項是-a,-v,-z,--delete和--exclude。
建立目錄和文件
# mkdir rsync
# cd rsync
# touch 1 2 3 /root/123.txt
# ln -s /root/123.txt ./123.txt
# ls -l
總用量 0
-rw-r--r--. 1 root root 0 12月 5 20:57 1
lrwxrwxrwx. 1 root root 13 12月 5 20:57 123.txt -> /root/123.txt
-rw-r--r--. 1 root root 0 12月 5 20:57 2
-rw-r--r--. 1 root root 0 12月 5 20:57 3
-av 把root下的rsync目錄同步到tmp下並且改名rsync_dest,示例如下:
# rsync -av /root/rsync/ /tmp/rsync_dest/
sending incremental file list
123.txt -> /root/123.txt
sent 89 bytes received 15 bytes 208.00 bytes/sec
total size is 13 speedup is 0.12
加上-L選項後,同步軟連接文件時會把源文件同步,示例如下:
# rsync -avL /root/rsync/ /tmp/rsync_dest/
sending incremental file list
123.txt
sent 103 bytes received 31 bytes 268.00 bytes/sec
total size is 0 speedup is 0.00
-delete 同步時刪除目標目錄rsync_dest中源目錄rsync沒有的文件,示例如下:
# rsync -avL -delete /root/rsync/ /tmp/rsync_dest/
sending incremental file list
sent 64 bytes received 12 bytes 152.00 bytes/sec
total size is 0 speedup is 0.00
--exclude 同步時過濾掉文件名或目錄名為.txt,不同步(支持寫多個exclude,但不支持同一個exclude有多個條件),示例如下:
# rsync -avL --exclude "*.txt" /root/rsync/ /tmp/rsync_dest/
sending incremental file list
created directory /tmp/rsync_dest
./
1
2
3
sent 172 bytes received 72 bytes 488.00 bytes/sec
total size is 0 speedup is 0.00
過濾掉帶1開頭,示例如下:
# rsync -avL --exclude "*.txt" --exclude "1" /root/rsync/ /tmp/rsync_dest/
sending incremental file list
created directory /tmp/rsync_dest
./
2
3
sent 127 bytes received 53 bytes 360.00 bytes/sec
total size is 0 speedup is 0.00
-P 選項是顯示同步過程,比如速率,示例如下:
# rsync -avP /root/rsync/ /tmp/rsync_dest/
sending incremental file list
created directory /tmp/rsync_dest
./
1
0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=3/5)
123.txt -> /root/123.txt
2
0 100% 0.00kB/s 0:00:00 (xfer#2, to-check=1/5)
3
0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=0/5)
sent 209 bytes received 75 bytes 568.00 bytes/sec
total size is 13 speedup is 0.05
-u 選項如果目標文件中的文件比源文件新,則不同步,示例如下:
# rsync -avPu /root/rsync/ /tmp/rsync_dest/ //加-u保留之前文件內容
sending incremental file list
./
sent 89 bytes received 15 bytes 208.00 bytes/sec
total size is 13 speedup is 0.12
# cat 1 查看文件沒有被覆蓋
afdgagadsga
dagdkdgja
agdaga
adgaga
3. rsync通過ssh同步
ssh同步到另外一臺主機
示例如下:
# rsync -av /etc/passwd 172.16.111.110:/tmp/aming.txt 把文件拷貝過去
[email protected]'s password:
sending incremental file list
passwd
sent 1460 bytes received 31 bytes 271.09 bytes/sec
total size is 1386 speedup is 0.93
# rsync -avP 172.16.111.110:/tmp/aming.txt /tmp/123.txt 把文件拷貝過來
[email protected]'s password:
receiving incremental file list
aming.txt
1386 100% 1.32MB/s 0:00:00 (xfer#1, to-check=0/1)
sent 30 bytes received 1468 bytes 332.89 bytes/sec
total size is 1386 speedup is 0.93
假設對方機器端口不是22的話,那應該如何,示例如下:
# rsync -avP -e "ssh -p 22 " /etc/passwd 172.16.111.110:/tmp/aming.txt
[email protected]'s password:
sending incremental file list
passwd
1386 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 1460 bytes received 31 bytes 331.33 bytes/sec
total size is 1386 speedup is 0.93
# ssh -p 22 172.16.111.110 通過輸入端口遠程連接對方機器
[email protected]'s password:
Last failed login: Tue Dec 5 19:45:42 CST 2017 from 172.16.111.100 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Tue Dec 5 19:34:02 2017 from 172.16.111.1
# 登出
Connection to 172.16.111.110 closed.
rsync工具的常用選項、ssh同步介紹