一、服務端的搭建
rpm -ivh http://mirrors.sohu.com/centos/6/os/x86_64/Packages/rsync-3.0.6-12.el6.x86_64.rpm
配置文件
rsync的主要有以下三個配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密碼文件)、rsyncd.motd(rysnc服務器信息)
服務器配置文件(/etc/rsyncd.conf),該文件默認不存在,請創建它。
具體步驟如下:
#touch /etc/rsyncd.conf #創建rsyncd.conf,這是rsync服務器的配置文件。
#touch /etc/rsyncd.secrets #創建rsyncd.secrets ,這是用戶密碼文件。
#chmod 600 /etc/rsyncd/rsyncd.secrets #將rsyncd.secrets這個密碼文件的文件屬性設爲root擁有, 且權限要設爲600, 否則無法備份成功!
#touch /etc/rsyncd.motd
1-3 rsyncd.conf配置文件內容
# Distributed under the terms of the GNU General Public License v2
# 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
pid file = /var/run/rsyncd.pid
port = 873
address = 192.168.2.100
#uid = nobody
#gid = nobody
uid = root
gid = root
use chroot = yes
read only = yes
#limit access to private LANs
hosts allow=192.168.1.0/255.255.255.0 192.168.2.0/255.255.255.0
hosts deny=*
max connections = 5
motd file = /etc/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
[json]
path = /var/www/html/json
list=no
ignore errors
comment = /var/www/html/json
auth users = root
secrets file = /etc/rsyncd.secrets
模塊定義
模塊定義什麼呢?主要是定義服務器哪個目錄要被同步。每個模塊都要以[name]形式。這個名字就是在rsync 客戶端看到的名字,其實有點象Samba服務器提供的共享名。而服務器真正同步的數據是通過path 指定的。我們可以根據自己的需要,來指定多個模塊。每個模塊要指定認證用戶,密碼文件、但排除並不是必須的
下面是前面配置文件模塊的例子:
[rhel4home] #模塊它爲我們提供了一個鏈接的名字,在本模塊中鏈接到了/home目錄;要用[name] 形式
path = /home #指定文件目錄所在位置,這是必須指定的
auth users = root #認證用戶是root ,是必須在服務器上存在的用戶
list=yes #list 意思是把rsync 服務器上提供同步數據的目錄在服務器上模塊是否顯示列出來。默認是yes 。如果你不想列出來,就no ;如果是no是比較安全的,至少別人不知道你的服務器上提供了哪些目錄。你自己知道就行了;
ignore errors #忽略IO錯誤
secrets file = /etc/rsyncd.secrets #密碼存在哪個文件
comment = linuxsir home data #註釋可以自己定義
exclude = beinan/ samba/
1-4 rsyncd.secrets文件內容
root:yqqlmgs1cl&gdzt039
然後執行chmod 600 /etc/rsyncd.secrets
1-5rsyncd.motd文件內容
++++++++++++++++++++++++++++++++++++++++++++++
Welcome to use the mike.org.cn rsync services!
2002------2015
++++++++++++++++++++++++++++++++++++++++++++++
1-6 修改/etc/xinetd.d/rsync內容(disable = no )
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
三、啓動rsync服務器及防火牆的設置
/usr/bin/rsync --daemon --config=/etc/rsyncd.conf
vim /etc/sysconfig/iptables裏面添加873端口
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
service iptables restart
啓動rsync服務器相當簡單,有以下幾種方法
A、--daemon參數方式,是讓rsync以服務器模式運行
#/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf #--config用於指定rsyncd.conf的位置,如果在/etc下可以不寫
B、xinetd方式
修改services加入如下內容
# nano -w /etc/services
rsync 873/tcp # rsync
rsync 873/udp # rsync
這一步一般可以不做,通常都有這兩行(我的RHEL4和GENTOO默認都有)。修改的目的是讓系統知道873端口對應的服務名爲rsync。如沒有的話就自行加入。
設定 /etc/xinetd.d/rsync, 簡單例子如下:
# default: off
# description: The rsync server is a good addition to am ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
上述, 主要是要打開rsync這個daemon, 一旦有rsync client要連接時, xinetd會把它轉介給 rsyncd(port 873)。然後service xinetd restart, 使上述設定生效.
rsync服務器和防火牆
Linux 防火牆是用iptables,所以我們至少在服務器端要讓你所定義的rsync 服務器端口通過,客戶端上也應該讓通過。
#iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT
#iptables -L 查看一下防火牆是不是打開了 873端口
如果你不太懂防火牆的配置,可以先service iptables stop 將防火牆關掉。當然在生產環境這是很危險的,做實驗纔可以這麼做喲!
服務端測試
rsync --list-only [email protected]::json
四、客戶端設置
1、設置密碼
vim /etc/rsync.secret
輸入yqqlmgs1cl&gdzt039
chmod 600 /etc/rsync.secret
服務端測試
rsync --list-only [email protected]::json
正式使用
/usr/bin/rsync -avzP --password-file=/etc/rsync.secret [email protected]::json /var/json
編寫腳本
/root/rsync.sh
#!/bin/sh
/usr/bin/rsync -avzP --password-file=/etc/rsync.secret [email protected]::json /var/json