首页 > 代码库 > 使用inotify和rsync进行nfs服务器共享目录实时推送到backup服务器
使用inotify和rsync进行nfs服务器共享目录实时推送到backup服务器
1.1 查看环境
1.1.1 backup服务端的环境
[root@backup nfsbackup]# uname -a
Linux backup 2.6.32-696.el6.x86_64 #1 SMP Tue Mar21 19:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
iptables的状态
[root@backup nfsbackup]# /etc/init.d/iptables status
iptables: Firewall is not running.
XElinux的状态
[root@backup nfsbackup]# getenforce
Disabled
查看rsync服务是否启动
[root@backup nfsbackup]# ss -lntup|grep"rsync"
tcp LISTEN 0 5 :::873 :::* users:(("rsync",1294,5))
tcp LISTEN 0 5 *:873 *:* users:(("rsync",1294,3))
查看推送的目录是模块nfsbackup
[nfsbackup]
path = /nfsbackup/
ignore errors
read only = false
list = false
hosts allow = 172.16.1.0/24
#hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
1.1.2 nfs服务器端
[root@nfs01 ~]# uname -a
Linux nfs01 2.6.32-696.el6.x86_64 #1 SMP Tue Mar 2119:29:05 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
iptables状态
[root@nfs01 ~]# /etc/init.d/iptables status
iptables: Firewall is not running.
XElinux状态
[root@nfs01 ~]# getenforce
Disabled
我们使用的共享目录是nfsbackup
[root@nfs01 ~]# showmount -e
Export list for nfs01:
/app/r 172.16.1.0/24
/app/w 172.16.1.0/24
/data 10.0.0.0/24
/www 172.16.1.0/24
/nfsbackup 172.16.1.0/24
查看是否有inotify,如果没有下载
[root@nfs01 ~]# rpm -qa inotify-tools
inotify-tools-3.14-1.el6.x86_64
下载方法
yum install inotify-tools -y
注: inotify-tools属于epel源.
查看epel
[root@nfs01 ~]# yum repolist
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
* base:mirrors.aliyun.com
* epel:mirrors.aliyun.com
* extras:mirrors.aliyun.com
* updates:mirrors.aliyun.com
repo id repo name status
base CentOS-6 - Base - mirrors.aliyun.com 6,706
epel Extra Packages for Enterprise Linux6 - x86_64 12,350
extras CentOS-6 - Extras - mirrors.aliyun.com 45
updates CentOS-6 - Updates - mirrors.aliyun.com 383
repolist: 19,484
更换方法(更换为阿里的源)
###更换系统默认的yum源为国内
http://mirrors.aliyun.com/
mv /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo
##扩展的yum源-补充
epel源-fedora
wget -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo
一步完成:
mv /etc/yum.repos.d/CentOS-Base.repo/etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repohttp://mirrors.aliyun.com/repo/Centos-6.repo
wget -O /etc/yum.repos.d/epel.repohttp://mirrors.aliyun.com/repo/epel-6.repo
我们的目标是每当nfs端的/nfsbackup有文件或目录变化就向backup端的女方是backup模块推送,保持实时更新.
1.2 nfs服务端设置
1.2.1 创建脚本
1,利用inotify监控/nfsbackup中目录和文件的变化
2,利用while循环把/nfsbackup中目录和文件的变化,推送到backup服务器的nfsbackup模块中
vim watch.sh
#!/bin/bash
IP=`hostname -i`
NFSBP=/nfsbackup/
###监控/nfsbackup的变化,并交给while进行逐行执行
inotifywait -mrq $NFSBP --format "%w%f" -e create,delete,close_write,moved_to|while read line
do
rsync-aLz --delete $NFSBP rsync_backup@172.16.1.41::nfsbackup/$IP/--password-file=/etc/rsync.password
###推送$line文件到172.16.1.41下的nfsbackup中
done
1.2.2 测试需要开多个窗口
窗口1, [root@nfs01 scripts]#/bin/bash /server/scripts/watch.sh
窗口2,[root@nfs01 nfsbackup]#touch {1..4}.log
[root@nfs01 nfsbackup]# echo "伟大" >a.txt
窗口3, [root@backup nfsbackup]#ll 172.16.1.31/
total 0
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:381.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:382.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:383.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:384.log
[root@backup nfsbackup]# ll 172.16.1.31/
total 4
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:381.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:38 2.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:383.log
-rw-r--r-- 1 rsync rsync 0 Jun 28 16:384.log
-rw-r--r-- 1 rsync rsync 7 Jun 28 16:40a.txt
[root@backup nfsbackup]# cat172.16.1.31/a.txt
伟大
1.2.3 添加到开机自启动
echo ‘/bin/bash /server/scripts/watch.sh &‘>>/etc/rc.local
查看
[root@nfs01 scripts]# tail -1 /etc/rc.local
/bin/bash /server/scripts/watch.sh &
注:&是在后台执行的意思.
[root@nfs01 scripts]# jobs
[1]+ Running /bin/bash/server/scripts/watch.sh &
[root@nfs01 nfsbackup]# jobs -p 查看后台命令的进程号
3362
[root@nfs01 nfsbackup]# kill 3362 删除这个进程号的进程
本文出自 “一直向前的河” 博客,请务必保留此出处http://csl19881016.blog.51cto.com/12855437/1943255
使用inotify和rsync进行nfs服务器共享目录实时推送到backup服务器