搭建文件同步服务rsync与inotify实时同步

版权声明:未经本人允许严禁转载 https://blog.csdn.net/WanJiaBaoBao/article/details/82763204

搭建要求

  • 同步客户端/ly目录至目标服务器/tmp/ly下;

搭建环境

  • 关闭防火墙、selinux;
  • 服务器的IP地址为:192.168.92.128/24、客户端的IP地址为:192.168.92.129/24

搭建步骤

  • 服务器端搭建
## 先安装rsync服务,修改/etc/rsyncd.conf配置文件 ##
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# cat >> /etc/rsyncd.conf << EOF
> log file = /var/log/rsync.log
> pid file = /var/run/rsync.pid
> lock file = /var/run/rsync.lock
> secrets file = /etc/rsync.passwd
> [ly]
> path = /tmp/ly/
> comment = backup
> uid =root
> gid = root
> port = 873
> ignore errors
> use chroot = no
> read only = no
> list = no
> max connections = 200
> timeout = 600
> auth users = user
> hosts allow = 192.168.92.129
> hosts deny = 172.16.12.128
> EOF

## 创建用户认证文件,并设置文件权限 ##
[root@localhost ~]# echo "user:passwd" > /etc/rsync.passwd  #文件名必须与配置文件中保持一致
[root@localhost ~]# chmod 600 /etc/rsync.passwd

## 启动rsync服务并设置开机自动启动 ##
[root@localhost ~]# systemctl start rsyncd
[root@localhost ~]# systemctl enable rsyncd
ln -s '/usr/lib/systemd/system/rsyncd.service' '/etc/systemd/system/multi-user.target.wants/rsyncd.service'

## 创建同步目录/tmp/ly ##
[root@localhost ~]# mkdir /tmp/ly

  • 客户端搭建(需要同步或者数据的)
## 先安装rsync服务,不需要启动,不需要配置,创建认证密码文件并修改权限 ##
[root@localhost ~]# yum -y install rsync
[root@localhost ~]# echo "passwd" > /etc/rsync.passwd  #客户端中写入的密码必须与服务器端设置的密码相同
[root@localhost ~]# chmod 600 /etc/rsync.passwd   #为了安全必须将设置权限

## 在客户端上将需要备份的/ly目录,备份至服务器中/tmp/ly中 ##
[root@localhost ~]# mkdir /ly
[root@localhost ~]# rsync -avH --port 873 --progress --delete /ly [email protected]::ly --password-file=/etc/rsync.passwd
sending incremental file list
ly/

sent 38 bytes  received 12 bytes  33.33 bytes/sec
total size is 0  speedup is 0.00
# 注:/ly——表示需要备份的目录;
# 注:IP地址后面接的ly表示服务器端共享的文件名(虚拟的,不是真实存在服务器上的文件名);
# 注:password-file=/etc/rsync.passwd是客户端的密码。

## 查看内核是否支持inotify ##
[root@localhost ~]# ll /proc/sys/fs/inotify
total 0
-rw-r--r--. 1 root root 0 Sep 19 06:18 max_queued_events
-rw-r--r--. 1 root root 0 Sep 19 06:18 max_user_instances
-rw-r--r--. 1 root root 0 Sep 19 06:18 max_user_watches

## 到此同步数据已经完成,但每次同步都需要手工完成同步,所以下面在客户端安装inotify-tools来配置实时监控,来自动同步 ##
[root@localhost ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@localhost ~]# sed -i "s/$releasever/7/g" /etc/yum.repos.d/CentOS-Base.repo
[root@localhost ~]# yum -y install  epel-release   #先安装epel源
[root@localhost ~]# yum -y install inotify-tools   #安装inotify-tools工具

## 编写同步脚本,自动检测同步目录下文件发生的变化,然后再执行rsync的命令把它同步到服务器端去 ##
[root@localhost ~]# touch inotify.sh
[root@localhost ~]# chmod 755 inotify.sh
[root@localhost ~]# vim inotify.sh 
#!/bin/bash

host=192.168.92.128
src=/ly
dest=ly
password=/etc/rsync.passwd
user=user
inotifywait=/usr/bin/inotifywait

$inotifywait -mrq --timefmt '%Y%m%d $H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src | while read files;do
        /usr/bin/rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$dest
        echo "${files} was rsynced" >> /tmp/rsync.log 2>&1
done


*注:infotifywait参数
-m, --monitor:inotifywait的默认动作是在监控至指定文件的特定事件发生一次后就退出了,而使用此选项则可实现持续性的监控;
-r, --recursive:递归监控指定目录下的所有文件,包括新建的文件或子目录;
-q,--quite:静默输出
--timefmt <fmt>:当在--format选项中使用%T时,--timefrt选项则可以用来指定自定义的符合strftime规范的时间格式。
--format <fmt>:自定义inotifywait的输出格式,%w:显示被监控文件的文件名;
%f:如果发生某事件的对象是目录,则显示被监控目录的名字;默认显示为空串;
%T:使用--timefmt选项中自定义的时间格式。
-e <event>, --event <event>:指定要监控的特定事件,默认是监控所有的事件;


## 再后台启动脚本 ##
[root@localhost ~]# nohup bash /root/inotify.sh &
[1] 10167
[root@localhost ~]# ps aux | grep inotify
root      10167  0.0  0.1 113116  1396 pts/0    S    05:20   0:00 bash /root/inotify.sh
root      10168  0.0  0.0   6480   752 pts/0    S    05:20   0:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d $H:%M --format %T %w%f%e -e modify,delete,create,attrib /ly
  • 再客户端同步目录中创建文件,查看验证效果
## 客户端 ##
[root@localhost ly]# mkdir 123
[root@localhost ly]# ls
123

## 服务器 ##
[root@localhost ly]# cd ly/
[root@localhost ly]# ls
123
  • 再指定的日志文件/tmp/rsync.log中查看生成的日志
[root@localhost ly]# cat /tmp/rsync.log 
20180919 $H:17 /ly/456DELETE was rsynced
20180919 $H:17 /ly/123CREATE,ISDIR was rsynced

  • 实现脚本开机自启动
## 再客户机上,将/etc/rc.d/rc.local文件给执行权限(这个文件文系统启动最后读取的文件) ##
[root@localhost ly]# chmod a+x /etc/rc.d/rc.local
[root@localhost ly]# echo "nohup /bin/bash /tmp/inotify.sh" >> /etc/rc.d/rc.local

搭建遇到的问题

  • 在客户机写的脚本放在/root目录下同步不成功:
    解决办法:将脚本移动到其他目录。

猜你喜欢

转载自blog.csdn.net/WanJiaBaoBao/article/details/82763204
今日推荐