Rsync+Inotify-tools+Shell脚本实现文件实时同步

1. 环境准备

机器名称机器IP角色部署内容文件同步目录
主机A192.168.79.134rsync客户端inotify-tools、rsync.sh/mnt/data/redis
主机B192.168.79.151rsync服务端rsyncd.service/mnt/backup

同步要求:将主机A上的/mnt/data/redis/目录下的文件实时同步到主机B中的/mnt/backup/目录下。

2. 部署与配置

2.1 rsync服务端 (192.168.79.151)

(1)创建rsync用户组与用户,以及文件同步的目录:

[root@active_sentinel ~]# groupadd rsync
[root@active_sentinel ~]# useradd -g rsync rsync -s /sbin/nologin
[root@active_sentinel ~]# mkdir -p /mnt/backup

(2)安装rsync并修改配置文件:

[root@active_sentinel ~]# yum -y install rsync

[root@active_sentinel ~]# vim /etc/rsyncd.conf

log file = /var/log/rsyncd.log         #日志文件位置,启动rsync后自动创建这个文件
pidfile = /var/run/rsyncd.pid          #pid文件的存放位置
lock file = /var/run/rsync.lock        #支持max connections参数的锁文件
secrets file = /etc/rsync.passwd       #用户认证配置文件,里面保存用户名称和密码,需要自己创建这个文件
motd file = /etc/rsyncd.welcome        #rsync启动时欢迎信息页面文件位置(自己创建这个文件,内容随便自定义)
[rsync_client]               #自定义名称
path = /data/backup          #rsync服务端数据目录路径,将客户端的数据同步在/mnt/backup/路径下
comment = rsync_client       #模块名称,可自定义名称
uid = root                   #设置rsync运行的uid权限。
gid = root                   #设置rsync运行的gid权限。
port=873                     #默认的rsync端口
use chroot = no              #默认为true,修改为no或false,增加对目录文件软连接的备份
read only = no               #设置rsync服务端文件为读写权限
list = no                    #不显示rsync服务端资源列表
max connections = 200        #最大连接数
timeout = 600                #设置超时时间
auth users = rsync           #执行数据同步的用户名,需要后面手动设置。可以设置多个,用英文状态下逗号隔开
#hosts allow = 172.16.200.143     #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开 (如果没有,就不用设置这一行)
#hosts deny = 172.16.200.150      #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开(如果没有,就不用设置这一行)

(3)配置rsync服务用户名与密码:

[root@active_sentinel ~]# vim /etc/rsync.passwd
#用户:密码
rsync:rsync

#一定要修改文件权限
[root@active_sentinel ~]# chmod 600 /etc/rsync.passwd 

(4)启动rsyncd服务:

[root@active_sentinel ~]# systemctl start rsyncd
[root@active_sentinel ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@active_sentinel ~]# netstat -tuanlp |grep 873
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      16339/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      16339/rsync  

(5)修改/etc/hosts,添加rsync客户端IP:

[root@active_sentinel ~]# vim /etc/hosts
#添加rsync客户端的主机IP与主机名
192.168.79.134 主机名

(6)关掉sexlinux与防火墙

[root@active_sentinel ~]# setenforce 0
[root@active_sentinel ~]# systemctl stop firewalld
[root@active_sentinel ~]# systemctl disable firewalld

2.2 rsync客户端 (192.168.79.134)

(1)安装rsync,配置用户认证文件

[root@192 ~]# yum -y install rsync

[root@192 etc]# vim /etc/rsync.passwd
#只写入密码即可
rsync

#修改文件权限
[root@192 etc]# chmod 600 /etc/rsync.passwd

(2)关掉sexlinux与防火墙

[root@192 etc]# setenforce 0
[root@192 etc]# systemctl stop firewalld
[root@192 etc]# systemctl  disable firewalld

(3)测试rsync是否能实现文件的传输(将客户端的/mnt/data/test同步到服务端/mnt/backup/下)

[root@192 etc]# mkdir /mnt/data/test        //创建测试目录
[root@192 etc]# rsync -avH --port 873 --delete /mnt/data/test rsync@192.168.79.151::rsync_client --password-file=/etc/rsync.passwd

sending incremental file list
test/

sent 62 bytes  received 24 bytes  172.00 bytes/sec
total size is 0  speedup is 0.00

看到以上显示就代表成功了!

(4)安装inotify-tools

#查看系统内核版本是否支持inotify,出现以下文件代表支持
[root@192 etc]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r--. 1 root root 0 Jul 14 23:14 max_queued_events
-rw-r--r--. 1 root root 0 Jul 14 23:14 max_user_instances
-rw-r--r--. 1 root root 0 Jul 14 22:23 max_user_watches

#安装inotify-tools
[root@192 etc]# yum --enablerepo=epel install inotify-tools

(5)创建rsync+inotify同步脚本rsync.sh:

[root@192 etc]# vim rsync.sh 


#!/bin/bash

SRCDIR="/mnt/data/redis/"      //服务端同步路径
USER="rsync"            //同步用户
IP="192.168.79.151"    //服务端地址
MODULE="rsync_client"   //rsync模块名称
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H%M' --format '%T %w%f%e' -e close_write,modify,delete,create,attrib,move $SRCDIR | while read file
do
/usr/bin/rsync -azr  $SRCDIR $USER@$IP::$MODULE --password-file=/etc/rsync.passwd
echo " ${file} was rsynced" >> /tmp/rsync.log 2>&1
done

(6)后台执行脚本,设置为开机自启

[root@192 etc]# nohup sh rsync.sh  > /dev/null 2> /dev/null &  
[1] 4116
[root@192 etc]# ps -ef |grep rsync
root       4116   2964  0 23:23 pts/1    00:00:00 sh rsync.sh
root       4118   4116  0 23:23 pts/1    00:00:00 sh rsync.sh
root       4120   2964  0 23:23 pts/1    00:00:00 grep --color=auto rsync

#添加到开机自启

vim /etc/rc.d/rc.local
#添加命令
nohup sh rsync.sh  > /dev/null 2> /dev/null &  

这样每当在客户端进行文件的修改,服务端也能看到同步过来的文件了!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值