rsync version 3.0.6 配置安装

rsync服务配置(centos6.3系统环境下配置文件同步)

一、rsync 简介

Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件,也可以使用 Rsync 同步本地硬盘中的不同目录。

Rsync的基本特点如下:

    可以镜像保存整个目录树和文件系统;

    可以很容易做到保持原来文件的权限、时间、软硬链接等;

    无须特殊权限即可安装;

    优化的流程,文件传输效率高;

    可以使用rcp、ssh等方式来传输文件,当然也可以通过直接的socket连接;

    支持匿名传输,以方便进行网站镜像。

无论本地同步目录还是远程同步数据,首次运行时将会把全部文件拷贝一次,以后再运行时将只拷贝有变化的文件(对于新文件)或文件的变化部分(对于原有文件)。

rsync 在首次复制时没有速度优势,速度不如 tar,因此当数据量很大时您可以考虑先使用 tar 进行首次复制,然后再使用 rsync 进行数据同步。

二、系统环境

系统平台:CentOS release 6.3 (Final)

Linux RS2 2.6.32-279.el6.i686 #1 SMP Fri Jun 22 10:59:55 UTC 2012 i686 i686 i386 GNU/Linux

rsync 版本:rsync  version 3.0.6  protocol version 30

rsync 服务器:RS1 (192.168.1.201)

rsync 客户端:RS2 (192.168.1.202)

三、服务器端安装rsync服务

yum install rsync

四、配置 rsync 服务

4.1 配置 rsync 服务器的步骤

首先要选择服务器启动方式

对于负荷较重的 rsync 服务器应该使用独立运行方式

对于负荷较轻的 rsync 服务器可以使用 xinetd 运行方式

创建配置文件 rsyncd.conf

对于非匿名访问的 rsync 服务器还要创建认证口令文件

4.2 独立运行 rsync 服务

# /usr/bin/rsync --daemon

4.3 以 xinetd 运行 rsync 服务

# chkconfig rsync on
# service xinetd restart

4.4 配置文件 rsyncd.conf

# mkdir /etc/rsyncd
# touch /etc/rsyncd/rsyncd.conf
# ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf

五、rsync 服务器应用例子

5.1 在服务器端RS1上配置 rsync 服务

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=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
[hello]
path = /var/www/html/hello
list=yes
ignore errors
auth users = rsync_user
comment = hello rsync test
exclude = Runtime/

b. 建立/etc/rsyncd/rsyncd.secrets文件

# vim /etc/rsyncd/rsyncd.secrets

# 加入以下内容

rsync_user:rsync_pass          #格式   用户名:口令
rsync_user2:rsync_pass2        #该用户不要求是系统用户

c. 为了密码的安全性,我们把权限设为600

# chown root:root /etc/rsyncd/rsyncd.secrets
# chmod 600 /etc/rsyncd/rsyncd.secrets

d. 建立客户端连接到服务器看到的欢迎信息文件/etc/rsyncd/rsyncd.motd

# vim /etc/rsyncd/rsyncd.motd

+++++++++++++++++++++++++++

+     rsync test          +

+++++++++++++++++++++++++++

e. 启动rsync

# /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf

f. 查看873端口是否起来

# netstat -an | grep 873
tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      
tcp        0      0 :::873                      :::*                        LISTEN

如果rsync启动成功的话可以看到873端口已经在监听了。

g. 打开防火墙

# vim /etc/sysconfig/iptables

#在iptables文件加入如下内容

# -A INPUT -m state --state NEW -m tcp -p tcp --dport 873 -j ACCEPT

h. 服务器的详细信息

[root@RS1 hello]# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 08:00:27:8F:3F:A8
inet addr:192.168.1.201  Bcast:192.168.1.255  Mask:255.255.255.0
inet6 addr: fe80::a00:27ff:fe8f:3fa8/64 Scope:Link
UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
RX packets:64100 errors:0 dropped:0 overruns:0 frame:0
TX packets:30006 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:15626227 (14.9 MiB)  TX bytes:3193183 (3.0 MiB)

[root@RS1 hello]# ps aux | grep rsync

root      2115  0.0  0.2   6236   644 ?        Ss   09:49   0:00 /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf

root      2186  0.0  0.2   5980   744 pts/1    S+   10:36   0:00 grep rsync

[root@RS1 hello]# pwd

/var/www/html/hello

[root@RS1 hello]# ls

app  realserver.sh  Runtime

5.2. 客户端配置

a. 客户端安装rsync

# yum -y install rsync

b. 配置客户端

# mkdir /etc/rsyncd/
# touch /etc/rsyncd/rsyncd.pass
# chmod 600 /etc/rsyncd/rsyncd.pass
# echo "rsync_pass" /etc/rsyncd/rsyncd.pass

c. 通过rsync客户端来同步数据

[root@RS2 html]# ls
[root@RS2 html]# pwd
/var/www/html
[root@RS2 html]# rsync -azvP --delete --password-file=/etc/rsyncd/rsyncd.pass [email protected]::hello /var/www/html/hello

++++++++++++++++++++++++++++

+    welcome rsync 2014    +

++++++++++++++++++++++++++++

receiving incremental file list

created directory /var/www/html/hello

./

realserver.sh

1355 100%    1.29MB/s    0:00:00 (xfer#1, to-check=2/4)

app/

app/index.php

0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=0/4)

sent 104 bytes  received 873 bytes  1954.00 bytes/sec

total size is 1355  speedup is 1.39

[root@RS2 html]# cd hello/

[root@RS2 hello]# ls

app  realserver.sh

可以看到Runtime目录没有被同步过来

说明:

-a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件;

-z 传输时压缩;

-P 传输进度;

-v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;

–delete 选项,表示客户端上的数据要与服务器端完全一致,如果 /var/www/html/hello/目录中有服务器上不存在的文件,则删除

–password-file 服务器rsync的密码文件,如果没有些选项则需要输入密码

5.3 rsync 客户端自动与服务器同步数据

编辑crontab

# crontab -e /10 * * * * rsync -azvP --delete --password-file=/etc/rsyncd/rsyncd.pass [email protected]::hello /var/www/html/hello

表示每10分执行一次同步

猜你喜欢

转载自liyonghui160com.iteye.com/blog/2209421