日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

rsync 常用命令及格式

發布時間:2025/1/21 编程问答 74 豆豆
生活随笔 收集整理的這篇文章主要介紹了 rsync 常用命令及格式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

rsync在同步文件夾內容這個工作上應用非常廣泛,但是rsync本身命令還是比較復雜,本文總結一下: rsync = remote sync的簡稱 ,它 被用于在linux/unix系統中執行備份操作。rsnync用于從一個位置到另外一個位置同步文件和文件夾。備份的地址可以是本地也可以是remote server。

rsync的重要功能:

speed

首次使用時,rsync在source和destination folder之間復制全部內容。下次使用時,rsync只傳輸變更的塊或字節到目的地,而這個機制將大大提升傳輸速度

security

rsync允許對數據使用ssh協議加密

less bandwidth

rsync使用對數據塊壓縮和解壓縮的辦法降低帶寬需求。

privileges

無需特殊的特權來運行rsync 語法 $ rsync options source destination source和destination可以是本地或者遠程目錄。對于遠程的情況,需要指定login name, remote server name and location 例1:在本地服務器上同步兩個目錄 在本地機器上同步兩個目錄,使用 rsync -zvr命令 $ rsync -zvr /var/opt/installation/inventory/ /root/temp building file list ... done sva.xml svB.xml . sent 26385 bytes received 1098 bytes 54966.00 bytes/sec total size is 44867 speedup is 1.63 $ 上述命令中: -z 打開壓縮功能-v verbose更多打印信息-r recursive 執行上述命令后,你會發現rsync copy會影響到文件的timestamp信息,這時因為默認rsync并不保護timestamp信息 例2:在sync時,保留時間戳 -a(achive mode:recursive mode, 保留符號鏈接,保留權限信息,時間戳,以及owner,group信息) $ rsync -azv /var/opt/installation/inventory/ /root/temp/ building file list ... done ./ sva.xml svB.xml . sent 26499 bytes received 1104 bytes 55206.00 bytes/sec total size is 44867 speedup is 1.63 $ 注意這時你會發現source,dest文件的時間戳等信息是不變的 $ ls -l /var/opt/installation/inventory/sva.xml /root/temp/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /var/opt/installation/inventory/sva.xml -r--r--r-- 1 root bin 949 Jun 18 2009 /root/temp/sva.xml 例3:只同步一個文件 只要在rsync命令中指定文件名稱即可: $ rsync -v /var/lib/rpm/Pubkeys /root/temp/Pubkeyssent 42 bytes received 12380 bytes 3549.14 bytes/sec total size is 12288 speedup is 0.99 例4:從本地到遠端 $ rsync -avz /root/temp/ thegeekstuff@192.168.200.10:/home/thegeekstuff/temp/ Password: building file list ... done ./ rpm/ rpm/Basenames rpm/Conflictnamesent 15810261 bytes received 412 bytes 2432411.23 bytes/sec total size is 45305958 speedup is 2.87 當執行和remote server同步的動作時,你需要指定username,ip。也要指定遠程服務器上的目的地目錄,格式是: username@machineIP:Path 這個過程中,rsync會要求輸入密碼。但是如果你有一個腳本自動運行這個備份動作,你可能希望不要手動輸入密碼,這時可以參考: http://www.thegeekstuff.com/2008/11/3-steps-to-perform-ssh-login-without-password-using-ssh-keygen-ssh-copy-id/ 例5:從遠程到本地 $ rsync -avz thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenames . sent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87 例6:remote shell for synchronization rsync允許你指定你想使用的remote shell,你可以使用rsync -e ssh來enable the secured remote connection $ rsync -avz -e ssh thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/ rpm/Basenamessent 406 bytes received 15810230 bytes 2432405.54 bytes/sec total size is 45305958 speedup is 2.87 例7:不覆蓋目的地址上已經修改過的文件 典型情況下,如果一個文件在destination被修改的話,我們可能并不希望使用來自source的老文件去覆蓋修改 使用 rsync -u選項達到這個目的(即:如果目的地上修改過,那么不要覆蓋它)在下面的例子中,Basenames文件在destination上做了修改,因此如果使用-u選項,則不會被修改 $ ls -l /root/temp/Basenames total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames$ rsync -avzu thegeekstuff@192.168.200.10:/var/lib/rpm /root/temp Password: receiving file list ... done rpm/sent 122 bytes received 505 bytes 114.00 bytes/sec total size is 45305958 speedup is 72258.31$ ls -lrt total 39088 -rwxr-xr-x 1 root root 4096 Sep 2 11:35 Basenames 例8:只同步目錄tree structure(而不同步文件) 使用-d想想將只從source到destination同步文件夾的tree structure,下面的例子,只會遞歸同步目錄樹,而目錄中的文件不會同步 $ rsync -v -d thegeekstuff@192.168.200.10:/var/lib/ . Password: receiving file list ... done logrotate.status CAM/ YaST2/ acpi/sent 240 bytes received 1830 bytes 318.46 bytes/sec total size is 956 speedup is 0.46 例9:查看rsnync傳輸進度 當使用rsync來做備份時,你可能希望知道backup的進度,比如有多少個文件已經copy了,以及copy的速度等信息, rsync -progress將會打印rsync執行中的詳細信息: $ rsync -avz --progress thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... 19 files to consider ./ Basenames5357568 100% 14.98MB/s 0:00:00 (xfer#1, to-check=17/19) Conflictname12288 100% 35.09kB/s 0:00:00 (xfer#2, to-check=16/19) . . . sent 406 bytes received 15810211 bytes 2108082.27 bytes/sec total size is 45305958 speedup is 2.87 例10:刪除在Targe上創建的文件 如果在source這一側并不存在一個文件,而這個文件本身又在destination上存在,那么你可以指定刪除這個文件,-delete選項完成這個功能 # Source and target are in sync. Now creating new file at the target. $ > new-file.txt$ rsync -avz --delete thegeekstuff@192.168.200.10:/var/lib/rpm/ . Password: receiving file list ... done deleting new-file.txt ./sent 26 bytes received 390 bytes 48.94 bytes/sec total size is 45305958 speedup is 108908.55 注意:new-file.txt文件將在rsync過程中被刪除 例11:在destination(target)上不創建新文件 如果你喜歡,你可以只update(sync)那些在target上已經存在的文件。如果source有新的文件,而這個文件本身在target上并不存在,那么你可以通過-existing選項避免在destination上創建這些新文件 首先在source上創建一個new-file.txt文件 [/var/lib/rpm ]$ > new-file.txt $ rsync -avz --existing root@192.168.1.2:/var/lib/rpm/ . root@192.168.1.2's password: receiving file list ... done ./sent 26 bytes received 419 bytes 46.84 bytes/sec total size is 88551424 speedup is 198991.96 例12:查看source/destination之間的變更 $ ls -l /var/lib/rpm -rw-r--r-- 1 root root 5357568 2010-06-24 08:57 Basenames -rw-r--r-- 1 root root 12288 2008-05-28 22:03 Conflictname -rw-r--r-- 1 root root 1179648 2010-06-24 08:57 Dirnames At destination: $ ls -l /root/temp -rw-r--r-- 1 root root 0 Sep 3 06:39 Basenames -rw-r--r-- 1 root root 12288 May 28 2008 Conflictname -rw-r--r-- 1 bin bin 1179648 Jun 24 05:27 Dirnames 在這里source和destination有兩個不同。owner/group,以及size不同。 $ rsync -avzi thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/Password:receiving file list ... done>f.st.... Basenames.f....og. Dirnamessent 48 bytes received 2182544 bytes 291012.27 bytes/sectotal size is 45305958 speedup is 20.76 在上面的例子中,在Basenames, Dirnames文件的前面有一些奇怪的信息,其實它非常重要: > specifies that a file is being transferred to the local host. f represents that it is a file. s represents size changes are there. t represents timestamp changes are there. o owner changed g group changed. 例13:在文件傳輸中包含和排除Pattern rsync允許你給一個pattern,指定你希望在做同步過程中包含或者排除的文件或者目錄 $ rsync -avz --include 'P*' --exclude '*' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Packages Providename Provideversion Pubkeyssent 129 bytes received 10286798 bytes 2285983.78 bytes/sec total size is 32768000 speedup is 3.19 上面的例子中它將僅僅包含那些以P打頭的文件或者文件夾并且排除所有其他文件 例14:不傳輸大的文件 你可以告訴rsync不要傳輸大于指定大小尺寸的文件,使用-max-size選項 $ rsync -avz --max-size='100K' thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp/ Password: receiving file list ... done ./ Conflictname Group Installtid Name Sha1header Sigmd5 Triggernamesent 252 bytes received 123081 bytes 18974.31 bytes/sec total size is 45305958 speedup is 367.35 上面的例子使得rsync只傳輸那些小于100K大小的文件。你也可以指定M或G 例15:傳輸整個文件 rsync的一個重要功能是它只傳輸一個文件的變更的塊到目的地,而不是傳輸文件本省。如果網絡帶寬本身并不是什么問題,你可以傳輸整個文件,通過-Wxuanxiang 這將加速rsync的處理速度,因為他不需要再在source和destination做checksum的運算了。 # rsync -avzW thegeekstuff@192.168.200.10:/var/lib/rpm/ /root/temp Password: receiving file list ... done ./ Basenames Conflictname Dirnames Filemd5s Group Installtid Namesent 406 bytes received 15810211 bytes 2874657.64 bytes/sec total size is 45305958 speedup is 2.87 本文原文來自于: http://www.thegeekstuff.com/2010/09/rsync-command-examples/

tips:

本文由wp2Blog導入,原文鏈接:http://devonios.com/rsync-command.html

轉載于:https://my.oschina.net/yangyan/blog/859370

總結

以上是生活随笔為你收集整理的rsync 常用命令及格式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。