Redhat中安裝rsync
1、 首先在服務(wù)端和客戶端都安裝rsync,我的RHEL5默認(rèn)已經(jīng)安裝好了。在安裝RedHat5的時(shí)候,可以在軟件定制中的“基本系統(tǒng)”——》“基本”的“可選的軟件包”中看見:rsync-2.6.8是默認(rèn)選擇安裝的


2、 也可以通過(guò)命令行檢查是否安裝:

或者:rpm –q rsync
3、 如果在開始安裝RedHat的時(shí)候,使用默認(rèn)選擇的rysnc軟件,但現(xiàn)在想用更高版本的rsync,可以卸載掉rysnc,再安裝更高版本的軟件:

4、 卸載完2.6.8版本的rysnc,下面來(lái)安裝更高版本的rsync(3.0.9):


配置rsync服務(wù)器端
5、 修改rsync的配置文件

可以看到rysnc服務(wù)是關(guān)閉的(disable = yes),這里把它開啟,把disable的值改為no
6、 創(chuàng)建rsync服務(wù)器配置文件/etc/rsyncd.conf
[root@hyj etc]# vim /etc/rsyncd.conf
uid = root
gid = root
port = 873 # 指定運(yùn)行端口,默認(rèn)是873,您可以自己指定
hosts allow = 192.168.0.204, 192.168.1.205 # 允許訪問(wèn)的客戶機(jī)
#hosts deny = 0.0.0.0/32 #拒絕訪問(wèn)的
use chroot =
max connections =
timeout=
## 下面這些綠色文件是安裝完RSYNC服務(wù)后自動(dòng)生成的文件,當(dāng)然也可以手動(dòng)配置到指定路徑
pid file = /var/run/rsyncd.pid ##pid文件的存放
lock file = /var/run/rsync.lock ##鎖文件的存放位置
log file = /var/log/rsyncd.log ##日志記錄文件的存放
#motd file = /etc/rsyncd.motd #歡迎
## 上面這段是全局配置,下面的模塊可以有
[test] ## 模塊名字,自己命名
path = /home/hyj/workspace/test #指定文件目錄所在位置,這是必須指定
comment = rsync files ## 注釋
ignore errors ##忽略IO
read only = yes
list = no ## 是否把rsync 服務(wù)器上提供同步數(shù)據(jù)的目錄顯示
## 下面這一行,同步驗(yàn)證時(shí)用的賬號(hào),如果沒有這項(xiàng)就是匿名同步,client同步時(shí)不用用戶名也能同步。
auth users = rsync
secrets file = /etc/rsync.passwd ## 指定認(rèn)證文件
7、 然后創(chuàng)建認(rèn)證文件:
[root@hyj etc]# vim /etc/rsync.passwd
rsync:hyl ## 用戶名:密碼。注意這個(gè)不是系統(tǒng)用戶,只是rsync用戶。
所以不用useradd。
(名字隨便寫,只要和上邊配置文件里的“auth users”參數(shù)一致即可),格式(一行一個(gè)用戶)
賬號(hào):密碼
(2)、把密碼文件的權(quán)限改成600
[root@hyj etc]# chmod 600 /etc/rsync.passwd ## 只能所有者可讀,否則報(bào)錯(cuò)
8、 如果在配置文件中指定了歡迎信息,在/etc下創(chuàng)建rsyncd.motd,設(shè)置歡迎信息:
[root@hyj etc]# vim /etc/rsyncd.motd
Welcome the rsync services!
啟動(dòng)rsyn
9、 在啟動(dòng)之前,可以先看一下rsync是否已經(jīng)啟動(dòng)運(yùn)行:用lsof或者nestat都可以

根據(jù)輸出顯示state為L(zhǎng)ISTEN,表示為等待接受鏈接的狀態(tài),說(shuō)明rsync已經(jīng)啟動(dòng)。
現(xiàn)在先kill掉rsync進(jìn)程,kill之前為上圖,kill之后,再次執(zhí)行netstat –anp|grep 873或者lsof –i:873,結(jié)果為空:

10、 防火墻設(shè)置:
如果服務(wù)器上裝有防火墻,需在服務(wù)器中設(shè)置iptables將837端口開放。
[root@hyj ~]# iptables -A INPUT -p tcp --dport 873 -j ACCEPT
11、 如果rsync進(jìn)程沒有運(yùn)行,在server端將rsync啟動(dòng):
(1)、啟動(dòng)rsync服務(wù)端(以守護(hù)進(jìn)程形式,獨(dú)立啟動(dòng))
#/usr/bin/rsync –daemon

