目录
各服务器环境
1、创建gjs用户和组
[root@host ~]# groupadd -g 666 gjs
[root@host ~]# useradd -u 666 -g 666 gjs
2、关闭防火墙和selinux
[root@nfs ~]# systemctl stop firewalld
[root@nfs ~]# systemctl disable firewalld
[root@nfs ~]# getenforce 查看是否开启
[root@nfs ~]# setenforce 0
[root@nfs ~]# vim /etc/selinux/config
SELINUX=disabled
备份服务器backup
rsyncd服务端
1># 安装rsyncd,rsync的守护进程
[root@backup ~]# yum install -y rsyncd
2># 写配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = gjs
gid = gjs
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = gjs_backup
secrets file = /etc/rsyncd.passwd
log file = /var/log/rsyncd.log
#################################
[upload]
comment = welcome to backup!
path = /web_upload
3># 创建备份的目录和密码文件
[root@backup ~]# mkdir /web_upload
[root@backup ~]# chown -R gjs.gjs /web_upload/
[root@backup ~]# echo 'gjs_backup:123456' > /etc/rsyncd.passwd
[root@backup ~]# cat /etc/rsyncd.passwd
gjs_backup:123456
[root@backup ~]# chmod 600 /etc/rsyncd.passwd
4># 启动rsyncd并检查
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# netstat -lntup | grep rsync
[root@backup ~]# ps aux | grep rsync
root 1149 0.0 0.1 114748 1124 ? Ss 02:54 0:00 /usr/bin/rsync --daemon --no-detach
NFS服务端
1>#安装NFS和rpcbind
[root@nfs ~]# yum install -y nfs-utils rpcbind
#如果是centos6需要单独安装rpcbind,centos7可以不单独安装
2>#创建数据目录 用于挂载到web服务器
[root@nfs ~]# mkdir /web_upload
[root@nfs ~]# chown -R gjs.gjs /web_upload
3>#配置/etc/exports
[root@nfs ~]# vim /etc/exports
/web_upload 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
4>#启动NFS服务
[root@nfs ~]# systemctl start rpcbind nfs-server
#如果是centos6,启动时必须先启动rpcbind,centos7会自动启动
#验证启动
[root@nfs ~]# ps -ef | grep nfs
5>#验证NFS配置
[root@nfs ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
文件服务器nfs
rsyncd客户端
# 安装rsyncd,rsync守护进程
[root@backup ~]# yum install -y rsyncd
# 创建密码文件
[root@backup ~]# echo '123456' > /etc/rsync.passwd
[root@backup ~]# cat /etc/rsync.passwd
123456
# 将密码文件的权限改成600
[root@backup ~]# chmod 600 /etc/rsync.passwd
# 启动rsync
[root@backup ~]# systemctl start rsyncd
# 检查启动情况
[root@backup ~]# netstat -lntup | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1149/rsync
tcp6 0 0 :::873 :::* LISTEN 1149/rsync
[root@backup ~]# ps aux | grep rsync
root 1149 0.0 0.1 114748 1124 ? Ss 02:54 0:00 /usr/bin/rsync --daemon --no-detach
NFS服务端
1>#安装NFS和rpcbind
[root@nfs ~]# yum install -y nfs-utils rpcbind
#如果是centos6需要单独安装rpcbind,centos7可以不单独安装
2>#配置/etc/exports
NFS服务程序的配置文件为/etc/exports,需要严格按照共享目录的路径 允许访问的NFS客户端(共享权限参数)格式书写,定义要共享的目录与相应的权限,具体书写方式如下图所示。
[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash)
| 语法 | /data | 172.16.1.0/24 | (rw,sync,all_squash) |
| -------- | ---------- | -------------------- | ------------------------- |
| 语法含义 | 共享的目录 | 允许连接的客户端网段 | 允许的操作(参数1,参数2) |
3>#创建数据目录 用于挂载到web服务器
[root@nfs ~]# mkdir /data
[root@nfs ~]# chown -R gjs.gjs /data
4>#启动NFS服务
[root@nfs ~]# systemctl start rpcbind nfs-server
#如果是centos6,启动时必须先启动rpcbind,centos7会自动启动
#验证启动
[root@nfs ~]# ps -ef | grep nfs
5>#验证NFS配置
[root@nfs ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
sersync服务
# 安装rsync和inotify
[root@nfs01 ~]# yum install rsync inotify-tools -y
# sersync文件放入指定目录
[root@nfs01 ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
# 配置sersync配置文件
[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>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*).svn"></exclude>
<exclude expression="(.*).gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<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="/web_upload">
<remote ip="172.16.1.41" name="upload"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="gjs_backup" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<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-->
<crontab start="false" schedule="600"><!--600mins-->
<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>
# 创建密码文件
[root@nfs ~]# echo 123456 > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password
# 启动
# 查看启动参数
[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命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
# 指定配置文件(同步多个)
[root@nfs /web_upload]# /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: /usr/local/sersync/confxml.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 gjs_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 /web_upload && rsync -artuz -R --delete ./ gjs_backup@172.16.1.41::upload --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /web_upload
用户服务器web
配置httpd服务
1># 安装httpd和php服务
[root@web01 ~]# yum install -y httpd php
2># 配置httpd文件
[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# unzip kaoshi.zip -d /var/www/html/
[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User gjs
Group gjs
3># 启服务
[root@web01 ~]# systemctl start httpd
4># 修改权限
[root@web01 ~]# chown -R gjs.gjs /var/www/html/
rsyncd客户端
1># 安装rsyncd,rsync守护进程
[root@backup ~]# yum install -y rsyncd
2># 创建目录
[root@backup ~]# mkdir /backup
3># 创建密码文件
[root@backup ~]# echo '123456' > /etc/rsyncd.passwd
[root@backup ~]# cat /etc/rsyncd.passwd
123456
4># 将密码文件的权限改成600
[root@backup ~]# chmod 600 /etc/rsyncd.passwd
5># 启动rsync
[root@backup ~]# systemctl start rsyncd
6># 检查启动情况
[root@backup ~]# netstat -lntup | grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1149/rsync
tcp6 0 0 :::873 :::* LISTEN 1149/rsync
[root@backup ~]# ps aux | grep rsync
root 1149 0.0 0.1 114748 1124 ? Ss 02:54 0:00 /usr/bin/rsync --daemon --no-detach
NFS客户端
1>#安装NFS和rpcbind
[root@nfs ~]# yum install -y nfs-utils rpcbind
2>#查看挂载点并挂载
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web_upload 172.16.1.0/24
3>#启动rpcbind
[root@web01 ~]# systemctl start rpcbind
4>#挂载测试
[root@web01 ~]# mount -t nfs 172.16.1.31:/web_upload /var/www/html/upload
5># 验证是否挂载成功
[root@web01 ~]# df -h
172.16.1.31:/web_upload 16G 1.7G 15G 11% /var/www/html/upload
6># 查看nfs与web端的上传文件
[root@web01 ~]# ll /var/www/html/upload
[root@nfs ~]# ll /web_upload/
NFS故障
# nfs出现故障
[root@nfs ~]# systemctl stop nfs
# 切换挂载机器
[root@web01 ~]# umount -lf /var/www/html/upload
[root@web01 ~]# mount -t nfs 172.16.1.41:/data /var/www/html/upload
#浏览器上传文件
[root@web01 /var/www/html/upload]# ll
total 720
-rw-r--r-- 1 gjs gjs 736514 Aug 16 20:57 22_web02.jpg
[root@nfs /web_upload]# ll
total 720
-rw-r--r-- 1 gjs gjs 736514 Aug 16 20:57 22_web02.jpg
[root@backup /web_upload]# ll
total 720
-rw-r--r-- 1 gjs gjs 736514 Aug 16 20:57 22_web02.jpg
三服务详细步骤
各服务器环境
主机 | IP | 安装服务 |
---|---|---|
web01 | 172.16.1.7 | httpd php rsyncd nfs-utils rpcbind |
NFS | 172.16.1.31 | rsyncd nfs-utils rpcbind |
backup | 172.16.1.41 | rsyncd nfs-utils rpcbind |
1、创建gjs用户和组
[root@host ~]# groupadd -g 666 gjs
[root@host ~]# useradd -u 666 -g 666 gjs
2、关闭防火墙和selinux
[root@nfs ~]# systemctl stop firewalld
[root@nfs ~]# systemctl disable firewalld
[root@nfs ~]# getenforce 查看是否开启
[root@nfs ~]# setenforce 0
[root@nfs ~]# vim /etc/selinux/config
SELINUX=disabled
3、NFS服务端配置
1>#安装NFS和rpcbind
[root@nfs ~]# yum install -y nfs-utils rpcbind
#如果是centos6需要单独安装rpcbind,centos7可以不单独安装
2>#创建数据目录 用于挂载到web服务器
[root@nfs ~]# mkdir /web_upload
[root@nfs ~]# chown -R gjs.gjs /web_upload
3>#配置/etc/exports
[root@nfs ~]# vim /etc/exports
/web_upload 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
4>#启动NFS服务
[root@nfs ~]# systemctl start rpcbind nfs-server
#如果是centos6,启动时必须先启动rpcbind,centos7会自动启动
#验证启动
[root@nfs ~]# ps -ef | grep nfs
5>#验证NFS配置
[root@nfs ~]# cat /var/lib/nfs/etab
/data 172.16.1.0/24(rw,sync,wdelay,hide,nocrossmnt,secure,root_squash,all_squash,no_subtree_check,secure_locks,acl,no_pnfs,anonuid=65534,anongid=65534,sec=sys,rw,secure,root_squash,all_squash)
4、httpd服务端配置
1># 安装httpd和php服务
[root@web01 ~]# yum install -y httpd php
2># 配置httpd文件
[root@web01 ~]# rz kaoshi.zip
[root@web01 ~]# unzip kaoshi.zip -d /var/www/html/
[root@web01 ~]# vim /etc/httpd/conf/httpd.conf
User gjs
Group gjs
3># 修改权限
[root@web01 ~]# chown -R gjs.gjs /var/www/html/
4># 启服务
[root@web01 ~]# systemctl start httpd
5># 浏览器上传文件
[root@web01 ~]# ll /var/www/html/upload
5、nfs客户端配置
1>#安装NFS和rpcbind
[root@nfs ~]# yum install -y nfs-utils rpcbind
2>#查看挂载点并挂载
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/web_upload 172.16.1.0/24
3>#启动rpcbind
[root@web01 ~]# systemctl start rpcbind
4>#挂载测试
[root@web01 ~]# mount -t nfs 172.16.1.31:/web_upload /var/www/html/upload
5># 验证是否挂载成功
[root@web01 ~]# df -h
172.16.1.31:/web_upload 16G 1.7G 15G 11% /var/www/html/upload
6、上传测试
# 浏览器10.0.0.7上传文件
# 查看nfs与web端的文件
[root@web01 ~]# ll /var/www/html/upload
[root@nfs ~]# ll /web_upload/
7、rsyncd服务端配置
1># 安装rsyncd,rsync的守护进程
[root@backup ~]# yum install -y rsyncd
2># 写配置文件
[root@backup ~]# vim /etc/rsyncd.conf
uid = gjs
gid = gjs
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = gjs_backup
secrets file = /etc/rsyncd.passwd
log file = /var/log/rsyncd.log
#################################
[upload]
comment = welcome to backup!
path = /web_upload
3># 创建备份的目录和密码文件
[root@backup ~]# mkdir /web_upload
[root@backup ~]# chown -R gjs.gjs /web_upload/
[root@backup ~]# echo 'gjs_backup:123456' > /etc/rsyncd.passwd
[root@backup ~]# cat /etc/rsyncd.passwd
gjs_backup:123456
[root@backup ~]# chmod 600 /etc/rsyncd.passwd
4># 启动rsyncd并检查
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# netstat -lntup | grep rsync
[root@backup ~]# ps aux | grep rsync
root 1149 0.0 0.1 114748 1124 ? Ss 02:54 0:00 /usr/bin/rsync --daemon --no-detach
5># NFS推送文件测试rsyncd
8、NFS推送文件测试
# 确保rsyncd服务正常运行
[root@nfs /web_upload]# rsync -axz ./ gjs_backup@172.16.1.41::upload --password-file=/etc/rsync.passwd
9、sersync服务端配置
1># 安装rsync和inotify
[root@nfs01 ~]# yum install rsync inotify-tools -y
2># sersync文件放入指定目录
[root@nfs01 ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
3># 配置sersync配置文件
[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>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*).svn"></exclude>
<exclude expression="(.*).gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<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="/web_upload">
<remote ip="172.16.1.41" name="upload"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="gjs_backup" passwordfile="/etc/rsync.passwd"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<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-->
<crontab start="false" schedule="600"><!--600mins-->
<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>
4># 创建密码文件
[root@nfs ~]# echo 123456 > /etc/rsync.password
[root@nfs ~]# chmod 600 /etc/rsync.password
5># 查看启动参数
[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命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
6># 指定配置文件(同步多个)
[root@nfs /web_upload]# /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: /usr/local/sersync/confxml.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 gjs_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 /web_upload && rsync -artuz -R --delete ./ gjs_backup@172.16.1.41::upload --password-file=/etc/rsync.passwd >/dev/null 2>&1
run the sersync:
watch path is: /web_upload
7># 手动推送一遍
[root@nfs ~]# cd /web_upload && rsync -artuz -R --delete ./ gjs_backup@172.16.1.41::upload --password-file=/etc/rsync.passwd
10、测试三服务器是否同步
#浏览器上传文件
[root@web01 ~]# ll /var/www/html/upload
total 720
-rw-r--r-- 1 gjs gjs 736514 Aug 16 20:57 22_web02.jpg
[root@nfs ~]# ll /web_upload
total 720
-rw-r--r-- 1 gjs gjs 736514 Aug 16 20:57 22_web02.jpg
[root@backup ~]# ll /web_upload
total 720
-rw-r--r-- 1 gjs gjs 736514 Aug 16 20:57 22_web02.jpg