服务器间文件实时传输-rsync

前言

目前由于后台系统生成相应脚本,需要同步到前端的静态资源中去,所以此处就使用了linux的rsync,如果遇到问题,可以先看下后面的踩坑实况。

服务器信息

server(源服务器):
  • 服务器:10.12.0.1
  • 目录:(properties中)visitor.script.path = /home/build/admin/script/
client(目标服务器):
  • 服务器:10.12.0.2
  • 目录: /home/build/rampage-visitor-front-pc/pc/script/
拓扑图

《服务器间文件实时传输-rsync》

1、服务端配置

server服务器配置
备注
ip10.12.0.1
操作系统centos
同步目录/home/build/admin/script/
1.1、安装步骤
  • Step 1:命令
    yum install rsync
  • Step 2、配置文件
    位置:/etc/rsyncd
    mkdir /etc/rsyncd
备注
rsyncd.confrsync服务器的配置文件
rsyncd.secrets用户密码文件chmod 600 /etc/rsyncd/rsyncd.secrets
rsyncd.motd定义服务器信息
1.2、配置文件信息
  • 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
pid file = /var/run/rsyncd.pid
port = 873
address = 10.12.0.1
#uid = nobody
#gid = nobody   
uid = root
gid = root

use chroot = yes
read only = no


#limit access to private LANs
#hosts allow=192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
hosts allow=10.12.0.2
#hosts deny=*

max connections = 5
motd file = /etc/rsyncd/rsyncd.motd

#This will give you a separate log file
log file = /home/logs/rsync/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
[rampage]
path = /home/build/admin/script
list=yes
ignore errors
auth users = root
secrets file = /etc/rsyncd/rsyncd.secrets
comment = linuxsir home
exclude =   beinan/  samba/

[beinan]
path = /opt
list=no
ignore errors
comment = optdir
auth users = beinan
secrets file = /etc/rsyncd/rsyncd.secrets
  • rsyncd.secrets
    用户名:密码
    root:123456
  • rsyncd.motd
+++++++++++++++++++++++++++
+ *.test.cn  rsync  2017-2099 +
+++++++++++++++++++++++++++

1.3、启动服务

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

1.4、开启防火墙

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

至此,server端的配置已经完成。

2、Client配置

服务器信息
备注
ip10.12.0.2
操作系统centos
同步目录/home/build/rampage-visitor-front-pc/pc/script/
同步说明
  • 机制: client每两分钟同步Server文件信息(包括新增和删除)
  • 实现方式:crontab
  • 配置和启动
2.1、安装相关服务同上rsync
  • 1、 安装服务
    yum install rsync
  • 2、配置密码
    vim /etc/rsyncd/rsync.password
  • 3、输入用户名密码
    root:123456
  • 4、设置权限
    chmod 600 /etc/rsyncd/rsync.password
  • 5、退出保存
  • 6、crontab –e
7. */2 * * * * rsync -avzP --delete --password-file=/etc/rsyncd/rsync.password root@:10.12.0.1:rampage     /home/build/rampage-visitor-front-pc/pc/script/

此处是使用了crontab服务,若服务器没有配置该服务,执行yum install crontab安装该服务

  • 8、启动服务
    /sbin/service crond restart

踩坑实况

  • 1、Rsync 默认873端口,如果修改默认端口需要在Service端的配置文件和Client端同时修改,否则会报Reset错误。
  • 2、Client 连接方式如下(指定端口):
    这是一条命令
rsync -avzP --port=8733 --password-file=/etc/rsyncd/rsync.password root@10.12.28.209::rampage   /home/build/rampage-visitor-front-pc/pc/script
  • 3、机器SSH端口默认22,如果机器修改了默认端口,使用如下方式连接
rsync -e 'ssh -p port' username@hostname:SourceFile DestFile
  • 4、注意服务器端和客服端的防火墙设置
    原文作者:阿亮私语
    原文地址: https://www.jianshu.com/p/a92f0722f810
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