說(shuō)明啟動(dòng)成功。
(切記:命令行啟動(dòng)的最后是daemon,而不是deamon,否則回報(bào)如下錯(cuò)誤:
rsync: --deamon: unknown option
rsync error: syntax or usage error (code 1) at main.c(1231) [client=2.6.8])
(2)、啟動(dòng)rsync服務(wù)端 (以xinetd超級(jí)進(jìn)程啟動(dòng))
# /etc/rc.d/init.d/xinetd reload(reload是網(wǎng)上的說(shuō)法,但是我試了一下報(bào)錯(cuò),start可以)
或者:/etc/rc.d/init.d/xinetd reload(reload是網(wǎng)上的說(shuō)法,但是我試了一下報(bào)錯(cuò),start可以)
演示:


12、 設(shè)置自動(dòng)啟動(dòng):
本機(jī)安裝的Redhat中rsync已經(jīng)是開機(jī)自動(dòng)啟動(dòng)的,網(wǎng)上說(shuō)的那兩種方法,配置文件中都沒有找到相應(yīng)的配置,通過(guò)用chkconfig 命令查看和修改開機(jī)啟動(dòng)服務(wù).
[root@hyj ~]# chkconfig

(結(jié)果部分省略),可以看出rsync處于on的狀態(tài)。
配置rsync客戶端
13、用安裝服務(wù)器端的方式安裝rsync。
14、啟動(dòng)rsync,如果報(bào)如下錯(cuò)誤,是因?yàn)樵趀tc下沒有rsyncd.conf配置文件:

15、[root@hyj etc]# vi /etc/rsyncd.conf
文件內(nèi)容為空就行。然后啟動(dòng)rsync,可以啟動(dòng):

命令和實(shí)例
16、Rsync的命令格式可以為以下六種:
rsync [OPTION]... SRC DEST
rsync [OPTION]... SRC [USER@]HOST:DEST
rsync [OPTION]... [USER@]HOST:SRC DEST
rsync [OPTION]... [USER@]HOST::SRC DEST
rsync [OPTION]... SRC [USER@]HOST::DEST
rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
17、常用為以下兩種:
(4)、rsync [OPTION]... [USER@]HOST::SRC DEST
從遠(yuǎn)程rsync服務(wù)器中拷貝文件到本地機(jī)。當(dāng)SRC路徑信息包含"::"分隔符時(shí)啟動(dòng)該模式。
如:rsync -av root@172.16.78.192::www /databack
(5)、rsync [OPTION]... SRC [USER@]HOST::DEST
從本地機(jī)器拷貝文件到遠(yuǎn)程rsync服務(wù)器中。當(dāng)DST路徑信息包含"::"分隔符時(shí)啟動(dòng)該模式。
如:rsync -av /databack root@172.16.78.192::www
18、下面為實(shí)例:
服務(wù)器ip為192.168.8.126,客戶端ip為192.168.8.122
19、 (1)、把服務(wù)器上的/home/hyj/workspace/test文件夾中的內(nèi)容備份到客戶端的/usr/local/share/rsync_backup中:
[root@hyj ~]# /usr/bin/rsync -vzrtopg --delete --progress rsync@192.168.8.126::test /usr/local/share/rsync_backup
/etc/rsyncd.conf中模塊的內(nèi)容:
path = /home/hyj/workspace/test
comment = rsync files
ignore errors
read only = yes
list = no
auth users = rsync
secrets file = /etc/rsync.passwd
上面這個(gè)命令行中-vzrtopg里的v是verbose,z是壓縮,r是recursive,topg都是保持文件原有屬性如屬主、時(shí)間的參數(shù)(也可以用直接用a來(lái)代替rtopg, a為 --archive 歸檔模式,表示以遞歸方式傳輸文件,并保持所有文件屬性,等于-rlptgoD)。--progress是指顯示出詳細(xì)的進(jìn)度情況,--delete是指如果服務(wù)器端刪除了這一文件,那么客戶端也相應(yīng)把文件刪除,保持真正的一致。
(2)、上面的命令需要在備份的時(shí)候需要輸入密碼,可以在客戶端建立一個(gè)密碼文件,在命令中把密碼文件作為參數(shù)帶入:
[root@hyj rsync_backup]# vim /etc/rsync.pass
密碼文件中不用輸入用戶名,只需輸入密碼即可:

