centos7安装rsync及两台机器进行文件同步

centos7安装rsync及两台机器进行文件同步

2017年12月21日 11:17:46 码农下的天桥 阅读数:2210 标签: centosrsync同步 更多html

我的分类: 后端java

所属专栏: 研发模式及运维web

版权声明:本文为博主原创文章,未经博主容许不得转载。 https://blog.csdn.net/cdnight/article/details/78861543vim

安装及配置

安装运行:后端

yum -y install rsync  
#启动rsync服务
systemctl start rsyncd.service
systemctl enable rsyncd.service

#检查是否已经成功启动
netstat -lnp|grep 873
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

这里写图片描述 
好了,好了。安装成功。centos

配置: 
首先,配置文件在: 
/etc/rsyncd.conf服务器

vim /etc/rsyncd.conf
  • 1

看到:java-web

这里写图片描述

好了,先修改为:运维

uid = root                              
 # //设置运行rsync 进程的用户
 gid = root
 use chroot = no
 max connections = 4
# pid file = /var/run/rsyncd.pid        
#//CentOS7中yum安装不需指定pid file 不然报错
 lock file=/var/run/rsyncd.lock
 log file = /var/log/rsyncd.log     
 # //此文件定义完成后系统会自动建立
 exclude = lost+found/
 transfer logging = yes
 timeout = 900
 ignore nonreadable = yes         
 # //同步时跳过没有权限的目录
 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2          
 #  //传输时不压缩的文件
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

这里写图片描述

重启:svn

systemctl restart rsyncd.service
  • 1

两个服务器间传输文件

好了,上面配置完rsync了,那么接下来,假设有两台服务器,开发服务器dev及线上测试环境test,如今须要从dev将可运行代码更新到test上面去。

注意:每一次传输文件客户端将告诉服务端须要调用哪一个传输规则进行传输,而传输规则以下:

[simba]          //此名字即客户端使用rsync来同步的路径
path=/usr/local/simba    //实际须要同步的路径
comment=simba    //和中括号里名字同样就行
ignore errors
read only=no   //表示能够pull 
write only=no      //表示能够push
list=no
auth users=rsyncuser    //客户端获取文件的身份此用户并非本机中确实存在的用户
secrets file=/etc/rsyncd.passwd     //用来认证客户端的秘钥文件 格式 USERNAME:PASSWD 此文件权       
                                                        //限必定须要改成600,且属主必须与运行rsync的用户一致。
hosts allow=*    //容许全部主机访问
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

好了,咱们本身作一个规则,假定test上面接收的目录是: /data/www/helloRsync

#建立目录
mkdir  /data/www/helloRsync
  • 1
  • 2
  • 3
  • 4

而dev上面也是。。。 
请自行建立。 
那么在test上面的/etc/rsyncd.conf添加规则:

#规则名称,做为测试用规则,直接用这个算了。
[helloRsync]          
#同步的路径
path=/data/www/helloRsync    
#规则描述
comment=测试规则
ignore errors
#是否能够pull
read only=no    
#是否能够push
write only=no      
list=no
#下面配置同步时候的身份,注意该身份是在rsync里面定义的,并不是是本机实际用户。等下说说如何在rsync里面定义身份。
#客户端获取文件的身份此用户并非本机中确实存在的用户
auth users=rsyncuser    
#//用来认证客户端的秘钥文件 格式 USERNAME:PASSWD 此文件权     
#//限必定须要改成600,且属主必须与运行rsync的用户一致。  
secrets file=/etc/rsyncd.passwd                                                             
#容许全部主机访问
hosts allow=*
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

这里写图片描述

给rsync定义身份,以下:

echo 'rsyncuser:123456'>/etc/rsyncd.passwd   //文件用户名和路径为上面定义,别写错,密码本身定
chmod 600 /etc/rsyncd.passwd        //修改权限
  • 1
  • 2

重启服务。

systemctl restart rsyncd.service
  • 1

客户端的配置

1。建立密码。

echo '123456' >>/etc/rsyncd-test.passwd     //注意这里只须要服务器rsyncd.passwd 中的密码
chmod 600 /etc/rsyncd-test.passwd
  • 1
  • 2

为了测试顺利,咱们添加一些文件进行同步,譬如:

echo 'test,hello'>> /data/www/helloRsync/readme.txt
  • 1
  • 2

好了,同步:

rsync -auv --password-file=/etc/rsyncd-test.passwd rsyncuser@120.x.x.x::helloRsync /data/www/helloRsync/
  • 1

这里写图片描述

看看服务器上面对应目录

这里写图片描述

检查缘由

这里写图片描述

test上面看日志:

vim  /var/log/rsyncd.log
  • 1
  • 2

这里写图片描述

好了,

这里写图片描述

auth user 是错的,要用 auth users,也是醉了,都没发现吗?

改过之后重启rsync服务,在尝试

获得:

这里写图片描述

name or service not kunown

据查这不是问题,怀疑,是rsync命令问题,应该是将test的文件下载下来了,因而将目录调换一下顺序,获得:

rsync -auv --password-file=/etc/rsyncd-test.passwd  /data/www/helloRsync/ rsyncuser@120.x.x.x::helloRsync
  • 1

报错: 
这里写图片描述

好了,是没有放开权限: 
这里写图片描述

这里写图片描述

放开之后重启终于成功了。泪流满面。坑爹丫。

一份真实环境用的更新脚本demo

src="/usr/local/webroot/java-web-bld"
rsync -rauvvt --progress \
--password-file=/etc/rsyncd-test.passwd \
--exclude="console/data" \
--exclude=".svn" \
--exclude="WEB-INF/logs" \
--exclude="res/upload" \
--exclude="WEB-INF/upload" \
--exclude="WEB-INF/temp" \
--exclude="env.properties" \
/usr/local/webroot/java-web-bld/ rsyncuser@xxx.xx.xx.xxx::backend
相关文章
相关标签/搜索