部署rsync+inotify服务

部署rsync+inotify

  • rsync简介
    rsync是linux系统下的数据镜像备份工具。使用快速增量备份工具 Remote Sync 可以远程同步,支持本地复制,或者与其他ssh,rsync同步。
  • inotify简介
    inotify是一种强大的、细粒度的、异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持。通过inotify可以监控文件系统中添加、删除、修改、移动等各种细微事件,利用这个内核接口,第三方软件就可以监控文件系统下文件的各种变化情况,而inotify-tools就是这样的一个第三方软件。
环境说明:需要两台服务器来模拟,一台做为源服务器,一台做为目标服务器。这里源服务器地址为192.168.226.128,目标服务器地址为192.168.226.130。
需求:把源服务器上/hxd目录实时同步到目标服务器/tmp/hxd/目录下
  • 配置目标服务器
//关闭防火墙与selinux
[root@hxdserver ~]# systemctl stop firewalld
[root@hxdserver ~]# systemctl disable firewalld
[root@hxdserver ~]# getenforce 
Enforcing
[root@hxdserver ~]# setenforce 0
[root@hxdserver ~]# vim /etc/sysconfig/selinux
SELINUX=disabled  //修改
//安装rsync软件
[root@hxdserver ~]# yum install -y rsync
//修改配置文件/etc/rsyncd.conf
[root@hxdserver ~]# vim /etc/rsyncd.conf 
log file = /var/log/rsyncd.log
pidfile = /var/run/rsync.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsync.pass

[hxd]
path = /tmp/hxd
comment = sync etc from client
uid = root
gid = root
port = 873
ignore errors
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = dubai
hosts allow = 192.168.226.128
hosts deny = 172.16.1.1
//添加以上配置
//创建用户认证文件
[root@hxdserver ~]# echo 'dubai:123' > /etc/rsync.pass
[root@hxdserver ~]# cat /etc/rsync.pass 
dubai:123
//修改文件权限
[root@hxdserver ~]# chmod 600 /etc/rsync*
[root@hxdserver ~]# ll /etc/rsync*
-rw-------. 1 root root 854 9月  18 19:20 /etc/rsyncd.conf
-rw-------. 1 root root  10 9月  18 19:27 /etc/rsync.pass

