3.sersync同步

Sersync实时同步数据


inotify介绍

inotify 是一个可以监控目录和文件的工具
监控文件内容是否发生变化
目录中是否删除或创建新文件
实时同步:通过监控文件和目录的变化,来触发rsync推送数据的动作,起到实时同步的作用

安装inotify

# 明确:inotify安装在客户端上(web01 web02)
[root@web01 ~]# yum install -y inotify-tools
[root@web02 ~]# yum install -y inotify-tools

inotify 选项和参数

-m 持续监控
-r 递归
-q 静默,仅打印时间信息
--timefmt 指定输出时间格式
--format 指定事件输出格式
%Xe 事件
%w 目录
%f 文件
-e 指定监控的事件
access 访问
modify 内容修改
attrib 属性修改
close_write 修改真实文件内容
open 打开
create 创建
delete 删除
umount 卸载

inotify命令测试

inotifywait -mrq --format '%Xe %w %f' -e
create,modify,delete,attrib,close_write /backup

脚本

## 实时同步:rsync + inotify
[root@web01 ~]# cat bak.sh
#!/bin/bash
export RSYNC_PASSWORD=123
bak_dir=/zls_backup/
/usr/bin/inotifywait -mrq --format '%Xe %w %f' -e
create,modify,delete,attrib,close_write $bak_dir|while read line;do
rsync -az --delete $bak_dir rsync_backup@172.16.1.41::zls
done

Sersync实时同步

#Sersync实时同步
#Inotify 是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件异动告警,增量备份,并针对目录或文件的变化及时作出响应。 rsync+inotify 可以实触发式实时同步增量备份sersync 是国人基于 rsync+inotify-tools 开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。

sersync项目地址

配置backup——服务端

#先安装rsync和inotify-tools
[root@backup ~]# yum install -y rsync inotify-tools

#修改配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log

[czm]
comment = commit
path = /backup
[data]
path = /data
#########################################注意#####################################################                    #由于多了一个模块所以
[root@backup ~]# useradd rsync -s /sbin/nologin -M
[root@backup ~]# mkdir -p /backup
[root@backup ~]# chown -R rsync.rsync /backup
[root@backup ~]# mkdir -p /data
[root@backup ~]# chown -R rsync.rsync /data

#创建密码文件并授权——密码文件的文件名要和配置文件一致
[root@backup ~]# echo "rsync_backup:123" > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd

# 启动服务并设置开机自启
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd

配置web01和web02——客户端

#准备事项先安装依赖包
[root@web01 ~]# yum install -y rsync inotify-tools

#下载sersync安装包
[root@web01 ~]# wget http://test.driverzeng.com/other/sersync2.5.4_64bit_binary_stable_final.tar.gz
##或者git hub里下载

#创建一个目录 /app 用来存放二进制包的解压文件
[root@web01 ~]# mkdir /app

#解压安装包
[root@web01 ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /app/

#修改程序目录名
[root@web01 app]# mv /app/GNU-Linux-x86 /app/sersync-2.5.4

#制作软链接:方便于代码上线更新版本只要改一下软连接所指的文件就行
[root@web01 app]# ln -s /app/sersync-2.5.4 /app/sersync

#修改配置
[root@web01 ~]# ll /app/sersync-2.5.4/
total 1772
-rwxr-xr-x 1 root root    2211 May 15 15:37 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync2

[root@web01 app]# vim /app/sersync/confxml.xml
    <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="true"/>
        <modify start="true"/>
    </inotify>

    <sersync>
        <localpath watch="/data">
            <remote ip="172.16.1.41" name="data"/>
            <!--<remote ip="192.168.8.39" name="tongbu"/>-->
            <!--<remote ip="192.168.8.40" name="tongbu"/>-->
        </localpath>
        <rsync>
            <commonParams params="-az"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="false" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        
#创建数据目录
[root@web01 app]# mkdir /data

#创建密码文件
[root@web01 app]# echo '123' > /etc/rsync.passwd

#授权
[root@web01 app]# chmod 600 /etc/rsync.passwd

#启动sersync
[root@web01 app]# /app/sersync/sersync2 -rdo /app/sersync/confxml.xml

在这里插入图片描述

排错流程

1.先查看SELinux这个是否关闭防火墙是否关闭
[root@web02 data]# getenforce
Disabled

2.查看配置文件是否错误
[root@backup ~]# cat /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log

[czm]
comment = commit
path = /backup
[data]
path = /data

3.查看密码文件权限是否正确
4.检查服务是否启动成功
[root@backup ~]# ps -ef|grep rsync
root 18475 1 0 11:43 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
[root@backup ~]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN
18475/rsync
tcp6 0 0 :::873 :::* LISTEN
18475/rsync

常见错误及解决办法

1——ERRPR:chdir failed
服务器端备份存储目录不存在或建立的备份存储目录和配置文件定义不一致

错误演示过程(想要实现实时同步后检验时)

[root@web02 data]# cd /data && rsync -az -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.passwd
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1656) [sender=3.1.2]

解决办法

#在服务器端创建相关目录(该目录是同步时上传的路径,在模块底下),并赋予权限rsync管理权限
mkdir -p /backup
chown -R rsync.rsync /backup
2——EEROR: some files/attrs were not transferred (see previous errors) (code23) at main.c(1179) [sender=3.1.2]
1、服务端配置文件中指定的用户和模块指定的目录的属主属组不同
2、服务端模块指定的目录属组属组没有权限

错误演示过程