這份密碼文件權(quán)限屬性要設(shè)得只有root可讀,不然會(huì)報(bào)錯(cuò),修改屬性:
[root@hyj rsync_backup]# chmod 600 /etc/rsync.pass,
用下面這條命令,可以不輸入密碼:
/usr/bin/rsync -vzrtopg --delete --progress rsync@192.168.8.126::test /usr/local/share/rsync_backup --password-file=/etc/rsync.pass
20、 帶exclude 參數(shù):
把服務(wù)器上的/home/hyj/workspace/test文件夾中的內(nèi)容備份到客戶端的/usr/local/share/rsync_backup中,但不包括:res目錄和default.properties文件:
/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
exclude/include規(guī)則實(shí)例
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 "*")
21、 把客戶端上的/home/hyj/vitest文件夾中的內(nèi)容備份到服務(wù)器的/usr/local/share/rsync_backup中,在客戶端執(zhí)行如下命令:
/usr/bin/rsync -vzrtopg --delete --progress /home/hyj/vitest rsync@192.168.8.126::clientdata --password-file=/etc/rsync.pass
此時(shí)服務(wù)器的配置文件/etc/rsyncd.conf內(nèi)容為:
uid = root
gid = root
hosts allow = 192.168.8.122, 192.168.8.123
#hosts deny = 0.0.0.0/32
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
timeout=600
[test]
path = /home/hyj/workspace/test
comment = rsync files
ignore errors
read only = yes
list = no
auth users = rsync
secrets file = /etc/rsync.passwd
# 上面的命令中,客戶端的數(shù)據(jù)備份到clientdata模塊中,備份到/usr/local/share/rsync_backup文件夾下,read only改為no,# # 否則會(huì)報(bào)ERROR: module is read only的錯(cuò)誤
[clientdata]
path = /usr/local/share/rsync_backup
comment = rsync files
ignore errors
read only = no
list = no
auth users = rsync
secrets file = /etc/rsync.passwd
定時(shí)計(jì)劃備份
22、 如果執(zhí)行crontab命令提示:

說(shuō)明還沒有為root這個(gè)賬號(hào)提供crontab。執(zhí)行crontab –e 創(chuàng)建一個(gè)crontab:

23、 編輯crontab,每?jī)煞昼妭浞菀淮危?br />

24、 兩分鐘后,查看,數(shù)據(jù)已經(jīng)備份:

25、 每天上午9點(diǎn)20執(zhí)行rysnc備份任務(wù):
20 9 * * * /usr/bin/rsync -vzrtopg --delete --progress rsync@192.168.8.126::test /usr/local/share/rsync_backup --password-file=/etc/rsync.pass
26、附時(shí)間設(shè)置:
(1)、查看當(dāng)前時(shí)區(qū) : date -R
(2)、查看時(shí)間和日期:date
(3)、設(shè)置時(shí)間和日期:
將系統(tǒng)日期設(shè)定成2012年8月4日的命令:date -s 08/04/2012
將系統(tǒng)時(shí)間設(shè)定成下午9點(diǎn)20分0秒的命令:date -s 09:20:00
(4)、將當(dāng)前時(shí)間和日期寫入BIOS,避免重啟后失效:hwclock -w
FAQ
27、我需要在防火墻上開放哪些端口以適應(yīng)rsync?
A:視情況而定
rsync可以直接通過(guò)873端口的tcp連接傳文件,也可以通過(guò)22端口的ssh來(lái)進(jìn)行文件傳遞,但你也可以通過(guò)下列命令改變它的端口:
rsync --port 8730 otherhost::
或者
rsync -e 'ssh -p 2002' otherhost:
28、 我如何通過(guò)rsync只復(fù)制目錄結(jié)構(gòu),忽略掉文件呢?
A:rsync -av --include '*/' --exclude '*' source-dir dest-dir
常見錯(cuò)誤
29、rsync: failed to connect to 218.107.243.2: No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
解決:對(duì)方?jīng)]開機(jī)、防火墻阻擋、通過(guò)的網(wǎng)絡(luò)上有防火墻阻擋,都有可能。關(guān)閉防火墻,其實(shí)就是把tcp udp 的873端口打開:
解決辦法:打開服務(wù)器873端口(RH-Firewall-1-INPUT為REHL 5自帶的鏈名,10為最后一條默認(rèn)規(guī)則(拒絕所以),根據(jù)實(shí)際情況修改這兩個(gè)參數(shù)。另外“-m state --state NEW -m tcp”參數(shù)不加也行)
#iptables -I RH-Firewall-1-INPUT 10 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
#iptables -I RH-Firewall-1-INPUT 10 -p udp -m state --state NEW -m udp --dport 873 -j ACCEPT
打開端口之前的iptables配置:

