rsync+inotify实现主备机的实时备份

实现原理

主机同步又很多办法,scp,rsync

scp比较单一,只能全备份,占用带宽

rsync可以压缩传输,并且增量传输,节省带宽,节省资源

--------------------------------------------------------------------------------------割

干货:

主机1:192.168.23.128

主机2:192.168.23.129

rsync也是c/s模式

被传输文件端为server端,传输文件端为client端,client端同样安装rsync服务,并且安装inotify服务

主机2:192.168.23.129

yum安装

主机1为主机端

yum install epel-release -y

yum install rsync -y

配置rsync配置文件

重要的文件有,/etc/rsyncd.conf(主配置文件)。

chmod 600 /etc/rsyncd.pass

/etc/rsyncd.pass(没有该文件,需要自行创建,虚拟账号密码文件,格式为user:passwd)

/etc/rsyncd.conf文件详解

uid = root
gid = root
use chroot = yes  (如果uid,gid不为root,且同步目录有软连接文件,则设置为no,否则同步失败)
max connections = 200(最大连接数)
port = 873
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

[test]  (模块名)

comment=rsync test
path=/root/test/(备份到哪里的位置)
ignore errors(忽略io传输错误)
read only=no(默认yes,不允许client同步文件)
list=no(设为no,就行,yes为隐藏模块)
auth users=wxg(/etc/rsyncd.pass中的user)
secrets file=/etc/rsyncd.pass 
hosts allow=192.168.23.128(允许同步文件的ip,空格隔开)

启动rsync

centos7:rsync --daemon --config=/etc/rsyncd.conf

开机自启:echo "rsync --daemon --config=/etc/rsyncd.conf" >>/etc/rc.local

centos6:rsync属于xinetd服务中的,可在/etc/xinetd.d/rsync文件中disable改为no

service rsync
{
    disable    = no
    flags        = IPv6
    socket_type     = stream
    wait            = no
    user            = root
    server          = /usr/bin/rsync
    server_args     = --daemon
    log_on_failure  += USERID
}

client端主机1:192.168.20.128

yum install rsync inotify-tools -y

不需要启动rsync服务,需要创建密码文件:vim /etc/rsyncd.pass

文件中只有主机2中的密码

现在可以测试一下rsync是否可以成功

rsync -az --progress --password-file=/etc/rsyncd.pass /etc/passwd wxg@192.168.23.129::test

如果成功了,说明可以同步,

不成功则查看日志,看看报错信息,检查防火墙,selinux是否关闭

/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib /root/test

执行上调命令,在/etc/test目录下创建文件。删除文件

可以看出,正在监控/root/test

根据此命令编写脚本

rsync.sh

#!bin/bash
host=192.168.23.129
src=/root/test
des=test
user=wxg
/usr/local/inotify/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files  
do
rsync -az --delete --exclude-from=/etc/rsync.file --password-file=/etc/rsyncd.pass $src $user@$host::$des
echo "$files was rsynced">>/root/rsync.log 2>&1
done

执行 nohup sh rsync.sh &

在root/test中创建文件,观察192.168.23.129中/root/test看是否有文件同步过去

自此,实时同步完毕

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值