新聞中心
rsync全稱remote sync,是一種更高效、可以本地或遠程同步的命令,之所以高效是因為rsync會對需要同步的源和目的進度行對比,只同步有改變的部分,所以比scp命令更高效,但是rsync本身是一種非加密的傳輸,可以借助-e選項來設(shè)置具備加密功能的承載工具進行加密傳輸,下面為大家分享一下搭建rsync服務(wù)器具體步驟。

img
系統(tǒng)環(huán)境
系統(tǒng)平臺:CentOS 6
rsync 版本:rsync-3.0.9-2.el6.rfx.x86_64.rpm
rsync 服務(wù)器:TS-DEV (172.16.1.135)
rsync 客戶端:TS-CLIENT (172.16.1.136)
服務(wù)器端安裝rsync服務(wù)
檢查rsync 是否已經(jīng)安裝
# rpm -qa|grep rsync
下載RPM包
# wget http://pkgs.repoforge.org/rsync/rsync-3.0.9-2.el6.rfx.x86_64.rpm
安裝rsync
# rpm -ivh rsync-3.0.9-2.el6.rfx.x86_64.rpm
配置 rsync 服務(wù)
配置 rsync 服務(wù)器的步驟
?首先要選擇服務(wù)器啟動方式 ?對于負荷較重的 rsync 服務(wù)器應(yīng)該使用獨立運行方式 ?對于負荷較輕的 rsync 服務(wù)器可以使用 xinetd 運行方式 ?創(chuàng)建配置文件 rsyncd.conf ?對于非匿名訪問的 rsync 服務(wù)器還要創(chuàng)建認證口令文件
以 xinetd 運行 rsync 服務(wù)
CentOS 默認以 xinetd 方式運行 rsync 服務(wù)。rsync 的 xinetd 配置文件在 /etc/xinetd.d/rsync。要配置以 xinetd 運行的 rsync 服務(wù)需要執(zhí)行如下的命令:
# chkconfig rsync on
# service xinetd restart
獨立運行 rsync 服務(wù)
最簡單的獨立運行 rsync 服務(wù)的方法是執(zhí)行如下的命令:
# /usr/bin/rsync --daemon
您可以將上面的命令寫入 /etc/rc.local 文件以便在每次啟動服務(wù)器時運行 rsync 服務(wù)。
配置文件rsyncd.conf
兩種 rsync 服務(wù)運行方式都需要配置 rsyncd.conf,配置文件 rsyncd.conf 默認在 /etc 目錄下,為了將所有與 rsync 服務(wù)相關(guān)的文件放在單獨的目錄下,可以執(zhí)行如下命令:
# mkdir /etc/rsyncd
# touch /etc/rsyncd/rsyncd.conf
# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
配置文件 rsyncd.conf 由全局配置和若干模塊配置組成。配置文件的語法為: ?模塊以 [模塊名] 開始 ?參數(shù)配置行的格式是 name = value ,其中 value 可以有兩種數(shù)據(jù)類型: ?字符串(可以不用引號定界字符串) ?布爾值(1/0 或 yes/no 或 true/false) ?以 # 或 ; 開始的行為注釋 ?\ 為續(xù)行符
全局參數(shù)
在文件中 [module] 之外的所有配置行都是全局參數(shù)。當然也可以在全局參數(shù)部分定義模塊參數(shù),這時該參數(shù)的值就是所有模塊的默認值。
CentOS6 下rsync服務(wù)器配置CentOS6 下rsync服務(wù)器配置
模塊參數(shù)
模塊參數(shù)主要用于定義 rsync 服務(wù)器哪個目錄要被同步。模塊聲明的格式必須為 [module] 形式,這個名字就是在 rsync 客戶端看到的名字,類似于 Samba 服務(wù)器提供的共享名。而服務(wù)器真正同步的數(shù)據(jù)是通過 path 來指定的。可以根據(jù)自己的需要,來指定多個模塊,模塊中可以定義以下參數(shù):
a. 基本模塊參數(shù)
CentOS6 下rsync服務(wù)器配置CentOS6 下rsync服務(wù)器配置
b. 模塊控制參數(shù)
c. 模塊文件篩選參數(shù)
CentOS6 下rsync服務(wù)器配置CentOS6 下rsync服務(wù)器配置
?一個模塊只能指定一個exclude 參數(shù)、一個include 參數(shù)。 ?結(jié)合 include 和 exclude 可以定義復(fù)雜的exclude/include 規(guī)則 。 ?這幾個參數(shù)分別與相應(yīng)的rsync 客戶命令選項等價,唯一不同的是它們作用在服務(wù)器端。
d. 模塊用戶認證參數(shù)
CentOS6 下rsync服務(wù)器配置CentOS6 下rsync服務(wù)器配置
?rsync 認證口令文件的權(quán)限一定是 600,否則客戶端將不能連接服務(wù)器。 ?rsync 認證口令文件中每一行指定一個 用戶名:口令 對,格式為: username:passwd ?一般來說口令最好不要超過8個字符。若您只配置匿名訪問的 rsync 服務(wù)器,則無需設(shè)置上述參數(shù)。
e. 模塊訪問控制參數(shù)
CentOS6 下rsync服務(wù)器配置CentOS6 下rsync服務(wù)器配置
客戶主機列表定義可以是以下形式: ?單個IP地址。例如:192.168.0.1 ?整個網(wǎng)段。例如:192.168.0.0/24,192.168.0.0/255.255.255.0 ?可解析的單個主機名。例如:centos,centos.bsmart.cn ?域內(nèi)的所有主機。例如:.bsmart.cn ?“”則表示所有。 ?多個列表項要用空格間隔。
f. 模塊日志參數(shù)
設(shè)置了”log file”參數(shù)時,在日志每行的開始會添加”%t [%p]“。
可以使用的日志格式定義符如下所示: ?%a – 遠程IP地址 ?%h – 遠程主機名 ?%l – 文件長度字符數(shù) ?%p – 該次 rsync 會話的 PID ?%o – 操作類型:”send” 或 “recv” ?%f – 文件名 ?%P – 模塊路徑 ?%m – 模塊名 ?%t – 當前時間 ?%u – 認證的用戶名(匿名時是 null) ?%b – 實際傳輸?shù)淖止?jié)數(shù) ?%c – 當發(fā)送文件時,記錄該文件的校驗碼
rsync 服務(wù)器應(yīng)用案例
在服務(wù)器端TS-DEV上配置rsync 服務(wù)
a. 編輯配置文件
# vi /etc/rsyncd/rsyncd.conf
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
# GLOBAL OPTIONS
uid = root
gid = root
use chroot = no
read only = yes
#limit access to private LANs
hosts allow=172.16.0.0/255.255.0.0 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
hosts deny=*
max connections = 5
pid file = /var/run/rsyncd.pid
secrets file = /etc/rsyncd/rsyncd.secrets
#lock file = /var/run/rsync.lock
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
# MODULE OPTIONS
[davidhome]
path = /home/david/
list=yes
ignore errors
auth users = david
comment = David home
exclude = important/
[chinatmp]
path = /tmp/china/
list=no
ignore errors
auth users = china
comment = tmp_china
b. 建立/etc/rsyncd/rsyncd.secrets文件 # vim /etc/rsyncd/rsyncd.secrets
david:asdf #格式 用戶名:口令
china:jk #該用戶不要求是系統(tǒng)用戶
c. 為了密碼的安全性,我們把權(quán)限設(shè)為600
# chown root:root /etc/rsyncd/rsyncd.secrets
# chmod 600 /etc/rsyncd/rsyncd.secrets
d. 建立連接到服務(wù)器的客戶端看到的歡迎信息文件/etc/rsyncd/rsyncd.motd # vim /etc/rsyncd/rsyncd.motd
+++++++++++++++++++++++++++
+ David Camp +
+++++++++++++++++++++++++++
e. 啟動rsync
# /etc/init.d/xinetd restart
f. 查看873端口是否起來
# netstat -an | grep 873
如果rsync啟動成功的話可以看到873端口已經(jīng)在監(jiān)聽了。 g. 服務(wù)器端文件詳細
客戶端配置
a. 客戶端安裝rsync
# yum -y install rsync
b. 通過rsync客戶端來同步數(shù)據(jù)
場景一:
在客戶端上面:
# rsync -avzP [email protected]::davidhome /tmp/david/
Password: 這里要輸入david的密碼,是服務(wù)器端提供的,在前面的例子中,我們用的是 asdf,輸入的密碼并不顯示出來;輸好后就回車;
注: 這個命令的意思就是說,用david 用戶登錄到服務(wù)器上,把davidhome數(shù)據(jù),同步到本地目錄/tmp/david/上。當然本地的目錄是可以你自己定義的,比如 dave也是可以的;當你在客戶端上,當前操作的目錄下沒有davidhome這個目錄時,系統(tǒng)會自動為你創(chuàng)建一個;當存在davidhome這個目錄中,你要注意它的寫權(quán)限。
說明: -a 參數(shù),相當于-rlptgoD,-r 是遞歸 -l 是鏈接文件,意思是拷貝鏈接文件;-p 表示保持文件原有權(quán)限;-t 保持文件原有時間;-g 保持文件原有用戶組;-o 保持文件原有屬主;-D 相當于塊設(shè)備文件; -z 傳輸時壓縮; -P 傳輸進度; -v 傳輸時的進度等信息,和-P有點關(guān)系,自己試試??梢钥次臋n;
場景二:
# rsync -avzP --delete [email protected]::davidhome /tmp/david/
這回我們引入一個 –delete 選項,表示客戶端上的數(shù)據(jù)要與服務(wù)器端完全一致,如果 /tmp/david/目錄中有服務(wù)器上不存在的文件,則刪除。最終目的是讓/tmp/david/目錄上的數(shù)據(jù)完全與服務(wù)器上保持一致;用的時候要小心點,最好不要把已經(jīng)有重要數(shù)所據(jù)的目錄,當做本地更新目錄,否則會把你的數(shù)據(jù)全部刪除;
場景三:
# rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::davidhome /tmp/david/
這次我們加了一個選項 –password-file=rsync.password ,這時當我們以david用戶登錄rsync服務(wù)器同步數(shù)據(jù)時,密碼將讀取 /tmp/rsync.password 這個文件。這個文件內(nèi)容只是david用戶的密碼。我們要如下做;
# touch /tmp/rsync.password
# chmod 600 /tmp/rsync.password
# echo "asdf"> /tmp/rsync.password
# rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::davidhome /tmp/david/
注: 這樣就不需要密碼了;其實這是比較重要的,因為服務(wù)器通過crond 計劃任務(wù)還是有必要的;
5.3. rsync 客戶端自動與服務(wù)器同步數(shù)據(jù)
編輯crontab # crontab -e 加入如下代碼:
10 0 * * * rsync -avzP --delete --password-file=/tmp/rsync.password [email protected]::davidhome /tmp/david/
表示每天0點10分執(zhí)行后面的命令。
本文名稱:搭建rsync服務(wù)器具體步驟
本文URL:http://m.fisionsoft.com.cn/article/coegcps.html


咨詢
建站咨詢
