rsync同步數據

rsync數據同步工具

rsync命令是一個遠程數據同步工具,可經過LAN/WAN快速同步多臺主機間的文件。rsync使用所謂的「rsync算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快。 rsync是一個功能很是強大的工具,其命令也有不少功能特點選項,咱們下面就對它的選項一一進行分析說明。

若是你的linux沒有rsync命令請使用 yum install -y rsync 安裝。linux

其使用語法以下算法

  • 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
  • rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

** rsync經常使用選項**ssh

  • -a 歸檔模式,表示以遞歸方式傳輸文件,並保持全部屬性,等同於-rlptgoD, -a選項後面能夠跟一個 --no-OPTION 這個表示關閉-rlptgoD中的某一個例如 -a--no-l 等同於-rptgoD工具

  • -r 對子目錄以遞歸模式處理,主要是針對目錄來講的,若是單獨傳一個文件不須要加-r,可是傳輸的是目錄必須加-r選項unix

  • -v 打印一些信息出來,好比速率,文件數量等code

  • -l 保留軟鏈結遞歸

  • -L 向對待常規文件同樣處理軟鏈結,若是是SRC中有軟鏈接文件,則加上該選項後將會把軟鏈接指向的目標文件拷貝到DSTdns

  • -p 保持文件權限rem

  • -o 保持文件屬主信息get

  • -g 保持文件屬組信息

  • -D 保持設備文件信息

  • -t 保持文件時間信息

  • --delete 刪除那些DST中SRC沒有的文件

  • --exclude=PATTERN 指定排除不須要傳輸的文件,等號後面跟文件名,能夠是萬用字符模式(如*.txt)

  • --progress 在同步的過程當中能夠看到同步的過程狀態,好比統計要同步的文件數量、同步的文件傳輸速度等等

  • -u 加上這個選項後將會把DST中比SRC還新的文件排除掉,不會覆蓋

舉例分析:

[root@localhost test]# rsync -av /tmp/1.txt /tmp/test2/
sending incremental file list
created directory /tmp/test2
1.txt

sent 759 bytes  received 68 bytes  1,654.00 bytes/sec
total size is 669  speedup is 0.81
[root@localhost test]# cd /tmp/test2
[root@localhost test2]# ls
1.txt
  • 上面這就是語法中的第一種,本地同步文件,-v選項就是現實速率,文件大小等
[root@localhost ~]# rsync -avl /tmp/ root@192.168.254.130:/tmp/
Enter passphrase for key '/root/.ssh/id_rsa': 
root@192.168.254.130's password: 
Permission denied, please try again.
root@192.168.254.130's password: 
sending incremental file list
./
1.txt
.ICE-unix/
.Test-unix/
.X11-unix/
.XIM-unix/
.font-unix/
test/
test/1.txt
test2/
test2/1.txt

sent 2,451 bytes  received 114 bytes  394.62 bytes/sec
total size is 2,007  speedup is 0.78
  • 上面這是語法中的第二種,將本地的/tmp/目錄下的文件同步到遠程192.168.254.130的/tmp/下,root@是以root的身份,不加這個默認也是root。
[root@localhost tmp]# rsync -avrl root@192.168.254.130:/tmp/ /tmp/

root@192.168.254.130's password: 
receiving incremental file list
./

sent 34 bytes  received 333 bytes  56.46 bytes/sec
total size is 2,022  speedup is 5.51
[root@localhost tmp]# ls
1.txt  2.txt  test  test2
[root@localhost tmp]# ll 2.txt 
lrwxrwxrwx. 1 root root 15 7月  29 17:08 2.txt -> /etc/passwd.old
  • 在這個例子中咱們發現,雖然咱們成功的將遠程機器的/tmp目錄同步了過來,且軟連接2.txt也同步了過來,可是軟連接是報錯的,也就是2.txt指向的/etc/passwd.old文件在本地並無,若是要在數據同步時將/etc/passwd.old這樣的軟連接文件一塊兒同步,須要使用-L選項。

** 當咱們須要使用腳本自動同步遠程數據時,腳本中沒法根據提示輸入遠程主機的密碼,就須要使用祕鑰登陸,且祕鑰密碼爲空。** 祕鑰登陸的配置方法以前介紹過就再也不贅述了。

[root@localhost ~]# rsync -avl root@192.168.254.130:/tmp/ /tmp/
receiving incremental file list
./
2.txt -> /etc/passwd.old
asound.conf
dnsmasq.conf
dracut.conf
e2fsck.conf
host.conf
kdump.conf
krb5.conf
ld.so.conf
libaudit.conf
libuser.conf
locale.conf
logrotate.conf
man_db.conf
mke2fs.conf
nsswitch.conf
resolv.conf
rsyncd.conf
rsyslog.conf
sestatus.conf
sudo-ldap.conf
sudo.conf
sysctl.conf
updatedb.conf
vconsole.conf
yum.conf

sent 512 bytes  received 57,070 bytes  38,388.00 bytes/sec
total size is 57,111  speedup is 0.99
  • --delete 選項 刪除那些DST中SRC沒有的文件
[root@llll tmp]# cp /etc/*.conf /tmp/
[root@llll tmp]# ls
1.txt        dnsmasq.conf  host.conf   ld.so.conf     locale.conf     mke2fs.conf    rsyncd.conf    sudo.conf       test           vconsole.conf
2.txt        dracut.conf   kdump.conf  libaudit.conf  logrotate.conf  nsswitch.conf  rsyslog.conf   sudo-ldap.conf  test2          yum.conf
asound.conf  e2fsck.conf   krb5.conf   libuser.conf   man_db.conf     resolv.conf    sestatus.conf  sysctl.conf     updatedb.conf
[root@llll tmp]# rsync -avl --delete root@192.168.254.100:/tmp/ /tmp/
Enter passphrase for key '/root/.ssh/id_rsa': 
root@192.168.254.100's password: 
Permission denied, please try again.
root@192.168.254.100's password: 
receiving incremental file list
deleting yum.conf
deleting vconsole.conf
deleting updatedb.conf
deleting sysctl.conf
deleting sudo.conf
deleting sudo-ldap.conf
deleting sestatus.conf
deleting rsyslog.conf
deleting rsyncd.conf
deleting resolv.conf
deleting nsswitch.conf
deleting mke2fs.conf
deleting man_db.conf
deleting logrotate.conf
deleting locale.conf
deleting libuser.conf
deleting libaudit.conf
deleting ld.so.conf
deleting krb5.conf
deleting kdump.conf
deleting host.conf
deleting e2fsck.conf
deleting dracut.conf
deleting dnsmasq.conf
deleting asound.conf
./

sent 34 bytes  received 329 bytes  19.62 bytes/sec
total size is 2,022  speedup is 5.57
[root@llll tmp]# ls
1.txt  2.txt  test  test2

在使用ssh的方式遠程同步數據的時候,若是ssh服務更改過端口了,則須要指定端口

rsync -avl -e "ssh -port 22" root@192.168.254.100:/tmp/ /tmp/ 就能夠了。

相關文章
相關標籤/搜索