Centos7 rsync 实现文件同步

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

序号 类型 ip
1 server 10.200.132.141
2 client 10.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 [email protected]::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 [email protected]::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 [email protected]::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 [email protected]::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 [email protected]::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 [email protected]::dcim_saas /opt/dcim-saas/

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

四、配置双向同步

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

猜你喜欢

转载自blog.csdn.net/liurui_wuhan/article/details/82422716