rsync -avz /etc/passwd rsync_user@10.0.0.41::backup
Password:
sending incremental file list
passwd
rsync: mkstemp ".passwd.W6cTyw" (in backup) failed: Permission denied (13)
sent 608 bytes received 122 bytes 208.57 bytes/sec
total size is 1,238 speedup is 1.70
rsync error: some files/attrs were not transferred (see previous errors) (code23) at main.c(1179) [sender=3.1.2]

解决办法

#服务端rsync对目录操作权限不足
chown -R rsync.rsync /backup
3——ERROR: cannot stat destination"." (in backup): Permission denied (13)
服务端rsync对目录操作权限不足

错误演示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::backup
Password:
sending incremental file list
rsync: ERROR: cannot stat destination "." (in backup): Permission denied (13)
rsync error: errors selecting input/output files, dirs (code 3) atmain.c(635)[Receiver=3.1.2]

解决办法

#修改服务器端共享目录的权限
chmod 755 /backup
4——ERROR: auth failed on module backup
服务端/etc/rsync.password 配置文件是否有问题

1、 客户端密码文件的权限不是600
2、 服务端密码文件不是600
3、 服务端密码文件不存在(名字写错了/没有创建/配置文件参数写错了)
4、 服务端密码文件里保存的用户名和密码不正确
5、 配置文件的命令行和注释行是不是在一行
6、 客户机和服务机搞反了

错误演示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::backup
Password:
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1656)[sender=3.1.2]

解决办法

服务器/etc/rsync.password和/etc/rsyncd.conf两个文件用户名或密码是否一致
客户端密码文件的权限是不是600
服务端密码文件是不是600
服务端密码文件不存在(名字写错了/没有创建/配置文件参数写错了)
服务端密码文件里保存的用户名和密码不正确
删除注释
重写文件在客户端
5——ERROR: Unknown module ‘aaa’
客户端输入的模块错误或者服务器端没有该模块

错误显示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::aaa
@ERROR: Unknown module 'aaa'
rsync error: error starting client-server protocol (code 5) at main.c(1656)[sender=3.1.2]

解决办法

#换一个模块使用
#编写一个局部模块
vim /etc/rsyncd.conf
uid = rsync
......
[aaa]
comment = welcome to softeem backup!
path = /backup
6——ERROR: auth failed on module backup
1./etc/rsync.password内用户名和密码有错误
2.secrets file = /etc/rsync.password指定的密码文件和实际密码文件名称不一致
3./etc/rsync.password文件权限不是600
4.rsync_backup:123456 密码配置文件后面注意不要有空格
5.!!!!!密码文件的路径一定要复制配置文件!!!!!

错误显示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::backup/ --password-file=/etc/rsync.password
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at main.c(1656)[sender=3.1.2]

解决办法

#按问题原因逐步排查
检查server端和client端的用户名和密码确认都无误;
服务器端检查这个/etc/rsync.pass密码文件,
客户端检查--password-file配置的密码文件
7——ERROR: password file must not be other-accessible
客户端使用免交互,创建了/etc/rsync.password文件,但权限不是600

错误显示过程

rsync -avz /etc/passwd rsync_user@10.0.0.41::backup/ --password-file=/etc/rsync.password
ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(196)[sender=3.1.2]

解决办法

#rsync客户端的秘钥文件也必须是600权限
chmod 600 /etc/rsync.password
8——ERROR: syntax or usage error (code 1) at authenticate.c(187)
加入了–password-file=/etc/rsync.password参数,但是没有创建/etc/rsync.password文件

错误显示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::backup/ --passwordfile=/etc/rsync.password
rsync: could not open password file /etc/rsync.password: No such file or directory (2)
rsync error: syntax or usage error (code 1) at authenticate.c(187)[sender=3.1.2]

解决办法

#:创建/etc/rsync.password文件,并更改权限
echo "1" > /etc/rsync.password
chmod 600 /etc/rsync.password
9——ERROR: error in socket IO (code 10) at clientserver.c(126) [sender=3.1.2]
rsync服务未启动

错误显示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::backup/
rsync: failed to connect to 10.0.0.41 (10.0.0.41): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(126) [sender=3.1.2]

解决方法

#启动服务
systemctl start rsyncd
#开机自启
systemctl enable rsyncd

10——ERROR: error starting client-server protocol (code 5) at main.c(1656)
rsync服务无效用户信息
rsync服务对应rsync虚拟用户不存在了

错误显示过程

[root@web01 ~]# rsync -avz /etc/passwd rsync_user@10.0.0.41::backup/
Password:
@ERROR: invalid uid rsyn
#@ERROR: invalid gid rsyn
rsync error: error starting client-server protocol (code 5) at main.c(1656) [sender=3.1.2]

解决方法

#创建需要的用户
vim /etc/rsyncd.conf
uid = rsync
gid = rsync
.............
11——ERROR: password file must not be other-accessible
文件不能由其他人访问

错误显示过程

[root@web02 data]# cd /data && rsync -az -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.passwd
ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.2]

解决方法

客户机的密码文件格式错误
[root@web02 ~]# vim /etc/rsync.passwd 
rsync_backup:123

正确是
[root@web02 ~]# vim /etc/rsync.passwd 
123

password file must not be other-accessible

文件不能由其他人访问

错误显示过程

[root@web02 data]# cd /data && rsync -az -R --delete ./ rsync_backup@172.16.1.41::data --password-file=/etc/rsync.passwd
ERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.2]

解决方法

客户机的密码文件格式错误
[root@web02 ~]# vim /etc/rsync.passwd 
rsync_backup:123

正确是
[root@web02 ~]# vim /etc/rsync.passwd 
123
  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值