參考:http://www.javashuo.com/article/p-hgmuxrfn-cm.htmlhtml
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
Access via rsync daemon:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
有上述語法可知,rsync有三種工做方式:web
(1)「Local」即本地文件同步;redis
(2)「Access via remote shell」即本地主機經過遠程shell與遠程主機通訊,實現本地與遠程文件的同步;算法
(3)「Access via rsync daemon」即經過套接字的方式實現本地主機直接與遠程主機的rsync daemon進程通訊,實現本地與遠程文件的同步。shell
(1)和(2)的本質是管道通訊,(3)則是遠程主機運行rsync daemon程序,經過監聽873端口與本地主機通訊(客戶端)。bash
還有一種特殊的工做方式——經過遠程shell啓動遠程主機的一個臨時性的rsync daemon,僅用於臨時讀取daemon的配置文件,當這次rsync同步完成,遠程shell啓動的rsync daemon進程也會自動消逝。此通訊方式的命令行語法格式同(3),但要求options部分必須明確指定"--rsh"選項或其短選項"-e"。服務器
其中,第一個路徑參數必定是源文件路徑,即做爲同步基準的一方,能夠同時指定多個源文件路徑。最後一個路徑參數則是目標文件路徑,也就是待同步方。路徑的格式能夠是本地路徑,也能夠是使用user@host:path或user@host::path的遠程路徑,若是主機和path路徑之間使用單個冒號隔開,表示使用的是遠程shell通訊方式,而使用雙冒號隔開的則表示的是鏈接rsync daemon。另外,鏈接rsync daemon時,還提供了URL格式的路徑表述方式rsync://user@host/path。網絡
若是僅有一個參數,則命令至關於"ls -l"的方式列出源文件列表,而不是複製文件。app
另外,使用rsync必定要注意的一點是,源路徑若是是一個目錄的話,帶上尾隨斜線和不帶尾隨斜線是不同的,不帶尾隨斜線表示的是整個目錄包括目錄自己,帶上尾隨斜線表示的是目錄中的文件,不包括目錄自己。例如:運維
-v:顯示rsync過程當中詳細信息。可使用"-vvvv"獲取更詳細信息。
-z --compress:傳輸時進行壓縮提升效率,--compress-level=NUM可按級別壓縮。
-a --archive :歸檔模式,表示遞歸傳輸並保持文件屬性。等同於"-rtopgDl"。-r --recursive:遞歸到目錄中去。 -t --times:保持mtime屬性。強烈建議任什麼時候候都加上"-t",不然目標文件mtime會設置爲系統時間,致使下次更新檢查出mtime不一樣從而致使增量傳輸無效。 -o --owner:保持owner屬性(屬主)。 -p --perms:保持perms屬性(權限,不包括特殊權限)。
-g --group:保持group屬性(屬組)。
-D :是"--device --specials"選項的組合,即也拷貝設備文件和特殊文件。 -l --links:若是文件是軟連接文件,則拷貝軟連接自己而非軟連接所指向的對象。
-n --dry-run :僅測試傳輸,而不實際傳輸。常和"-vvvv"配合使用來查看rsync是如何工做的。 -P:顯示文件傳輸的進度信息。(實際上"-P"="--partial --progress",其中的"--progress"纔是顯示進度信息的)。
-R --relative:使用相對路徑。意味着將命令行中指定的全路徑而非路徑最尾部的文件名發送給服務端,包括它們的屬性。用法見下文示例。 --size-only :默認算法是檢查文件大小和mtime不一樣的文件,使用此選項將只檢查文件大小。 -u --update :僅在源mtime比目標已存在文件的mtime新時才拷貝。注意,該選項是接收端判斷的,不會影響刪除行爲。 -d --dirs :以不遞歸的方式拷貝目錄自己。默認遞歸時,若是源爲"dir1/file1",則不會拷貝dir1目錄,使用該選項將拷貝dir1但不拷貝file1。 --max-size :限制rsync傳輸的最大文件大小。可使用單位後綴,還能夠是一個小數值(例如:"--max-size=1.5m") --min-size :限制rsync傳輸的最小文件大小。這能夠用於禁止傳輸小文件或那些垃圾文件。 --exclude :指定排除規則來排除不須要傳輸的文件。 --delete :以SRC爲主,對DEST進行同步。多則刪之,少則補之。注意"--delete"是在接收端執行的,因此它是在exclude/include規則生效以後才執行的。 -b --backup :對目標上已存在的文件作一個備份,備份的文件名後默認使用"~"作後綴。 --backup-dir:指定備份文件的保存路徑。不指定時默認和待備份文件保存在同一目錄下。 -e :指定所要使用的遠程shell程序,默認爲ssh。 --port :鏈接daemon時使用的端口號,默認爲873端口。 --password-file:daemon模式時的密碼文件,能夠從中讀取密碼實現非交互式。注意,這不是遠程shell認證的密碼,而是rsync模塊認證的密碼。 -W --whole-file:rsync將再也不使用增量傳輸,而是全量傳輸。在網絡帶寬高於磁盤帶寬時,該選項比增量傳輸更高效。 --existing :要求只更新目標端已存在的文件,目標端還不存在的文件不傳輸。注意,使用相對路徑時若是上層目錄不存在也不會傳輸。 --ignore-existing:要求只更新目標端不存在的文件。和"--existing"結合使用有特殊功能,見下文示例。 --remove-source-files:要求刪除源端已經成功傳輸的文件。
參考:http://www.cnblogs.com/f-ck-need-u/p/7221713.html
(1)將/etc/fstab拷貝到/tmp目錄下
[root@backup ~]# rsync /etc/fstab /tmp
(2)將/etc/cron.d目錄拷貝到/tmp下
[root@backup ~]# rsync -r /etc/cron.d /tmp
(3)將/etc/cron.d目錄拷貝到/tmp下,但要求在/tmp下也生成etc子目錄
[root@backup ~]# rsync -R -r /etc/cron.d /tmp
其中"-R"選項表示使用相對路徑,此相對路徑是以目標目錄爲根的。對於上面的示例,表示在目標上的/tmp下建立etc/cron.d目錄,即/tmp/etc/cron.d,etc/cron.d的根"/"表明的就是目標/tmp。
若是要拷貝的源路徑較長,但只想在目標主機上保留一部分目錄結構,例如要拷貝/var/lib/yum/yumdb/*到/tmp下,但只想在/tmp下保留從yum開始的目錄,如何操做?使用一個點表明相對路徑的起始位置便可,也就是將長目錄進行劃分。
[root@backup ~]# rsync -R -r /var/lib/./yum/yumdb/ /tmp
這樣,從點開始的目錄都是相對路徑,其相對根目錄爲目標路徑。因此對於上面的示例,將在目標上建立"/tmp/yum/yumdb/"。
(4)對遠程目錄下已存在文件作一個備份
[root@backup ~]# rsync -R -r --backup /var/lib/./yum/yumdb/ /tmp
這樣在目標目錄下,已存在的文件就被作一個備份,備份文件默認使用"~"作後綴,可使用"--suffix"指定備份後綴。
[root@backup ~]# ls -l /tmp/yum/yumdb/a/09e97db56b022f05de9d57fc7d1e8d2caaff5a93-authconfig-6.1.12-23.el6-x86_64 total 64 -rw-r--r-- 1 root root 64 May 31 17:17 checksum_data -rw-r--r-- 1 root root 64 May 31 17:13 checksum_data~ -rw-r--r-- 1 root root 6 May 31 17:17 checksum_type -rw-r--r-- 1 root root 6 May 31 17:13 checksum_type~ -rw-r--r-- 1 root root 35 May 31 17:17 from_repo -rw-r--r-- 1 root root 35 May 31 17:13 from_repo~ -rw-r--r-- 1 root root 10 May 31 17:17 from_repo_revision -rw-r--r-- 1 root root 10 May 31 17:13 from_repo_revision~ -rw-r--r-- 1 root root 10 May 31 17:17 from_repo_timestamp -rw-r--r-- 1 root root 10 May 31 17:13 from_repo_timestamp~ -rw-r--r-- 1 root root 10 May 31 17:17 installed_by -rw-r--r-- 1 root root 10 May 31 17:13 installed_by~ -rw-r--r-- 1 root root 4 May 31 17:17 reason -rw-r--r-- 1 root root 4 May 31 17:13 reason~ -rw-r--r-- 1 root root 3 May 31 17:17 releasever -rw-r--r-- 1 root root 3 May 31 17:13 releasever~
可使用"--backup-dir"指定備份文件保存路徑,但要求保存路徑必須存在。
[root@backup ~]# mkdir /tmp/log_back [root@backup ~]# rsync -R -r --backup --backup-dir=/tmp/log_back /var/lib/./yum/yumdb/ /tmp [root@backup ~]# ls -l /tmp/log_back/yum/yumdb/a/09e97db56b022f05de9d57fc7d1e8d2caaff5a93-authconfig-6.1.12-23.el6-x86_64 total 32 -rw-r--r-- 1 root root 64 May 31 17:17 checksum_data -rw-r--r-- 1 root root 6 May 31 17:17 checksum_type -rw-r--r-- 1 root root 35 May 31 17:17 from_repo -rw-r--r-- 1 root root 10 May 31 17:17 from_repo_revision -rw-r--r-- 1 root root 10 May 31 17:17 from_repo_timestamp -rw-r--r-- 1 root root 10 May 31 17:17 installed_by -rw-r--r-- 1 root root 4 May 31 17:17 reason -rw-r--r-- 1 root root 3 May 31 17:17 releasever
指定備份路徑後,默認將不會加備份後綴,除非使用"--suffix"顯式指定後綴,如"--suffix=~"。
(5)指定ssh鏈接參數,如端口、鏈接的用戶、ssh選項等
[root@xuexi tmp]# >~/.ssh/known_hosts # 先清空host key以便下面的測試 [root@xuexi tmp]# rsync -e "ssh -p 22 -o StrictHostKeyChecking=no" /etc/fstab 172.16.10.5:/tmp Warning: Permanently added '172.16.10.5' (RSA) to the list of known hosts. root@172.16.10.5's password:
(6)"--existing"之同步存在的和"--ignore-existing"之同步不存在的
目前/tmp/{a,b}目錄中內容以下,bashrc在a目錄中,crontab在b目錄中,且a目錄中多了一個c子目錄。
使用"--existing"選項可實現只更新目標端已存在的文件的目標。
[root@backup ~]# rsync -r -v --existing /tmp/a/ /tmp/b sending incremental file list fstab profile rc.local sent 3112 bytes received 70 bytes 6364.00 bytes/sec total size is 5561 speedup is 1.75
結果只有3個目標上已存在的文件被更新了,因爲目標上沒有c目錄,因此c目錄中的文件也沒有進行傳輸。
而"--ignore-existing"是更新目標端不存在的文件。
[root@backup ~]# rsync -r -v --ignore-existing /tmp/a/ /tmp/b sending incremental file list bashrc c/ c/find sent 2905 bytes received 54 bytes 5918.00 bytes/sec total size is 5561 speedup is 1.88
"--existing"和"--ignore-existing"結合使用時,有個特殊功效,當它們結合"--delete"使用的時候,文件不會傳輸,但會刪除receiver端額外多出的文件。
[root@backup ~]# cd /tmp [root@backup tmp]# mkdir -p d/{a,b} [root@backup tmp]# echo "hello,world." >d/b/a.log [root@backup tmp]# awk 'BEGIN{for(i=1;i<=4;i++)print "echo hello,stranger. >d/a/"i".txt"}'|bash
[root@backup tmp]# rsync -nrv --delete d/a/ d/b/ sending incremental file list deleting a.log 1.txt 2.txt 3.txt 4.txt sent 80 bytes received 24 bytes 208.00 bytes/sec total size is 64 speedup is 0.62 (DRY RUN)
[root@backup tmp]# rsync -nrv --existing --ignore-existing --delete d/a/ d/b/
sending incremental file list
deleting a.log
sent 68 bytes received 12 bytes 160.00 bytes/sec
total size is 64 speedup is 0.80 (DRY RUN)
實際上,"--existing"和"--ingore-existing"是傳輸規則,只會影響receiver要求讓sender傳輸的文件列表,在receiver決定哪些文件須要傳輸以前的過程,是這兩個選項沒法掌控的,因此各類規則、"--delete"等操做都不會被這兩個選項影響。
(7)"--remove-source-files"刪除源端文件
使用該選項後,源端已經更新成功的文件都會被刪除,源端全部未傳輸或未傳輸成功的文件都不會被移除。未傳輸成功的緣由有多種,如exclude排除了,"quick check"未選項該文件,傳輸中斷等等。
總之,顯示在"rsync -v"被傳輸列表中的文件都會被移除。以下:
[root@xuexi ~]# rsync -r -v --remove-source-files /tmp/a/anaconda /tmp/a/audit /tmp sending incremental file list anaconda/anaconda.log anaconda/ifcfg.log anaconda/journal.log anaconda/ks-script-1uLekR.log anaconda/ks-script-iGpl4q.log anaconda/packaging.log anaconda/program.log anaconda/storage.log anaconda/syslog audit/audit.log sent 4806915 bytes received 204 bytes 9614238.00 bytes/sec total size is 4805676 speedup is 1.00
上述顯示出來的文件在源端所有被刪除。
使用"--exclude"選項指定排除規則,排除那些不須要傳輸的文件。
[root@backup tmp]# rsync -r -nv --exclude="anaconda/*.log" /var/log/anaconda /var/log/audit /tmp sending incremental file list anaconda/ anaconda/syslog audit/ audit/audit.log sent 3365629 bytes received 58 bytes 6731374.00 bytes/sec total size is 3365016 speedup is 1.00
上例中只排除了anaconda目錄中的log文件,可是audit目錄中的log文件是正常傳輸的。
注意,一個"--exclude"只能指定一條規則,要指定多條排除規則,須要使用多個"--exclude"選項,或者將排除規則寫入到文件中,而後使用"--exclude-from"選項讀取該規則文件。
另外,除了"--exclude"排除規則以外,"--include"包含規則——篩選出要進行傳輸的文件,因此include規則也稱爲傳輸規則。它的使用方法和"--exclude"同樣。若是一個文件既能匹配排除規則,又能匹配包含規則,則先匹配到的當即生效,生效後就再也不進行任何匹配。
最後,關於規則,最重要的一點是它的做用時間。當發送端敲出rsync命令後,rsync將當即掃描命令行中給定的文件和目錄(掃描過程當中還會按照目錄進行排序,將同一個目錄的文件放在相鄰的位置),這稱爲拷貝樹(copy tree),掃描完成後將待傳輸的文件或目錄記錄到文件列表中,而後將文件列表傳輸給接收端。而篩選規則的做用時刻是在掃描拷貝樹時,因此會根據規則來匹配並決定文件是否記錄到文件列表中(嚴格地說是會記錄到文件列表中的,只不過排除的文件會被標記爲hide隱藏起來),只有記錄到了文件列表中的文件或目錄纔是真正須要傳輸的內容。換句話說,篩選規則的生效時間在rsync整個同步過程當中是很是靠前的,它會影響不少選項的操做對象,最典型的如"--delete"。也許,你看完這一整篇文章都沒感受到這一點的重要性,但若是你閱讀rsync的man文檔或者學習rsync的原理,你必定會深有體會。
實際上,排除規則和包含規則都只是"--filter"篩選規則的兩種特殊規則。詳見。
有如下兩個目錄,其文件見下圖:
目錄/root/anaconda和/tmp/anaconda的文件的mtime不相同,其中有四個相同文件。進行如下操做:
[root@backup ~]# rsync -vrn --delete /root/anaconda /tmp sending incremental file list deleting anaconda/fstab deleting anaconda/anaconda.yum.bak deleting anaconda/anaconda.storage.bak deleting anaconda/anaconda.program.bak anaconda/anaconda.ifcfg.log anaconda/anaconda.log anaconda/anaconda.program.log anaconda/anaconda.storage.log anaconda/anaconda.syslog anaconda/anaconda.xlog anaconda/anaconda.yum.log sent 166 bytes received 34 bytes 400.00 bytes/sec total size is 406329 speedup is 2031.64 (DRY RUN)
由以上可知:rsync 有--delete選項後,會刪除目標目錄中存在而源目錄中不存在的文件,並將源目錄中的文件同步到目標目錄(同步,包含了mtime不一樣的相同文件)。
這樣的行爲實現了遠程刪除的功能,對於做用於本地的rsync,也就實現了rm的本地刪除功能。並且,若是使用空目錄做爲源目錄,則它的做用是清空目錄上的整個目錄。
含有--delete選項的文件同步的原理:
在發送端將源文件列表發送給接收端後,接收端的generator進程會掃描目標目錄造成目標文件列表,而後對兩個列表中的信息條目都計算數據塊校驗碼,最後將數據塊校驗碼發給發送端,發送端經過校驗碼來匹配哪些數據塊是須要傳輸的,這樣就實現了增量傳輸的功能——只傳輸改變的部分,不會傳輸整個文件。而rsync中--delete選項生效的時間點是generator對兩個列表進行對比,生成校驗碼以前進行的,先將目標上存在但源上不存在的多餘文件刪除,這樣就無需爲多餘的文件生成校驗碼。
若是將"--delete"選項和"--exclude"選項一塊兒使用,則被排除的文件不會被刪除。例如:
[root@backup ~]# rsync -vrn --exclude="*.bak" --delete /root/anaconda /tmp sending incremental file list deleting anaconda/fstab anaconda/anaconda.ifcfg.log anaconda/anaconda.log anaconda/anaconda.program.log anaconda/anaconda.storage.log anaconda/anaconda.syslog anaconda/anaconda.xlog anaconda/anaconda.yum.log sent 166 bytes received 34 bytes 400.00 bytes/sec total size is 406329 speedup is 2031.64 (DRY RUN)
更多的關於--delete選項的說明,見。
既然rsync經過遠程shell就能實現兩端主機上的文件同步,還要使用rsync的服務幹啥?試想下,你有的機器上有一堆文件須要時不時地同步到衆多機器上去,好比目錄a、b、c是專門傳輸到web服務器上的,d/e、f、g/h是專門傳輸到ftp服務器上的,還要對這些目錄中的某些文件進行排除。經過遠程shell只能經過運維逐條命令執行,不能實現批量、定時的任務管理,影響運維效率。rsync daemon就解決了這些問題,同時還能夠向外提供服務,只要告訴了別人rsync的url路徑,外人就能向ftp服務器同樣獲取文件列表並進行選擇性地下載,因此,你所制定的列表,你的同事也能夠獲取到並使用。
rsync daemon進程執行「rsync --daemon」命令便可開啓。rsync daemon進程的配置文件默認爲/etc/rsyncd.conf(該文件須要人爲建立),rsync daemon進程端口默認爲873.
rsync daemon的通訊方式和傳輸通道與遠程shell不一樣:
遠程shell是經過管道鏈接的方式進行數據傳輸的,無需事先開啓rsync服務;
rsync經過socket套接字的方式進行,須要開啓rsync daemon進程,經過873端口監聽客戶端的請求。
如下是rsync client鏈接rsync daemon時的命令語法:
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST] rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST::DEST rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST
鏈接命令有兩種類型,一種是rsync風格使用雙冒號的"rsync user@host::src dest",一種是url風格的"rsync://user@host:port/src dest"。對於rsync風格的鏈接命令,若是想要指定端口號,則須要使用選項"--port"。
上述語法中,其中daemon端的路徑,如user@host::src,它的src表明的是模塊名,而不是真的文件系統中的路徑(文件路徑是配置文件模塊src下的path)。
The rsyncd.conf file is the runtime configuration file for rsync when run as an rsync daemon. The rsyncd.conf file controls authentication, access, logging and available modules.
以上是man syncd,conf描述。rsyncd.conf是rsync daemon進程運行時的配置文件。文件rsyncd.conf的配置決定了訪問權限、訪問受權、日誌功能和可訪問的模塊。
如下是一個syncd.conf的配置文件模板:
[root@backup ~]# cat /etc/rsyncd.conf # /etc/rsyncd: configuration file for rsync daemon mode # See rsyncd.conf man page for more options. # configuration example: # uid = nobody # gid = nobody # use chroot = yes # max connections = 4 # pid file = /var/run/rsyncd.pid # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp1] # path = /home/ftp # comment = ftp export area
在上述示例配置文件中,先定義了一些全局選項,而後定義了[ftp1],這個用中括號包圍的"[ftp1]"就是rsync中所謂的模塊,ftp1爲模塊ID,必須保證惟一,每一個模塊中必須定義一項"path",path定義的是該模塊表明的路徑,例如此示例文件中,若是想請求ftp1模塊,則在客戶端使用"rsync user@host::ftp1",這表示訪問user@host上的/home/ftp目錄,若是要訪問/home/ftp目錄下的子目錄www,則"rsync user@host::ftp1/www"。
如下是常見的配置項,也算是一個配置示例:
# rsyn_conf ____________________start # create by ant 190601 # /etc/rsyncd.conf start port = 888 # rsync的端口,默認爲873 uid = rsync # rsync服務的運行用戶,默認爲nobody,文件傳輸完成後文件的屬主就是這個uid gid = rsync # rsync服務的運行組,默認爲nobody,文件傳輸完成後文件的屬組就是這個uid use chroot = no # rsync daemon在傳輸前是否切換到指定的path目錄下,並將其監禁在內 max connections = 200 # 最大鏈接數,0表示沒限制 timeout = 300 # rsync服務器超時時間,0表示永不超時 pid file = /var/run/rsyncd.pid # rsync daemon的pid文件 lock file = /var/run/rsync.lock # 鎖文件 log file = /var/log/rsyncd.log # 指定rsync的日誌文件,避免日誌發送給syslog ignore errors # 忽略某些IO錯誤 read only = false # 指定模塊是否可讀寫,false表示可讀寫(是否可上傳文件),true表示只讀 write only = false # 指定模塊是否支持下載,true表示客戶端不能下載 list = false # 客戶端是否可顯示(列表)模塊下的文件 hosts allow = 172.16.1.0/24 # 容許鏈接到指定模塊的主機,多個不一樣ip用空格隔開或者設置區間 hosts deny = 0.0.0.0/32 # 禁止鏈接指定模塊的主機 auth users = rsync_backup # 指定鏈接到該模塊的用戶列表,只有列表裏的用戶才能鏈接到模塊,用戶名和對應密碼保存在secrts file中,這裏使用的
# 不是系統用戶,而是虛擬用戶.不設置時,默認全部用戶都能鏈接,但使用的是匿名鏈接 secrets file = /etc/rsync.password # 保存auth users用戶列表的用戶名和密碼,每行包含一個username:passwd.因爲"strict modes"默認爲true,因此
# 此文件要求非rsync daemon用戶不可讀寫。只有啓用了auth users該選項纔有效。 [backup] # 模塊backup comment = "backup dir by ant" # 說明 path = /backup # 模塊對應的目錄 [test] # 模塊test comment = "nfsbackup dir by ant" path = /test # rsync_config______________end
注意事項:
(1)從客戶端推到服務端的文件的屬主和屬組分別是配置文件中指定的uid和gid;客戶端從服務端拉來的文件的屬主和屬組是客戶端正在執行rsync程序的當前用戶;
(2).auth users和secrets file這兩個參數是非必須的,省略時默認使用匿名鏈接;這兩個參數一旦配置,secrets file參數對應的文件權限必須是600,對應的客戶端的密碼文件也必須是600。
[root@backup ~]# rpm -qa | grep rsync rsync-3.0.6-12.el6.x86_64
[root@backup ~]# rsync --version rsync version 3.0.6 protocol version 30 Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others. Web site: http://rsync.samba.org/ Capabilities: 64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints, socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace, append, ACLs, xattrs, iconv, symtimes rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU General Public Licence for details.
[root@backup ~]# useradd -s /sbin/nologin -M rsync [root@backup ~]# id rsync uid=501(rsync) gid=501(rsync) groups=501(rsync)
見上述 ———— 3.2 rsync 的 daemon模式下的配置文件/etc/rsyncd.conf
[root@backup ~]# echo "rsync_backup:backup123" >/etc/rsync.password [root@backup ~]# cat /etc/rsync.password rsync_backup:root123
[root@backup ~]# chmod 600 /etc/rsync.password [root@backup ~]# ls -l /etc/rsync.password -rw------- 1 root root 21 Jun 2 09:26 /etc/rsync.password
[root@backup ~]# mkdir -p /backup /test [root@backup ~]# chown rsync.rsync /backup [root@backup ~]# chown rsync.rsync /test [root@backup ~]# ls -ld /backup /test drwxr-xr-x 2 rsync rsync 4096 Jun 2 09:33 /backup drwxr-xr-x 2 rsync rsync 4096 Jun 2 09:33 /backup
[root@backup ~]# ps -ef|grep rsync|grep -v grep root 1792 1 0 09:35 ? 00:00:00 rsync --daemon [root@backup ~]# lsof -i :873 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 1792 root 3u IPv4 11946 0t0 TCP *:rsync (LISTEN) rsync 1792 root 5u IPv6 11947 0t0 TCP *:rsync (LISTEN) [root@backup ~]# netstat -lntup|grep 873 tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1792/rsync tcp 0 0 :::873 :::* LISTEN 1792/rsync
[root@backup ~]# echo "/usr/bin/rsync --daemon" >>/etc/rc.local [root@backup ~]# tail -1 /etc/rc.local /usr/bin/rsync --daemon
[root@nfs01 ~]# rpm -qa|grep rsync rsync-3.0.6-12.el6.x86_64 [root@nfs01 ~]# echo "backup123" >/etc/rsync.password [root@nfs01 ~]# cat /etc/rsync.password backup123
[root@nfs01 ~]# chmod 600 /etc/rsync.password [root@nfs01 ~]# ls -l /etc/rsync.password -rw------- 1 root root 10 Jun 2 11:54 /etc/rsync.password
++++++++++++++++ 服務端 +++++++++++++++++
一、查看rsync程序是否安裝 rpm -qa | grep rsync rsync --version 二、添加rsync服務的用戶,管理本地目錄 id rsync useradd -s /sbin/nologin -M rsync id rsync 三、生成rsync daemon的配置文件/etc/rsyncd.conf 四、根據auth user參數建立secret file參數對應的密碼文件 echo "rsync_backup:backup123" >/etc/rsync.password cat /etc/rsync.password 五、給密碼文件設置權限 chmod 600 /etc/rsync.password ls -l /etc/rsync.password 六、建立文件共享目錄並受權rsync服務管理 mkdir -p /backup chown rsync.rsync /backup ls -ld /backup 七、啓動rsync daemon服務並檢查端口是否開啓 ps -ef|grep rsync|grep -v grep lsof -i :873 netstat -lntup|grep 873 八、將rsync daemon服務加入開機自啓動 echo "/usr/bin/rsync --daemon" >>/etc/rc.local tail -1 /etc/rc.local
++++++++++++++++ 客戶端 +++++++++++++++++
一、把服務端密碼文件的密碼寫入本地/etc/rsync.password echo "backup123" >/etc/rsync.password cat /etc/rsync.password 二、配置密碼文件的權限 chmod 600 /etc/rsync.password
[root@nfs01 ~]# rsync -avz /etc/rc.local rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password sending incremental file list rc.local -> rc.d/rc.local sent 46 bytes received 11 bytes 38.00 bytes/sec total size is 13 speedup is 0.23 [root@nfs01 ~]# rsync -avz /etc/yum.conf rsync://rsync_backup@172.16.1.41/test/ --password-file=/etc/rsync.password sending incremental file list yum.conf sent 643 bytes received 27 bytes 1340.00 bytes/sec total size is 969 speedup is 1.45 ######################################### [root@backup ~]# ls -l /backup/ /test/ /backup/: total 0 lrwxrwxrwx 1 rsync rsync 28 Feb 23 12:48 rc.local -> /rsyncd-munged/rc.d/rc.local /test/: total 4 -rw-r--r-- 1 rsync rsync 969 Jul 24 2015 yum.conf
問題一: [root@nfs tmp]# rsync -avz /etc/hosts rsync@172.16.1.41::backup Password: @ERROR: auth failed on module backup rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 問題緣由: ① 認證的用戶名或者密碼輸入的不正確 ② 認證文件編寫有問題(認證內容信息) ③ 認證文件的權限設置的不正確(權限爲600,文件用戶和用戶組仍是root不變) ④ rsync服務端配置文件中指定的認證文件路徑與實際認證文件路徑不符 問題二: [root@nfs01~]#rsync -avz /etc/hosts rsync_backu@172.16.1.41::backup --password-file=/etc/rsync.password rsync: failed to connect to 172.16.1.41: Connection refused (111) rsync error: error in socket IO (code 10) at clientserver.c(124) [sender=3.0.6] 問題緣由: ① rsync服務沒有啓動 問題三: rsync -avz /etc/sysconfig/network rsync_backup@172.16.1.41::nfsbackup --password-file=/etc/rsync.password @ERROR: chdir failed rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6] 解決緣由: ① rsync服務端沒有模塊指定的目錄,須要進行建立和受權