rsync命令基礎

10月31日任務
10.28 rsync工具介紹
10.29/10.30 rsync經常使用選項
10.31 rsync經過ssh同步
 

同步工具rsync介紹

默認未安裝,使用yum install -y rsyncpython

拷貝一個目錄的數據到另外一個目錄,拷貝的同時目錄的內容處在不斷更新中,在這裏使用cp命令就可能會有些問題:沒法實現增量拷貝,浪費時間;屢次全量拷貝,對磁盤的讀寫壓力大;bash

rsync命令就十分使用於這樣的狀況,能夠實現增量備份ssh

  • 本地同步
[root@localhost system]# rysnc -av /etc/passwd root@192.168.65.133:/tmp/1.txt
-bash: rysnc: 未找到命令
[root@localhost system]# rsync -av /etc/passwd root@192.168.65.133:/tmp/1.txt
The authenticity of host '192.168.65.133 (192.168.65.133)' can't be established.
ECDSA key fingerprint is 42:50:a7:09:91:db:af:77:a5:3a:b3:67:1c:8a:5b:99.
Are you sure you want to continue connecting (yes/no)? yes     
Warning: Permanently added '192.168.65.133' (ECDSA) to the list of known hosts.
root@192.168.65.133's password: 
sending incremental file list
passwd

sent 1156 bytes  received 31 bytes  67.83 bytes/sec
total size is 1082  speedup is 0.91
  • 遠程同步
# 指定遠程主機的格式能夠不指定用戶名,默認是指遠程主機上的當前用戶
[root@localhost ~]# rsync -av /etc/passwd root@192.168.65.130:/tmp/1.txt
root@192.168.65.133's password: 
sending incremental file list

sent 31 bytes  received 12 bytes  3.19 bytes/sec
total size is 1128  speedup is 26.23

rsync命令經常使用選項

  • -a 包含-rtplhoD
  • -v 顯示同步的過程
[root@localhost ~]# rsync -av /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
file.py
test -> /etc/passwd
try.py
222/

sent 576 bytes  received 60 bytes  1272.00 bytes/sec
total size is 382  speedup is 0.60

[root@localhost ~]# ls -l 111/
總用量 8
drwxr-xr-x. 2 root root   6 10月 27 19:49 222
-rw-r--r--. 1 root root 146 10月 27 19:45 file.py
lrwxrwxrwx. 1 root root  11 10月 27 19:49 test -> /etc/passwd
-rw-r--r--. 1 root root 225 10月 27 19:45 try.py

[root@localhost ~]# ls -l /tmp/111_dest/
總用量 8
drwxr-xr-x. 2 root root   6 10月 27 19:49 222
-rw-r--r--. 1 root root 146 10月 27 19:45 file.py
lrwxrwxrwx. 1 root root  11 10月 27 19:49 test -> /etc/passwd
-rw-r--r--. 1 root root 225 10月 27 19:45 try.py
  • -L 同步軟連接時會把源文件同步 (不會同步軟連接文件,而是同步被連接的源文件)
[root@localhost ~]# rsync -avL /root/111/ /tmp/111_dest/
sending incremental file list
test

sent 1265 bytes  received 32 bytes  2594.00 bytes/sec
total size is 1499  speedup is 1.16

[root@localhost ~]# ls -l /tmp/111_dest/
總用量 12
drwxr-xr-x. 2 root root    6 10月 27 19:49 222
-rw-r--r--. 1 root root  146 10月 27 19:45 file.py
-rw-r--r--. 1 root root 1128 10月 23 20:22 test
-rw-r--r--. 1 root root  225 10月 27 19:45 try.py

[root@localhost ~]# ls -l 111/
總用量 8
drwxr-xr-x. 2 root root   6 10月 27 19:49 222
-rw-r--r--. 1 root root 146 10月 27 19:45 file.py
lrwxrwxrwx. 1 root root  11 10月 27 19:49 test -> /etc/passwd
-rw-r--r--. 1 root root 225 10月 27 19:45 try.py
  • --delete 刪除目標中源所沒有的文件
[root@localhost ~]# rsync --delete -avL /root/111/ /tmp/111_dest/ 
sending incremental file list
./
deleting 1.txt

sent 97 bytes  received 16 bytes  226.00 bytes/sec
total size is 1499  speedup is 13.27
  • --exclude 過濾指定文件
[root@localhost ~]# rsync -avL --exclude "*.py" /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
test
222/

sent 1244 bytes  received 38 bytes  2564.00 bytes/sec
total size is 1128  speedup is 0.88
  • -P 顯示同步過程,例如速率,比-v更詳細
[root@localhost ~]# rsync -aP /root/111/ /tmp/111_dest/
sending incremental file list
created directory /tmp/111_dest
./
file.py
         146 100%    0.00kB/s    0:00:00 (xfer#1, to-check=3/5)
test -> /etc/passwd
try.py
         225 100%  219.73kB/s    0:00:00 (xfer#2, to-check=1/5)
222/

sent 576 bytes  received 60 bytes  1272.00 bytes/sec
total size is 382  speedup is 0.60
  • -u 目標中的文件比源中新,則不一樣步
# 修改目標的文件內容,使目標的文件比源新
[root@localhost ~]# vi /tmp/111_dest/try.py

# 使用-u參數,防止源中文件同步過來
[root@localhost ~]# rsync -avu /root/111/ /tmp/111_dest/
sending incremental file list
./

sent 113 bytes  received 16 bytes  258.00 bytes/sec
total size is 382  speedup is 2.96

# 驗證
# 目標文件添加一行註釋,較源文件更新
[root@localhost ~]# cat /tmp/111_dest/try.py 
#!/usr/bin/env python
# test
try:
    filename = raw_input('Enter file name: ')
    fobj = open(filename, 'r')
    for eachLine in fobj:
        print eachLine,
    fobj.close()
except IOError as e:
    print "file open error:", e
    
[root@localhost ~]# cat 111/try.py 
#!/usr/bin/env python

try:
    filename = raw_input('Enter file name: ')
    fobj = open(filename, 'r')
    for eachLine in fobj:
        print eachLine,
    fobj.close()
except IOError as e:
    print "file open error:", e
  • -z 傳輸時進行壓縮(能夠節省帶寬)
[root@localhost ~]# rsync -avPz /root/111/ /tmp/111_dest/
sending incremental file list
try.py
         225 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/5)

sent 297 bytes  received 32 bytes  658.00 bytes/sec
total size is 382  speedup is 1.16

其餘的幾個參數(因爲-a默認包括了不少參數,因此幾乎使用-a替代了)工具

  • -r 同步目錄時須要
  • -l 保留軟連接
  • -p 保持文件的權限
  • -o 保持文件的全部者
  • -g 保持文件的所屬組
  • -D 保持設備文件信息
  • -t 保持文件的時間屬性

P、L等參數配合-a參數使用,會替代默認的p、l參數功能spa

rsync經過ssh同步

前提:本機和遠程主機都須要安裝rsynccode

經過ssh訪問來同步文件/目錄的幾種寫法:ci

把本地文件推(同步)到遠端主機

[root@localhost ~]# rsync -av /etc/passwd 192.168.65.130:/tmp/1.txt

將遠端主機的文件拉(同步)到本機

[root@localhost ~]# rsync -av 192.168.65.130:/etc/passwd /tmp/1.txt

指定ssh訪問的端口

# 這裏假設遠端ssh端口爲66
[root@localhost ~]# rsync -av -e "ssh -p 66" /etc/passwd 192.168.133.130:/tmp/1.txt
相關文章
相關標籤/搜索