NFS共享存储 inotify和 Sersync 实时同步
目录
NFS存储
关闭防火墙
#1.关闭Firewalld防火墙
[root@nfs ~]# systemctl disable firewalld
[root@nfs ~]# systemctl stop firewalld
#2.关闭selinux防火墙
[root@nfs ~]# sed -ri '#^SELINUX=#cSELINUX=Disabled' /etc/selinux/config
[root@nfs ~]# setenforce 0
安装nfs服务端
[root@nfs ~]# yum -y install nfs-utils
NFS配置文件修改
#NFS客户端地址与权限之间没有空格
[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash)
#在NFS服务器上建立用于NFS文件共享的目录,并设置对应权限
[root@nfs ~]# mkdir /data
[root@nfs ~]# chown -R nfsnobody.nfsnobody /data
#NFS共享目录会记录至/var/lib/nfs/etab,如果该目录不存在共享信息,请检查/etc/exports是否配置错误
设置开机自
#加入开机自启并启动
[root@nfs ~]# systemctl enable --now nfs-server rpcbind
#加入开机自启
[root@nfs ~]# systemctl enable rpcbind nfs-server
#启动服务
[root@nfs ~]# systemctl restart rpcbind nfs-server
#检测端口
[root@nfs ~]# netstat -lntup
#检测文件
[root@nfs ~]# cat /var/lib/nfs/etab
NFS客户端
关闭防火墙
#1.关闭Firewalld防火墙
[root@nfs ~]# systemctl disable firewalld
[root@nfs ~]# systemctl stop firewalld
#2.关闭selinux防火墙
[root@nfs ~]# sed -ri '#^SELINUX=#cSELINUX=Disabled' /etc/selinux/config
[root@nfs ~]# setenforce 0
安装客户端,仅启动rpc服务
[root@nfs-client ~]# yum -y install nfs-utils rpcbind
[root@nfs-client ~]# systemctl restart rpcbind
客户端查看挂载源
客户端使用showmount -e查看远程服务器rpc提供的可挂载nfs信息
[root@nfs-client ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
客户端挂载
在NFS客户端创建一个挂载目录, 使用mount命令并结合-t参数, 指定要挂载的文件系统的类型, 并在命令后面写上服务器的IP地址, 以及服务器上的共享目录, 最后需要写上要挂载到本地系统(客户端)的目录。
[root@nfs-client ~]# mkdir /nfsdir
[root@nfs-client ~]# mount -t nfs 172.16.1.31:/data /nfsdir
#查看挂载信息(mount也可以查看)
[root@nfs-client ~]# df –h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 62G 845M 58G 2% /
tmpfs 244M 0 244M 0% /dev/shm
/dev/sda1 190M 26M 155M 14% /boot
172.16.1.31:/data 62G 880M 58G 2% /nfsdir
挂载成功后可以进行增删改操作
#使用客户端往nfs存储写入
[root@nfs-client ~]# echo "nfs-client" >> /mnt/test.txt
#检查nfs服务端是否存在客户端创建的新文件
[root@nfs-client ~]# cat /data/test.txt
nfs-client
开机挂载
例1:(不安全,如果挂不上会开不了机了)
如果希望NFS文件共享服务能一直有效,则需要将其写入到fstab文件中
#编辑fstab文件
[root@nfs-client ~]# vim /etc/fstab
172.16.1.31:/data /nfsdir nfs defaults 0 0
#验证fstab是否写正确
[root@nfs-client ~]# mount -a
例2:检查nfs服务是否正常(脚本)把脚本路径写入/etc/rc.local
#!/bin/bash
# 探测NFS服务是否正常
showmount -e $1 >/dev/null 2>&1
if [ $? -eq 0 ];then
mount -t nfs $1:/data/image /mnt
else
#下面是挂载不成功发邮件脚本
/root/nfs.sh
fi
卸载nfs
如果不希望使用NFS共享, 可进行卸载
[root@nfs-client ~]# umount /nfsdir
#注意:卸载的时候如果提示”umount.nfs: /nfsdir: device is busy”
#1.切换至其他目录, 然后在进行卸载。
#2.NFS Server宕机, 强制卸载umount -lf /nfsdir
增加安全性
在企业工作场景,通常情况NFS服务器共享的只是普通静态数据(图片、附件、视频),不需要执行suid、exec等权限,挂载的这个文件系统只能作为数据存取之用,无法执行程序,对于客户端来讲增加了安全性。例如: 很多木马篡改站点文件都是由上传入口上传的程序到存储目录。然后执行的。
#通过mount -o指定挂载参数,禁止使用suid,exec,增加安全性能
[root@nfs-client ~]# mount -t nfs -o nosuid,noexec,nodev 172.16.1.31:/data /mnt
增加性能
有时也需要考虑性能相关参数[可选]
#通过mount -o指定挂载参数,禁止更新目录及文件时间戳挂载
[root@nfs-client ~]# mount -t nfs -o noatime,nodiratime 172.16.1.31:/data /mnt
6.NFS配置详解
执行man exports命令,然后切换到文件结尾,可以快速查看如下样例格式:
| nfs共享参数 | 参数作用 |
| rw* | 读写权限 |
| ro | 只读权限 |
| root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的匿名用户(不常用) |
| no_root_squash | 当NFS客户端以root管理员访问时,映射为NFS服务器的root管理员(不常用) |
| all_squash | 无论NFS客户端使用什么账户访问,均映射为NFS服务器的匿名用户(常用) |
| no_all_squash | 无论NFS客户端使用什么账户访问,都不进行压缩 |
| sync* | 同时将数据写入到内存与硬盘中,保证不丢失数据 |
| async | 优先将数据保存到内存,然后再写入硬盘;这样效率更高,但可能会丢失数据 |
| anonuid* | 配置all_squash使用,指定NFS的用户UID,必须存在系统 |
| anongid* | 配置all_squash使用,指定NFS的用户UID,必须存在系统 |
nfs端
1、统一用户
[root@web01 pic]# groupadd www -g 666
[root@web01 pic]# useradd www -u 666 -g 666
2、授权(给挂载点)
[root@nfs web]# chown -R www.www /data/
3、修改配置文件
[root@nfs web]# cat /etc/exports
/data/image 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/data/web 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
4、重启nfs-server
[root@nfs web]# systemctl restart nfs-server
web端
1、挂载
[root@web02 ~]# mount -t nfs 172.16.1.31:/data/web /var/www/html/
2、统一web端用户
[root@nfs web]# groupadd www -g 666
[root@nfs web]# useradd www -u 666 -g 666 -r -M -s /sbin/nologin
3、安装http和php服务
[root@web01 backup]# yum install -y httpd php
4、上传代码
[root@web01 backup]# rpm -ql httpd | grep html
[root@web01 backup]# cd /var/www/html
[root@web01 html]# rz kaoshi.zip
[root@web01 html]# unzip kaoshi.zip
5、统一进程用户
[root@web02 html]# vim /etc/httpd/conf/httpd.conf
User www
Group www
6、重启HTTPD
[root@web02 html]# systemctl restart httpd
解决NFS单点故障
1、inotify实时同步(脚本)
#!/bin/bash
inotifywait -mrq --format '%Xe %w %f' -e create,modify,delete,attrib /data/web | while read line
do
rsync -avz /data/web/* rsync_backup@rsync::web --password-file=/etc/rsync.passwd --delete
done
2、把rsync做成一个nfs
1、安装nfs服务端
[root@rsync web]# yum install nfs-utils rpcbind -y
2、设置挂载点
[root@rsync web]# cat /etc/exports
/rsync/web 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
3、统一用户
[root@nfs web]# groupadd www -g 666
[root@nfs web]# useradd www -u 666 -g 666 -r -M -s /sbin/nologin
修改rdync配置文件
[root@rsync web]# vim /etc/rsyncd.conf
[root@rsync web]# cat /etc/rsyncd.conf
uid=www
gid=www
[root@rsync web]# chown -R www.www /rsync/web
4、启动rsync端的nfs
[root@rsync web]# systemctl enable --now nfs-server rpcbind
3、nfs故障自动切换
1、探测nfs服务是否正常
showmount -e 172.16.1.31
2、如果不正常切换rsync为nfs,并挂载
3、rsync与nfs之间的角色切换
#!/bin/bash
IP='172.16.1.31'
IP_BACKUP='172.16.1.21'
while true
do
showmount -e ${IP} >/dev/null 2>&1
if [ $? -ne 0 ];then
# 卸载nfs
umount -lf /var/www/html
# 挂载rsync
mount -t nfs ${IP_BACKUP}:/data/web /var/www/html
TEST=${IP}
IP=${IP_BACKUP}
IP_BACKUP=${TEST}
else
df -h | grep ${IP} >/dev/null 2>&1
[ $? -eq 0 ] || mount -t nfs ${IP}:/data/web /var/www/html
fi
sleep 1
done
sersync实时同步
1.为什么要使用实时同步?
1.解决nfs单点
2.大量的静态资源迁移(本地迁移云端)
2.实时同步能解决什么问题?
1.平滑的迁移
2.备份:减少人为的干预
3.实时同步工具选择?
rsync+inotify 少量文件同步,麻烦。同步大文件太慢,遍历扫描,非常影响效率。
sersync 配置简单,多线程同步,同步块。适合大量的小文件或者图片。
lsryncd
nfs端部署sersync
准备工作
安装rsync和inotify
[root@nfs ~]# yum install -y rsync inotify-tools
1、下载sersync
[root@nfs ~]# wget http://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
2、解压
[root@nfs ~]# tar -xf sersync2.5.4_64bit_binary_stable_final.tar.gz
3、移动
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync2
4、修改配置文件
[root@nfs ~]# confxml.xml confxml.bak #备份配置文件
[root@nfs ~]# vim /usr/local/sersync/confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host> #本机ip地址和端口
<debug start="false"/> #是否打开调试模式
<fileSystem xfs="false"/> #是否支持xfs文件系统
<filter start="false"> #是否过滤,是否排除名称中含有制定字符串的文件的同步
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
#inotify 监控的动作
<delete start="true"/> #删除动作
<createFolder start="true"/> #创建文件夹动作
<createFile start="true"/> #创建文件动作
<closeWrite start="true"/> #写入完成动作
<moveFrom start="true"/> #移动来自动作
<moveTo start="true"/> #移动到动作
<attrib start="true"/> #属性被更改
<modify start="true"/> #修改动作
</inotify>
<sersync>
<localpath watch="/data"> #监控的目录
<remote ip="172.16.1.41" name="web_data"/> #远端rsync服务器的地址和模块
</localpath>
<rsync>
<commonParams params="-az"/> #rsync的参数
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
#开启认证 #虚拟用户 #指定虚拟用户的密码文件
#如果远端rsync服务不是873端口,则开启并修改
<userDefinedPort start="false" port="874"/><!-- port=874 #如果远端rsync服务不是873端口,则开启并修改
<timeout start="false" time="100"/><!-- timeout=100 --> #超时时间
<ssh start="false"/>
</rsync>
#错误日志存储路径
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once--> #定时任务,开启后,600分钟默认全备一次
#定时任务,开启后,600分钟默认全备一次
<crontab start="false" schedule="600"><!--600mins--> #定时任务,开启后,600分钟默认全备一次
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command"> #扩展插件功能的配置举例
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket"> #扩展插件功能的配置举例
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN"> #扩展插件功能的配置举例
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
6.创建密码文件
[root@nfs sersync]# echo "123" > /etc/rsync.password
[root@nfs sersync]# chmod 600 /etc/rsync.password
7.查看sersync命令
[root@nfs sersync]# ./sersync2 -h (相对路径)
[root@nfs ~]# /usr/local/sersync/sersync2 -h (绝对路径)
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
5、启动服务
[root@nfs sersync2]# ./sersync2 -dro confxml1.xml (相对路径)
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml (绝对路径)
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: confxml1.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.passwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/web && rsync -az -R --delete ./ rsync_backup@172.16.1.21::web --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /data/web
拓展分享
1.rstnc配置多个模块及目录
1)rsync服务端
[root@backup ~]# vim /etc/rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = true
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[web_data]
comment = "该备份文件是web端挂载到nfs服务器的文件"
path = /data
[backup]
comment = "该目录备份服务器日常备份文件"
path = /backup
auth users = rsync_user
secrets file = /etc/rsync.password
2)rsync客户端
[root@nfs ~]# rsync -avz . rsync_backup@172.16.1.41::web_data
Password: 123456
sending incremental file list
./
sent 47 bytes received 23 bytes 28.00 bytes/sec
total size is 0 speedup is 0.00
#使用两个模块推送
[root@nfs ~]# rsync -avz . rsync_user@172.16.1.41::backup
Password: 678910
sending incremental file list
./
sent 47 bytes received 23 bytes 20.00 bytes/sec
total size is 0 speedup is 0.00
#使用密码文件推送
[root@nfs ~]# echo '123456' > /etc/rsync.passwd
[root@nfs ~]# echo '678910' > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.passwd
[root@nfs ~]# chmod 600 /etc/rsync.password
[root@nfs ~]# rsync -avz . rsync_backup@172.16.1.41::web_data --password-file=/etc/rsync.passwd
[root@nfs ~]# rsync -avz . rsync_user@172.16.1.41::backup --password-file=/etc/rsync.password
NFS配置多个挂载目录
1)NFS配置多个可挂载目录
[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
/pic 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
2)NFS客户端查看
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/pic 172.16.1.0/24
/data 172.16.1.0/24
3.sersync配置启动多个实时备份
1)第一个配置文件
[root@nfs ~]# cat /usr/local/sersync/confxml.xml
... ...
<host hostip="localhost" port="8008"></host>
<localpath watch="/data">
<remote ip="172.16.1.41" name="web_data"/>
... ...
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.passwd"/>
... ...
... ...
2)第二个配置文件
[root@nfs ~]# cat /usr/local/sersync/backup.xml
... ...
<host hostip="localhost" port="8009"></host>
<localpath watch="/backup">
<remote ip="172.16.1.41" name="backup"/>
... ...
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_user" passwordfile="/etc/rsync.password"/>
... ...
... ...
3)启动多个sersync
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
[root@nfs ~]# /usr/local/sersync/sersync2 -dro /usr/local/sersync/backup.xml
#查看进程
[root@nfs ~]# !ps
ps -ef | grep sersync
root 9601 1 0 17:03 ? 00:00:00 /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
root 9938 1 0 17:52 ? 00:00:00 /usr/local/sersync/sersync2 -dro /usr/local/sersync/backup.xml
root 9976 9270 0 17:56 pts/1 00:00:00 grep --color=auto sersync

浙公网安备 33010602011771号