rysnc(remote synchronize)在CentOS系統默認安裝在/usr/bin,此外rysnc在windows平台下也有相應版本。主頁地址為: http://rsync.samba.org/
概述
Rsync(remote synchronize) 遠程同步工具,基於SSH同步本地和遠程主機之間的文件。
好處:只同步兩個文件不同的部分,相同的部分不在傳遞。類似於增量備份,
這使得在服務器傳遞備份文件或者同步文件,比起scp工具要省好多時間。
ubuntu默認安裝了rsync:
默認的配置文件位置:/usr/share/doc/rsync/examples
配置文件名稱:rsyncd.conf
需要將其復制到 /etc/ 下
rsync默認是沒有啟動的,可通過ps -e |grep rsync查看
可在 /etc/init.d 路徑下執行:
./rsync start #啟動
./rsync start #關閉
可在 /etc/default 路徑下的 rsync文件中將其改為自啟動
將 RSYNC_ENABLE=true
默認配置文件詳解 rsyncd.conf
# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd file=/etc/motd #歡迎信息,需要這玩意的話,可以在前面指定目錄下建立指定名稱的文件,將想當做歡迎語的信息寫在里面,即可! #log file=/var/log/rsyncd #日志文件的路徑及名稱,當配置后,rsync不能正確工作時,需要查看該文件 # for pid file, do not use /var/run/rsync.pid if # you are going to run rsync out of the init.d script. # The init.d script does its own pid file handling, # so omit the "pid file" line completely in that case. # pid file=/var/run/rsyncd.pid #syslog facility=daemon #指定rsync發送日志消息給syslog時的消息級別,常見的消息級別是:uth, authpriv, cron, daemon, ftp, kern, lpr, mail, news, security, #sys-log, user, uucp, local0, local1, local2, local3,local4, local5, local6和local7。默認值是daemon。 #socket options= #自定義tcp選項,默認是關閉的 # MODULE OPTIONS #以下是模塊信息,我們可以創建多個模塊 [ftp] #模塊的名稱可以自定義 comment = public archive #模塊描述 path = /var/www/pub #需要同步的路徑 use chroot = yes #默認是yes|true,如果為true,那么在rsync在傳輸文件以前首先chroot到path參數指定的目錄下。這樣做的原因是實現額外的安全 防護,但是缺點是需要 #root權限,並且不能備份指向外部的符號連接指向的目錄文件。 # max connections=10 #最大連接數 lock file = /var/lock/rsyncd #指定支持max connections參數的鎖文件 # the default for read only is yes... read only = yes list = yes #客戶請求時可用模塊是否列出該模塊 uid = nobody #設定該模塊傳輸文件時守護進程應該具有的uid gid = nogroup #設定該模塊傳輸文件時守護進程應具有的gid,此項與uid配合可以確定文件的訪問權限 # exclude =
#用來指定多個由空格隔開的多個模式列表,並將其添加到exclude列表中。這等同於在客戶端命令中使用--exclude來指定模式,不過配置文件中 指定的exlude模式不會傳遞給客戶 #端,而僅僅應用於服務器。一個模塊只能指定一個exlude選項,但是可以在模式前面使用"-"和"+"來指定是 exclude還是include
# exclude from = #可以指定一個包含exclude模式定義的文件名 # include = # include from = # auth users =
#該選項指定由空格或逗號分隔的用戶名列表,只有這些用戶才允許連接該模塊。這里的用戶和系統用戶沒有任何關系。如果"auth users"被設置,那么客戶端發出對該模塊的連接請求 #以后會被rsync請求challenged進行驗證身份這里使用的 challenge/response認證協議。用戶的名和密碼以明文方式存放在"secrets file"選項指定的文件中。默認情況下無 #需密碼就可以連接模塊(也就是匿名方式)
# secrets file = /etc/rsyncd.secrets
#該文件每行包含一個username:password對,以明文方式存儲,只有在auth users被定義時,此選項才生效。同時我們需要將此文件權限設置為0600
strict modes = yes
#該選項指定是否監測密碼文件的權限,如果該選項值為true那么密碼文件只能被rsync服務器運行身份的用戶訪問,其他任何用戶不可以訪問該文件。默認值為true
# hosts allow = #允許的主機IP # hosts deny = #拒絕訪問的主機IP ignore errors = no #設定rsync服務器在運行delete操作時是否忽略I/O錯誤 ignore nonreadable = yes #設定rysnc服務器忽略那些沒有訪問文件權限的用戶 transfer logging = no #使rsync服務器使用ftp格式的文件來記錄下載和上載操作在自己單獨的日志中 # log format = %t: host %h (%a) %o %f (%l bytes). Total %b bytes. #設定日志格式 timeout = 600 #超時設置(秒) refuse options = checksum dry-run #定義一些不允許客戶對該模塊使用的命令選項列表 dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
補充:
日志格式選項列表:
%h:遠程主機名
%a:遠程IP地址
%l:文件長度字符數
%p:該次rsync會話的進程id
%o:操作類型:"send"或"recv"、”del.”
%f:文件名
%P:模塊路徑
%m:模塊名
%t:當前時間
%u:認證的用戶名(匿名時是null)
%b:實際傳輸的字節數
%c:當發送文件時,該字段記錄該文件的校驗碼
配置舉例
服務器:192.168.31.62
客戶端:192.168.31.63
1.先說服務器端:
# sample rsyncd.conf configuration file # GLOBAL OPTIONS #motd file=/etc/motd log file=/var/log/rsyncd # for pid file, do not use /var/run/rsync.pid if # you are going to run rsync out of the init.d script. # The init.d script does its own pid file handling, # so omit the "pid file" line completely in that case. pid file=/var/run/rsyncd.pid syslog facility=daemon #socket options= # MODULE OPTIONS [ftp] comment = public archive path = /opt/rsync use chroot = no lock file = /var/lock/rsyncd read only = yes list = yes uid = nobody gid = nogroup auth users = scdev secrets file = /etc/rsyncd.secrets strict modes = yes hosts allow = 192.168.31.63 ignore errors = yes ignore nonreadable = yes transfer logging = yes timeout = 600 refuse options = checksum dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz
本例配置使用了驗證權限的配置,需要建立
/etc/rsyncd.secrets
scdev:passwd
$sudo chmod 0600 /etc/rsyncd.secrets
2.客戶端
同服務端一樣,只不過,rsyncd.conf一般設為空文件,即可
密碼文件也要創建,只不過里面只寫密碼
passwd
$sudo chmod 0600 /etc/rsyncd.secrets
至此,配置就完成了!
3.在客戶端執行
rsync -vzrtopg --progress scdev@scdev1005::ftp /opt/rsync
提示輸入密碼:#輸入你密碼驗證文件中的密碼即可
完成同步!
也可以不輸入密碼,通過指定密碼文件的方式
rsync -vzrtopg --delete --progress scdev@scdev1005::ftp /opt/rsync --password-file=/etc/rsyncd.secrets
完成同步!
可以自己寫個腳本myRsync.sh,省得每次都要輸命令。
#!/usr/bin/env bash echo "welcome use myrsync.sh" sudo rsync -vzrtopg scdev@scdev1005::ftp /opt/rsync --password-file=/etc/rsyncd.secrets echo "Done !"
一定要給予可執行的權限:chmod +x myRsync.sh
以后直接執行myRsync.sh就好。其實最好的方法是利用crontab 等命令做自動的執行!
詳細補充:
常用的兩種方式:
1.從遠程rsync服務器中拷貝文件到本地機。當SRC路徑信息包含"::"分隔符時啟動該模式。
如:rsync -av root@172.16.78.192::www /databack
2.從本地機器拷貝文件到遠程rsync服務器中。當DST路徑信息包含"::"分隔符時啟動該模式。
如:rsync -av /databack root@172.16.78.192::www
-vzrtopg解釋:
v是verbose,z是壓縮,r是recursive,topg都是保持文件原有屬性如屬主、時間的參數(也可以用直接用a來代替rtopg, a為 --archive 歸檔模式,表示以遞歸方式傳輸
文件,並保持所有文件屬性,等於-rlptgoD)。
--progress解釋:
是指顯示出詳細的進度情況,
--delete解釋:
是指如果服務器端刪除了這一文件,那么客戶端也相應把文件刪除,保持真正的一致。
--exclude解釋:
Here are some exclude/include examples:
--exclude "*.o" would exclude all filenames matching *.o
--exclude "/foo" would exclude a file in the base directory called foo
--exclude "foo/" would exclude any directory called foo.
--exclude "/foobar" would exclude any file called bar two or more levels below a base directory called foo.
--include "*/" --include "*.c" --exclude "*" would include all directories and C source files
--include "foo/" --include "foo/bar.c" --exclude "*" would include only foo/bar.c
(the foo/ directory must be explicitly included or it would be excluded by the "*")
例如:/usr/bin/rsync -vzrtopg --delete --exclude "res/" --exclude "default.properties" --progress rsync@192.168.8.126::test /usr/local/share/rsync_backup --password-
file=/etc/rsync.pass#把服務器的/home/hyj/workspace/test文件夾中的內容備份到客戶端的/usr/local/share/rsync_backup中,不包括:res目錄和default.properties文件
rsync 命令常用參數
-a 遞歸方式傳輸文件,並保持文件屬性,等價-rlptgoD不包含-H
-r 子目錄以遞歸模式處理
-l 保持符號鏈接文件
-H 保持硬鏈接文件
-p 保持文件權限
-t 保持文件時間信息
-g 保持文件歸屬組信息
-o 保持文件歸屬用戶信息
-D 保持設備文件和特殊文件
-z 在傳輸文件時進行壓縮處理
--exclude=PATTERN 指定排除一個不需要傳輸文件匹配模式
--exclude-from=FILE 從FILE中讀取排除規則
--include=PATTERN 指定需要傳輸的文件匹配模式
--include-from=FILE 從FILE中讀取匹配規則
--copy-unsafe-links 復制指向復制源路徑目錄以外的鏈接文件
--safe-links 忽略指向復制源路徑目錄以外的鏈接文件(默認)
--existing 僅更新那些已經存在於接收端的文件,而不復制新創建文件
--ignore-existing 忽略那些已經存在於接收端的文件,只復制新創建文件
-b 當有變化時,對目標目錄中的舊文件進行備份
--backup-dir=DIR 與-b結合使用,指定備份路徑
--link-dest=DIR 當文件未改變時在指定路徑創建硬鏈接文件
--delete 刪除接收端還存在而保存端不存在的文件
--delete-before 接收端在傳輸之前進行刪除操作(默認)
--delete-during 接收端在傳輸過程中進行刪除操作
--delete-after 接收端在傳輸之后進行刪除操作
--delete-excluded 接收端在傳輸同時進行刪除操作
--e,--rsh=COMMAND 指定代替rsh的shell程序,例如可以指定為ssh
--ignore-erros 即使出現I/O錯誤也要進行刪除
--partial 保留因故沒有完全傳輸的文件,以加快隨后的再次傳輸
--progress 在傳輸時顯示傳輸過程
-p 等價於—partial—progress
--delay-updates 將正在更新的文件先保存到.~tmp~臨時目錄,待傳輸完畢再更新目標文件
-v,--verbose 詳細輸出模式
-q,--quiet 精簡輸出模式
-h,--human-readable 輸出文件大小
-n,--dry-run 顯示那些文件將要被傳輸
--list-only 僅列出文件而不進行復制
--rsync-path=PROGRAM 指定遠程服務器上的rsync命令所在路徑
--password-file=FILE 指定從FILE中讀取口令,避免在終端中輸入口令
-4,-6 使用IPv4或者IPv6
--version 打印版本信息
--help 顯示幫組信息
注意: 若用普通用戶身份運行rsync命令,同步后的文件的屬主將改變為這個普通用戶身份;若用超級用戶身份運行rsync命令同步后文件屬性保持原來用戶身份不變。若指定-tg但目標計算機沒有指定用戶和組,則復制的文件歸入501用戶 501組