Centos7 rsync 实现文件同步

 

rsync(remote sync)是unix及类unix平台下的数据镜像备份软件,它不像FTP那样需要全备份,rsync可以根据数据的变化进行差异备份,从而减少数据流量,提高工作效率

序号类型ip
1server10.200.132.141
2client10.200.132.142

从server端同步数据到client端

一、安装rsync

centos7 自带rsync,所以不需要额外的安装。

二、server配置

1、编辑配置文件

vim /etc/rsync.conf

uid = root
gid = root
use chroot = no
port = 873
max connections = 2000
timeout = 200
# 指定日志文件位置,由于日志是放到log下面,所以需要手动创建rsyncd目录,否则启动失败
log file = /var/log/rsyncd/rsyncd.log
# 指定rsync的pid目录
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log format = %t %a %m %f %b

# 同步的模块名
[dcim_saas]
# 同步地址
path=/opt/dcim-saas
# 与同步模块名保持一致
comment =dcim_saas
list =yes
read only =no
write only =no
uid = root
# 认证信息地址
secrets file =/etc/rsyncd.passwd
ignore errors = yes
# 允许同步的客户端地址
hosts allow = 10.200.132.142

2、编辑认证文件

echo "rsyncuser:123456" > /etc/rsyncd.passwd

[root@dcim-saas-master rsyncd]# echo "rsyncuser:123456" > /etc/rsyncd.passwd
[root@dcim-saas-master rsyncd]# cat /etc/rsyncd.passwd 
rsyncuser:123456

3、启动

[root@dcim-saas-master rsyncd]# systemctl restart rsyncd

查看端口

[root@dcim-saas-master rsyncd]# netstat -anpt|grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      6581/rsync          
tcp6       0      0 :::873                  :::*                    LISTEN      6581/rsync          

查看日志

[root@dcim-saas-master rsyncd]# tail -f /var/log/rsyncd/rsyncd.log
2018/09/06 03:57:10 [6581] rsyncd version 3.0.9 starting, listening on port 873

4、关闭防火墙,否则客户端不能同步数据

[root@dcim-saas-master rsyncd]# systemctl stop firewalld
[root@dcim-saas-master rsyncd]# systemctl disabled firewalld

三、client配置

client基本上不用做配置

在客户端机器上执行同步命令

rsync -auv --password-file=/etc/rsyncd.passwd rsyncuser@10.200.132.141::dcim_saas /opt/dcim-saas/

rsync -auv --password-file=/etc/rsyncd.passwd 用户名@server_ip::同步模块名 client_path

如果只同步某个子目录,加上参数 --include 比如只同步js目录 --include=js/

rsync -auv --include=js/ --password-file=/etc/rsyncd.passwd rsyncuser@10.200.132.141::dcim_saas /opt/dcim-saas/

如果同步部分目录,可以多加几个--include=,这样会比较麻烦,更简便的方式是加上参数--include-from

先添加一个文件

vim /opt/dcim-saas/conf/rsync-include.conf

[root@dcim-saas-slave conf]# vim /opt/dcim-saas/conf/rsync-include.conf
agent/
grafana/
nginx/
zutai/

然后执行

 rsync --size-only -azvP --include-from="/opt/dcim-saas/conf/rsync-include.conf" --exclude=/* --password-file=/etc/rsyncd.passwd rsyncuser@10.200.132.141::dcim_saas /opt/dcim-saas/

[root@dcim-saas-slave conf]# rsync --size-only -azvP --include-from="/opt/dcim-saas/conf/rsync-include.conf" --exclude=/* --password-file=/etc/rsyncd.passwd rsyncuser@10.200.132.141::dcim_saas /opt/dcim-saas/

如果排除一些文件同步,可以加上参数--exclude=

如果排除的文件比较多,类型也不一样 可以加上 --exclude-from

vim /opt/dcim-saas/conf/rsync-exclude.conf

[root@dcim-saas-slave conf]# vim /opt/dcim-saas/conf/rsync-exclude.conf
agent/*.log
agent/agent.2*
agent/prometheus/*.log

然后执行

rsync --size-only -azvP --include-from="/opt/dcim-saas/conf/rsync-include.conf" --exclude-from="/opt/dcim-saas/conf/rsync-exclude.conf" --exclude=/* --password-file=/etc/rsyncd.passwd rsyncuser@10.200.132.141::dcim_saas /opt/dcim-saas/

[root@dcim-saas-slave conf]# rsync --size-only -azvP --include-from="/opt/dcim-saas/conf/rsync-include.conf" --exclude-from="/opt/dcim-saas/conf/rsync-exclude.conf" --exclude=/* --password-file=/etc/rsyncd.passwd rsyncuser@10.200.132.141::dcim_saas /opt/dcim-saas/

同步过程中会产生日志,可以去server端查看同步日志情况

 

四、配置双向同步

双向同步就是反过来配置一遍就好了,将server的配置拷贝到client,修改里面的host allows,其他不变,将client的配置拷贝到server

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值