rsync命令是一個遠程數據同步工具,可經過LAN/WAN快速同步多臺主機間的文件。rsync使用所謂的「rsync算法」來使本地和遠程兩個主機之間的文件達到同步,這個算法只傳送兩個文件的不一樣部分,而不是每次都整份傳送,所以速度至關快。算法
rsync有六種不一樣的工做模式:數組
注: src表示源文件,dest表示目的文件ssh
例如:工具
rsync -av /etc/passwd /tmp/1.txtspa
rsync -av 192.168.180.134:/tmp/ipt.txt /tmp/new.txt
rsync格式code
rsync [OPTION] … SRC DEST 遞歸
rsync [OPTION] … SRC [user@]host:DEST user能夠省略, 省略則用當前終端用戶身份同步, 若是對方機器沒有該用戶名則會報錯ip
rsync [OPTION] … [user@]host:SRC DEST 從遠程目錄同步到本地 拉文件rem
rsync [OPTION] … SRC [user@]host::DEST 兩個冒號同步
rsync [OPTION] … [user@]host::SRC DEST
進行遠程同步時 必須遠程機和本地機都有rsync工具才能夠 yum install -y rsync
[root@yong-01 ~]# rsync -av /root/123/ /tmp/123_dest/ sending incremental file list created directory /tmp/123_dest ./ 123.txt awk.sh cj.txt passwd 考試.txt sent 1559 bytes received 110 bytes 3338.00 bytes/sec total size is 1243 speedup is 0.74 [root@yong-01 ~]# ls /tmp/123_dest/ 123.txt awk.sh cj.txt passwd 考試.txt
刪除目標目錄中源目錄中不存在的文件 new.txt 加上--delete選項
[root@yong-01 ~]# rsync -av /etc/passwd 192.168.180.135:/tmp/test0514.txt sending incremental file list passwd sent 1208 bytes received 31 bytes 2478.00 bytes/sec total size is 1134 speedup is 0.92 [root@yong-02 ~]# ls /tmp/test0514.txt /tmp/test0514.txt
[root@yong-01 ~]# rsync -av -e "ssh -p 22 " /etc/passwd 192.168.180.135:/tmp/test0514.txt sending incremental file list passwd 1134 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1) sent 1208 bytes received 31 bytes 2478.00 bytes/sec total size is 1134 speedup is 0.92
[root@yong-01 ~]# rsync -av 192.168.180.135:/etc/passwd /tmp/22.txt receiving incremental file list passwd sent 30 bytes received 1099 bytes 2258.00 bytes/sec total size is 1020 speedup is 0.90 [root@yong-01 ~]# ls /tmp/22.txt /tmp/22.txt
[root@yong-01 ~]# rsync -avP /root/123 /tmp/123_test/ sending incremental file list created directory /tmp/123_test 123/ 123/123.txt 0 100% 0.00kB/s 0:00:00 (xfer#1, to-check=4/6) 123/awk.sh 283 100% 0.00kB/s 0:00:00 (xfer#2, to-check=3/6) 123/cj.txt 0 100% 0.00kB/s 0:00:00 (xfer#3, to-check=2/6) 123/passwd 960 100% 937.50kB/s 0:00:00 (xfer#4, to-check=1/6) 123/考試.txt 0 100% 0.00kB/s 0:00:00 (xfer#5, to-check=0/6) sent 1575 bytes received 111 bytes 3372.00 bytes/sec total size is 1243 speedup is 0.74
[root@1 ~]# rsync -avL /root/grep/ /tmp/grep_dest/ sending incremental file list adailink sent 1530 bytes received 32 bytes 3124.00 bytes/sec total size is 14275 speedup is 9.14
[root@yong-01 ~]# touch /tmp/123_dest/new.txt [root@yong-01 ~]# rsync -av --delete /root/123/ /tmp/123 123_dest/ 123.log [root@yong-01 ~]# rsync -av --delete /root/123/ /tmp/123_dest/ sending incremental file list ./ deleting new.txt //刪除目標文件中的new.txt sent 113 bytes received 15 bytes 256.00 bytes/sec total size is 1243 speedup is 9.71
[root@yong-01 ~]# ls 123 123.txt awk.sh cj.txt passwd 考試.txt [root@yong-01 ~]# rsync -av --exclude "*.sh" /root/123/ /tmp/123_dest/ //過濾掉了sh文件 sending incremental file list created directory /tmp/123_dest ./ 123.txt cj.txt passwd 考試.txt sent 1218 bytes received 91 bytes 2618.00 bytes/sec total size is 960 speedup is 0.73 [root@yong-01 ~]# ls /tmp/123_dest/ 123.txt cj.txt passwd 考試.txt
注: 能夠同時過濾多種文件。