你好,游客 登录 注册 搜索
背景:
阅读新闻

CentOS 下 Rsync安装配置详解

[日期:2016-07-30] 来源:Linux社区  作者:zhenhui [字体:大 中 小]

五、rsync 服务器应用案例

5.1. 在服务器端TS-DEV上配置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=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              #该用户不要求是系统用户

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

+++++++++++++++++++++++++++ +                  David Camp                  + +++++++++++++++++++++++++++

e. 启动rsync

# /etc/init.d/xinetd restart

f. 查看873端口是否起来

# netstat -an | grep 873

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

g. 服务器端文件详细

5.2. 客户端配置

a. 客户端安装rsync

# yum -y install rsync

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

场景一:

# rsync -avzP david@172.16.1.135::davidhome  /tmp/david/

Password: 这里要输入david的密码,是服务器端提供的,在前面的例子中,我们用的是 asdf,输入的密码并不显示出来;输好后就回车; 注: 这个命令的意思就是说,用david 用户登录到服务器上,把davidhome数据,同步到本地目录/tmp/david/上。当然本地的目录是可以你自己定义的,比如 dave也是可以的;当你在客户端上,当前操作的目录下没有davidhome这个目录时,系统会自动为你创建一个;当存在davidhome这个目录中,你要注意它的写权限。

说明: -a 参数,相当于-rlptgoD,-r 是递归 -l 是链接文件,意思是拷贝链接文件;-p 表示保持文件原有权限;-t 保持文件原有时间;-g 保持文件原有用户组;-o 保持文件原有属主;-D 相当于块设备文件; -z 传输时压缩; -P 传输进度; -v 传输时的进度等信息,和-P有点关系,自己试试。可以看文档;

场景二: # rsync -avzP  --delete david@172.16.1.135::davidhome  /tmp/david/

这回我们引入一个 –delete 选项,表示客户端上的数据要与服务器端完全一致,如果 /tmp/david/目录中有服务器上不存在的文件,则删除。最终目的是让/tmp/david/目录上的数据完全与服务器上保持一致;用的时候要小心点,最好不要把已经有重要数所据的目录,当做本地更新目录,否则会把你的数据全部删除;

场景三: # rsync -avzP  --delete  --password-file=/tmp/rsync.password  david@172.16.1.135::davidhome  /tmp/david/

这次我们加了一个选项 –password-file=rsync.password ,这时当我们以david用户登录rsync服务器同步数据时,密码将读取 /tmp/rsync.password 这个文件。这个文件内容只是david用户的密码。我们要如下做;

# touch /tmp/rsync.password # chmod 600 /tmp/rsync.password # echo "asdf"> /tmp/rsync.password # rsync -avzP  --delete  --password-file=/tmp/rsync.password  david@172.16.1.135::davidhome  /tmp/david/

注: 这样就不需要密码了;其实这是比较重要的,因为服务器通过crond 计划任务还是有必要的;

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

编辑crontab # crontab -e 加入如下代码:

10 0 * * * rsync -avzP  --delete  --password-file=/tmp/rsync.password  david@172.16.1.135::davidhome  /tmp/david/

表示每天0点10分执行后面的命令。

六、错误分析

@ERROR: chdir failed 
rsync error: error starting client-server protocol (code 5) at main.c(1530) [receiver=3.0.6] 

rsync: opendir "." (in xxxxxxx) failed: Permission denied (13)

解决办法:

1、将 selinux 对 rsync 的限制全部去掉: # /usr/sbin/setsebool -P rsync_disable_trans 1 # service xinetd restart
2、狠一点,禁止整个 selinux : # vim /etc/selinux/config 将其中的 SELINUX=enforcing 修改为 SELINUX=disabled 保存退出后,重启机器。

Rsync 的详细介绍:请点这里
Rsync 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-07/133731.htm

linux
相关资讯       rsync配置  rsync安装  Rsync安装配置 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款