注:最后一條規(guī)則(REJECT all -- anywhere anywhere reject-with icmp-host-prohibited)表示拒絕所有的數(shù)據(jù)包,并向?qū)Ψ交貞?yīng)icmp-host- prohibited數(shù)據(jù)包。
本機(jī)測(cè)試通過(guò)的方法,打開服務(wù)器873端口:
(1)、[root@duoduo ~]# iptables -I INPUT -p tcp --dport 873 -j
[root@duoduo ~]# iptables -I INPUT -p udp --dport 873 -j ACCEPT
(注意要用-I,而不能用-A,用A的話會(huì)添加這條規(guī)則到iptables鏈的最后,但是沒有到這條規(guī)則就被原來(lái)的最后一條REJECT了,客戶端還是無(wú)法執(zhí)行rysnc備份,用I的話會(huì)插入到鏈的最前面,經(jīng)過(guò)測(cè)試可以正常執(zhí)行rysnc備份。另外“-m state --state NEW -m tcp”參數(shù)可以加上)。
(2)、上面提到的,插入規(guī)則到最后一條的前面:
#iptables -I RH-Firewall-1-INPUT 10 -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
#iptables -I RH-Firewall-1-INPUT 10 -p udp -m state --state NEW -m udp --dport 873 -j ACCEPT
插入之前:

插入規(guī)則:


插入規(guī)則后:

再在客戶端執(zhí)行,可以正常執(zhí)行。
(3)、或者關(guān)閉服務(wù)器的iptables:service iptables stop
30、password file must not be other-accessible
continuing without password file
Password:
解決:這是因?yàn)閞syncd.pwd rsyncd.sec的權(quán)限不對(duì),應(yīng)該設(shè)置為600。如:chmod 600 rsyncd.pwd
31、@ERROR: auth failed on module xxxxx
rsync: connection unexpectedly closed (90 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
解決:這是因?yàn)槊艽a設(shè)置錯(cuò)了,無(wú)法登入成功,檢查一下rsync.pwd,看客服是否匹配。還有服務(wù)器端沒啟動(dòng)rsync 服務(wù)也會(huì)出現(xiàn)這種情況。
32、@ERROR: chroot failed
rsync: connection unexpectedly closed (75 bytes read so far)
rsync error: error in rsync protocol data stream (code 12) at io.c(150)
解決:這是因?yàn)槟阍?rsync.conf 中設(shè)置的 path 路徑不存在,要新建目錄才能開啟同步。
33、[root@hyj rsync_backup]# /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
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1516) [Receiver=3.0.9]
原因及解決辦法:
SELinux;(下面這條命令在服務(wù)器端執(zhí)行)
setsebool -P rsync_disable_trans on
34、ERROR: module is read only
rsync: read error: Software caused connection abort (113)
rsync error: error in rsync protocol data stream (code 12) at io.c(769) [sender=3.0.8]
解決:這是因?yàn)榉?wù)器端配置文件rsyncd.conf中read only = yes,為只讀,即不允許客戶端上傳文件,改成no就可以了。
您可能感興趣的文章:- Linux下使用Rsync完成自動(dòng)化備份
- Linux 命令之rsync命令詳解
- linux rsync同步命令(值得收藏)
- linux系統(tǒng)中rsync+inotify實(shí)現(xiàn)服務(wù)器之間文件實(shí)時(shí)同步
- linux下通過(guò)rsync+inotify 實(shí)現(xiàn)數(shù)據(jù)實(shí)時(shí)備份(遠(yuǎn)程容災(zāi)備份系統(tǒng))
- linux rsync的安裝與配置
- Linux 用cp和rsync同步文件時(shí)跳過(guò)指定目錄的方法
- 用rsync實(shí)現(xiàn)windows與linux文件同步的方法
- Linux 服務(wù)器同步 Rsync同步服務(wù)器文件
- 如何使用Linux的rsync