若是你的linux沒有rsync命令請使用 yum install -y rsync 安裝。linux
其使用語法以下算法
** 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
[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
[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
** 當咱們須要使用腳本自動同步遠程數據時,腳本中沒法根據提示輸入遠程主機的密碼,就須要使用祕鑰登陸,且祕鑰密碼爲空。** 祕鑰登陸的配置方法以前介紹過就再也不贅述了。
[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
[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/
就能夠了。