rsync同步服务器配置指南

来源:互联网 发布:c语言二分法求方程例题 编辑:程序博客网 时间:2024/04/27 02:52

第一:安装前准备

1, 说明:

要求将192.168.1.147的 /usr/gameserver 目录同步到 192.168.1.130的 /usr/gameserver
也就是说 192.168.1.147是rsync 源服务器,192.168.1.130 是rsync 目的服务器
只允许客户端从服务器同步内容到本地 不允许客户端上传数据。

如果有多台源服务器要同步到同一台目的服务器上,那么最好是将源服务器设置成客户端,目的服务器设置成服务器端,当然配置文件中要允许写(read only = no)。

在本例中 我们只到一台源服务器和一台目的服务器上,所以两种方法都可以。

本文参照:
http://hi.baidu.com/fiber212121/blog/item/9db4351f7e5fcbc0a68669fc.html(主要参照)
http://www.lingzhong.cn/tech/23765.htm

2, 本文的系统环境

[root@localhost ~]# # uname -a
Linux FWQ-SZ-NSJ-0909-GS-160-147 2.6.32-220.17.1.el6.x86_64 #1 SMP Wed May 16 00:01:37 BST 2012 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# # more /etc/redhat-release 
CentOS release 6.2 (Final)

3,    检查必须的系统组件是否安装

[root@localhost ~]# # rpm -q xinetd openssh-clients
xinetd-2.3.14-33.el6.x86_64
openssh-clients-5.3p1-70.el6_2.2.x86_64
[root@localhost ~]# #
如果没有安装,请自行安装,最方便的方法是:
[root@localhost ~]# #yum -y install  xinetd openssh-clients安装

第二:rsync 的安装和配置:
1,    安装软件

[root@localhost ~]# # yum -y install rsync

2,    修改相关配置文件(服务器端)
1)    新建rsync服务器的配置文件

[root@localhost ~]# # vi /etc/rsyncd.conf
#################################脚本开始####################################
全局设置
secrets file = /etc/rsyncd.secrets     \\ rsync 同步时密码文件
motd file = /etc/rsyncd.motd           \\ 欢迎词存放文件
read only = yes          \\ 只读模式(下载模式),只允许客户端下载,不允许上传,适合一对多的同步方式。如果是多对一的同步,请将这里改成no。
list = yes
uid = root            \\ 可以自行创建备份用户,也可用root,默认是nobody
gid = root
hosts allow = 192.168.1.130           \\ 允许同步的机器,可以是一个网段
hosts deny = 0.0.0.0/0              \\ 拒绝同步的机器,这里是只允许上面指定的机器
max connections =2                  \\ 最大连接数
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsnc.lock

[gameserver]                          \\ 定义要同步的目录域
comment = gameserver's directoty form 192.168.1.147   \\ 介绍说明
path = /usr/gameserver
#secrets file = /etc/rsyncd.secrets     \\ rsync 同步时密码文件
#motd file = /etc/rsyncd.motd           \\ 欢迎词存放文件
auth users = rsync                     \\ 允许连接用户,使用逗号分隔多个用户

要定义多个目录可以重复上面的格式
#################################脚本结束####################################

2)    新建rsync登陆的欢迎文件

[root@localhost ~]# # vi /etc/rsyncd.motd
#################################脚本开始####################################
Welcome to use the rsync services!
#################################脚本结束####################################
[root@localhost ~]# #

3)    新建rsync登陆的用户名密码文件

[root@localhost ~]# # vi /etc/rsyncd.secrets
#################################脚本开始####################################
用户名:密码
rsync:javjav
#################################脚本结束####################################
[root@localhost ~]# #

4)    新建rsync的服务进程

[root@localhost ~]# # vi /etc/xinetd.d/rsync
#################################脚本开始####################################
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no                  \\ 由xinetd 进程 启动rsync,如果要由独立的进行启动rsync 那么这里改成yes
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}
#################################脚本结束####################################
[root@localhost ~]# #

5)    更改文件权限

[root@localhost ~]# # chmod 600 /etc/rsyncd.*

6)    启动相关服务

以下两种启动方式任选一种,当然/etc/xinetd.d/rsync 中要做相应的修改
[root@localhost ~]# service xinetd restart                  \\ 以xinetd守护进程启动
[root@localhost ~]# rsync --daemon /etc/rsyncd.conf         \\ 不以xinetd守护进程启动
[root@localhost ~]# echo “rsync --daemon /etc/rsyncd.conf”>> /etc/rc.local  开机自动启动

7)    查看服务端口状态

[root@localhost ~]# netstart -tlnp | grep 873       如果有873 说明服务启动成功

8)    在防火墙中打开873端口

[root@localhost ~]# iptables -A INPUT -s 192.168.1.130 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
[root@localhost ~]# iptables -A INPUT -s 192.168.1.130 -p udp --dport 873 -j ACCEPT

[root@localhost ~]# iptables-save

3,    修改相关配置文件(客户端)

1)    新建rsync登陆的密码文件

这样就可以自动同步了
[root@localhost ~]# vi /etc/rsyncd.secrets
#################################脚本开始####################################
客户端只需密码无须用户
密码
javjav
#################################脚本结束####################################

2)    更改文件权限

[root@localhost ~]# chmod 600 /etc/rsyncd.*

3)    在防火墙中打开873端口

iptables -A INPUT -d 192.168.1.147 -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT
iptables -A INPUT -d 192.168.1.147 -p udp --dport 873 -j ACCEPT

4,    在客户端进行测试

[root@localhost ~]# rsync -azvp rsync@192.168.1.147::gameserver /usr/gameserver

rsync -参数 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容 本地存放路径
说明:
-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;
-z 传输时压缩;
-P 传输进度;
-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;


可以将以上命令加入到crontab中自动执行 也可以加入到脚本中。

4, 其它说明(错误解决)

1)报错 rsync: failed to set permissions on  和rsync error:如下

 

rsync -参数 本地存放路径 用户名@同步服务器的IP::rsyncd.conf中那个方括号里的内容

[root@localhost ~]# rsync -azvp --delete --exclude-from=/etc/exclude.list --password-file=/etc/rsync.pass /usr/gameserver/ rsync@192.168.1.147::gameserver

sending incremental file list
./
rsync: failed to set permissions on "." (in web): Permission denied (13)

sent 43614 bytes  received 93 bytes  5142.00 bytes/sec
total size is 108128586  speedup is 2473.94
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
[root@localhost ~]#

以上错误常常是在linux 和windows 之间出现的,此错误的原因是因为 在linux目录下有 “.”这个目录 而在windows下没有,所以要想同步整个目录 要加一个星号(*)。如下

[root@localhost ~]# rsync -azvp --delete --exclude-from=/etc/exclude.list --password-file=/etc/rsync.pass /usr/gameserver/*   rsync@192.168.1.147::gameserver

windows 下的rsync安装请上baidu自行查找,很多的。也可以参照http://hi.baidu.com/cong_rong520/blog/item/e42ac64fd6fb3cf4d62afcbc.html


http://yd514.iteye.com/blog/1860313