//启动rsync服务并设置开机自启
[root@hxdserver ~]# systemctl start rsyncd
[root@hxdserver ~]# systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.
[root@hxdserver ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128              *:22                           *:*                  
LISTEN     0      100      127.0.0.1:25                           *:*                  
LISTEN     0      5                *:873                          *:*                  
LISTEN     0      128             :::80                          :::*                  
LISTEN     0      128             :::22                          :::*                  
LISTEN     0      100            ::1:25                          :::*                  
LISTEN     0      5               :::873                         :::* 


  • 配置源服务器
//关闭防火墙与selinux
[root@Hongxiande ~]# systemctl stop firewalld
[root@Hongxiande ~]# systemctl disable firewalld
[root@Hongxiande ~]# getenforce 
Disabled

//配置yum源
[root@Hongxiande ~]# yum install -y wget
[root@Hongxiande ~]# cd /etc/yum.repos.d/
[root@Hongxiande yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@Hongxiande yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@Hongxiande yum.repos.d]#  sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@Hongxiande yum.repos.d]#  yum clean all
[root@Hongxiande yum.repos.d]# yum install -y epel-release
//安装rsync服务端软件,只需要安装不需要启动,也不需要配置。
[root@Hongxiande ~]# yum install -y rsync
//创建认证密码文件并设置权限。
[root@Hongxiande ~]# echo '123' > /etc/rsync.pass
[root@Hongxiande ~]# cat /etc/rsync.pass 
123
[root@Hongxiande ~]# chmod 600 /etc/rsync.pass 
[root@Hongxiande ~]# ls
anaconda-ks.cfg
[root@Hongxiande ~]# mkdir -p hxd/xixi
[root@Hongxiande ~]# ls
anaconda-ks.cfg  hxd
[root@Hongxiande ~]# rsync -acH --port 873 --progress --delete /root/hxd [email protected]::hxd --password-file=/etc/rsync.pass
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
//这里报错了,原因是目标主机没有dubai用户。和hxd目录。
//在目标主机先创建dubai用户和/tmp/hxd目录
[root@hxdserver ~]# useradd dubai
[root@hxdserver ~]# cd /tmp/
[root@hxdserver tmp]# mkdir hxd

//再回到源主机测试
[root@Hongxiande ~]# rsync -acH --port 873 --progress --delete /root/hxd [email protected]::hxd --password-file=/etc/rsync.pass
sending incremental file list
hxd/
hxd/xixi/
[root@hxdserver ~]# ls /tmp/hxd/
hxd

//数据同步成功
//安装inotify-tools工具,实时触发rsync进行同步
//查看服务器内核是否支持inotify
[root@Hongxiande ~]# ll /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 9月  19 09:41 max_queued_events
-rw-r--r-- 1 root root 0 9月  19 09:41 max_user_instances
-rw-r--r-- 1 root root 0 9月  19 09:41 max_user_watches

//如果有这三个max开头的文件则表示服务器内核支持inotify
//安装inotify-tools
[root@Hongxiande ~]# yum install -y inotify-tools
//写同步脚本,关键的一步,让脚本自动去检测我们制定的目录下
//文件发生的变化,然后再执行rsync的命令把它同步到服务端去
[root@Hongxiande ~]# mkdir /scripts
[root@Hongxiande ~]# touch /scripts/inotify.sh
[root@Hongxiande ~]# chmod 755 /scripts/inotify.sh 
[root@Hongxiande ~]# ll /scripts/inotify.sh 
-rwxr-xr-x 1 root root 0 9月  19 09:56 /scripts/inotify.sh
//编写脚本
[root@Hongxiande ~]# vim /scripts/inotify.sh
host=192.168.226.130
src=/root/hxd
des=hxd
password=/etc/rsync.pass
user=dubai
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
    rsync -avzP --delete --timeout=100 --password-file=${password} $src $user@$host::$des
    echo "${files} was rsynced" >>/tmp/rsync.log 2>&1
done

//添加如上配置
//执行脚本
[root@Hongxiande ~]# nohup bash /scripts/inotify.sh &
[root@Hongxiande ~]# nohup bash /scripts/inotify.sh &
[1] 1851
[root@Hongxiande ~]# nohup: 忽略输入并把输出追加到"nohup.out"

[root@Hongxiande ~]# ps -ef|grep inotify
root       1851   1243  0 10:22 pts/0    00:00:00 bash /scripts/inotify.sh
root       1852   1851  0 10:22 pts/0    00:00:00 /usr/bin/inotifywait -mrq --timefmt %Y%m%d %H:%M --format %T %w%f%e -e modify,delete,create,attrib /root/hxd
root       1853   1851  0 10:22 pts/0    00:00:00 bash /scripts/inotify.sh
root       1857   1243  0 10:23 pts/0    00:00:00 grep --color=auto inotify

//在源服务器上生成一个新文件
[root@Hongxiande ~]# touch hxd/xixi/test
[root@Hongxiande ~]# echo 'hello world' > hxd/xixi/test
//查看inotify生成的日志
[root@Hongxiande ~]# tail /tmp/rsync.log 
20180919 10:34 /root/hxd/xixi/testCREATE was rsynced
20180919 10:34 /root/hxd/xixi/testATTRIB was rsynced
20180919 10:34 /root/hxd/xixi/testMODIFY was rsynced

//日志中有记录我们所做的操作
//再到目标服务器上查看是否已经同步
[root@hxdserver ~]# cat /tmp/hxd/hxd/xixi/test 
hello world
//同步了 说明是没问题的
//最后把脚本设置为开机自启
[root@Hongxiande ~]# chmod +x /etc/rc.
rc.d/     rc.local  
[root@Hongxiande ~]# chmod +x /etc/rc.d/rc.local 
[root@Hongxiande ~]# ll /etc/rc.d/rc.local 
-rwxr-xr-x. 1 root root 473 6月  27 2017 /etc/rc.d/rc.local
[root@Hongxiande ~]# vim /etc/rc.d/rc.local

nohup /bin/bash /scripts/inotify.sh  //添加这一条配置
这样rsync+inotify就搭建好了
//错误解决
[root@Hongxiande ~]# rsync -acH --port 873 --progress --delete /root/hxd [email protected]::hxd --password-file=/etc/rsync.pass
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1648) [sender=3.1.2]
//这里报错了,原因是目标主机没有dubai用户。和hxd目录。
//在目标主机先创建dubai用户和/tmp/hxd目录

猜你喜欢

转载自blog.csdn.net/dubaiToT/article/details/82763293