rsync+sersync实时同步数据

前言

rsync+sersync实时同步数据。

简介

rsync+sersync实时同步数据的原理是在客户端安装sersync监控目录的变化,一般是增删改,检测到变化以后,将变化的文件同步到服务端。

rsync+sersync实时同步数据

实验环境:

服务器:tudou64

客户端:tudou63

服务端tudou64的操作:

1.安装完rsync后修改服务端tudou64的rsyncd的配置文件

[root@tudou64 web-bak]# yum -y install rsync

[root@tudou64 web-bak]# vim /etc/rsyncd.conf

uid = root #运行进程的身份。

gid = root #运行进程的组。

address =192.168.130.64 #监听 IP。

port =873 #监听端口。

hosts allow =* #用*表示所 有

use chroot = yes #是否囚牢,锁定家目录,rsync 被黑之后,黑客无法再 rsync 运行的家目录之外创建文件,选项设置为 yes。

max connections =5 #最大连接数。

pid file =/var/run/rsyncd.pid #进程 PID,自动生成。

lock file =/var/run/rsync.lock #指 max connectios 参数的锁文件。

log file =/var/log/rsyncd.log #日志文件位置。

motd file =/etc/rsyncd.motd #客户端登陆之后弹出的消息,需要创建。

[wwwroot] #共享模块名称。

path =/web-back/ #路径。

comment = used for web-data root #描述。

read only = false #只读权限为假。

list = yes #是否允许查看模块信息。

auth users = rsyncuser #备份的用户,和系统用户无关。

secrets file =/etc/rsync.passwd #存放用户的密码文件,格式是 用户名:密码。

2.创建登陆提示文件和密码文件

[root@tudou64 web-bak]# echo “Welcome to Backup Server” > /etc/rsync.motd

[root@tudou64 web-bak]# echo “rsyncuser:123456” > /etc/rsync.passwd

3.修改密码文件权限(这个很重要)

[root@tudou64 web-bak]# chmod 600 /etc/rsync.passwd

4.启动rsyncd服务

[root@tudou64 web-bak]# systemctl start rsyncd && systemctl enable rsyncd
[root@tudou64 web-bak]# netstat -antpu |grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 3114/rsync
tcp6 0 0 :::873 ::😗 LISTEN 3114/rsync

客户端tudou63的操作:

1.下载sersync并安装sersync

[root@tudou63 ~]#wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@tudou63 ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@tudou63 ~]# mv ./GNU-Linux-x86/ ./sersync

[root@tudou63 ~]# mv sersync /opt/

2.创建rsyncd的密码文件

[root@tudou63 html]# echo 123456 > /etc/rsync.passwd

3.修改密码文件权限(这个很重要)

[root@tudou63 html]#chmod 600 /etc/rsync.passwd

4.修改sersync的配置文件

[root@tudou63 sersync]# cp confxml.xml{,.bak}

[root@tudou63 sersync]# vim confxml.xml

#修改配置文件中的 24–28 行,sersync 模块部分





#修改 31–34 行,rsync 密码认证部分





在这里插入图片描述

注:

根据tudou64服务端的配置更改tudou63客户端sersync的配置文件

5.启动sersync监控并备份/var/www/html目录

[root@tudou63 ~]# /opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml

[root@tudou63 html]# vim /root/webbackup.sh
#!/bin/bash
rsync -avz /var/www/html/ rsyncuser@192.168.130.64::wwwroot --password-file=/etc/rsync.passwd

在这里插入图片描述

6.编写sersync监控自动开机启动脚本

[root@tudou63 ~]# vim /opt/check-sersync.sh

#!/bin/sh
sersync="/opt/sersync/sersync2"
confxml="/opt/sersync/confxml.xml"
status=$(ps aux |grep ‘sersync2’|grep -v ‘grep’|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi

[root@tudou63 ~]# chmod +x /opt/check-sersync.sh

[root@tudou63 ~]# echo “sh /opt/check-sersync.sh” >> /etc/rc.d/rc.local

总结

需要注意的 2 点:

  1. 密码文件一定要加600的权限。

  2. 服务端的配置文件不要出错。

  3. sersync的配置文件需要根据服务端进行配置。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值