[toc]

seRsync使用

实现架构

1.实现LNMP架构

2.独立设备的数据库

3.NFS共享存储

4.实现用户数据的sersync实时同步,(nfs服务器中/data的数据实时同步到backup服务器)

rsync结合inotify实时同步

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# inotify的使用场景
如果只需要备份 不需要同步 数据需要同步

# 安装inotify-tools
yum -y install inotify-tools

# 选项
-m 持续监控
-r 递归
-q 静默,仅打印时间信息
--timefmt 指定输出时间格式
--format 指定事件输出格式
%Xe 事件
%w 目录
%f 文件
-e 指定监控的事件
access 访问
modify 内容修改
attrib 属性修改
close_write 修改真实文件内容
open 打开
create 创建
delete 删除
umount 卸载

/usr/bin/inotifywait -mrq --format '%Xe %w %f' -e
create,modify,delete,attrib,close_write /backup

# 修改配置脚本文件
vim rsyn-inotify.sh

#!/bin/bash
dir=/backup
/usr/bin/inotifywait -mrq --format '%w %f' -e create,delete,attrib,close_write $dir| while read line;do
cd $dir && rsync -az -R --delete . rsync_backup@172.16.1.31::backup --password-file=/etc/rsync.passwd >/dev/null 2>&1
done &

部署seRsync实时同步

准备环境

主机名 WanIP LanIP 角色 安装应用
web01 10.0.0.7 172.16.1.7 web网站,nfs客户端 nginx php nfs wordpress wecenter
web02 10.0.0.8 172.16.1.8 web网站,nfs客户端 nginx php nfs wordpress wecenter
db01 10.0.0.51 172.16.1.51 数据库 mariadb
nfs 10.0.0.31 172.16.1.31 nfs服务端,rsync客户端 nfs-utils sersync rsync inotify
backup 10.0.0.41 172.16.1.41 rsync服务端,nfs客户端 rsync nfs-utils

image-20221223152617379

rsync服务端部署(backup)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 安装rsync
yum install -y rsync
# 修改rsync的配置文件
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 = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[wp_data]
comment = welcome to backup
path = /wp_data

[zh_data]
comment = welcome to backup
path = /zh_data

# 创建用户和组
groupadd www -g 666
useradd www -g 666 -u 666 -s /sbin/nologin/ -M

# 创建密码文件
echo 'rsync_backup:123' > /etc/rsync.passwd

# 授权密码文件权限
chmod 600 /etc/rsync.passwd

# 创建备份目录
mkdir /{wp,zh}_data

# 授权备份目录
chown www:www /{wp,zh}_data

# 启动并加入开机自启
systemctl start rsyncd && systemctl enable rsyncd

NFS服务端部署(NFS Backup)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 安装NFS服务
## NFS
yum install -y nfs-utils
## Backup
yum install -y nfs-utils

# 编辑配置文件
## NFS
vim /etc/exports
/wp_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)
/zh_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)
## backup
vim /etc/exports
/wp_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)
/zh_data 172.16.1.0/24(rw,sync,anonuid=666,anongid=666,all_squash)

# 创建www用户和组
groupadd www -g 666
useradd www -g 666 -u 666 -s /sbin/nologin/ -M

# 创建共享目录
mkdir /{wp,zh}_data

# 授权www权限给共享目录
chown www:www /{wp,zh}_data

# 启动服务并加入开机自启
## nfs
systemctl start nfs && systemctl enable nfs
## backup
systemctl start nfs && systemctl enable nfs

部署web服务器(web01 web02)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# 修改nginx的yum源
## web01 & web02
vim /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

# 安装nginx和php
## web01 & web02
yum install -y nginx
yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71wopcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb
rpm -ivh *.rpm

# 创建www用户
## web01 & web02
groupadd www -g 666 && useradd www -g 666 -u 666 -s /sbin/nologin/ -M

# 修改nginx php配置文件
## web01 & web02
vim /etc/nginx/nginx.conf
user www;

vim /etc/php-fpm.d/www.conf
[www]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = www
; RPM: Keep a group allowed to write in log dir.
group = www
; The address on which to accept FastCGI requests.
; Valid syntaxes are:
; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on
; a specific port;
; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on
; a specific port;
; 'port' - to listen on a TCP socket to all addresses
; (IPv6 and IPv4-mapped) on a specific port;
; '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
;listen = 127.0.0.1:9000
listen = /opt/xxx.sock
listen.owner = www
listen.group = www

# 编写wordpress网页配置文件(nginx)
## web01 & web02
vim /etc/nginx/conf.d/wp.conf
server {
listen 80;
server_name blog.xxx.com;
root /code/wordpress;

location / {
index index.php index.html;
}

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/opt/xxx.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

vim /etc/nginx/conf.d/zh.conf
server {
listen 80;
server_name zh.xxx.com;
root /code/wordpress;

location / {
index index.php index.html;
}

location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/opt/xxx.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}

# 将nginx和php-fpm服务启动并加入开机自启
## web01 & web02
systemctl start nginx &&systemctl enable nginx
systemctl start php-fpm && systemctl enable php-fpm

# 创建站点目录
## web01 & web02
mkdir /code

# 下载wordpress wecenter网页代码
## web01 & web02
wget http://test.driverzeng.com/Nginx_Code/wordpress-5.0.3-zh_CN.tar.gz
wget http://test.driverzeng.com/Nginx_Code/WeCenter_3-2-1.zip

# 授权站点目录
## web01 & web02
chown -R www:www wordpress/
chown -R www:www wecenter/

部署mariadb数据库(db01)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 安装数据库
yum install -y mariadb-server

# 启动服务并加入开机自启
systemctl start mariadb && systemctl enable mariadb

# 设置数据库的管理用户root和密码
mysqladmin -uroot password '123'

# 连接数据库
mysql -uroot -p123

# 创建wordpress和wecenter数据库
MariaDB [(none)]> create database wordpress charset utf8;
MariaDB [(none)]> create database wecenter charset utf8;

# 创建wordpress和wecenter数据库的用户及密码
grant all on wordpress.* to wp_user@'172.16.1.%' identified by '123';
grant all on wecenter.* to zh_user@'172.16.1.%' identified by '123';

# 域名解析
10.0.0.7 blog.xxx.com zh.xxx.com
10.0.0.8 blog.xxx.com zh.xxx.com

image-20221225161405526

image-20221225161612377

部署NFS客户端实现共享存储(web01 web02)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 安装nfs
## web01 & web02
yum install -y nfs-utils

# 查看挂载点
## web01 & web02
showmount -e 172.16.1.31

# 创建wp共享目录
## web01 & web02
mkdir /code/wordpress/wp-content/uploads
mkdir /code/wordpress/wp-content/uploads

# 挂载wp共享目录
## web01 & web02
mount -t nfs 172.16.1.31:/wp_data /code/wordpress/wp-content/uploads/
mount -t nfs 172.16.1.31:/wp_data /code/wordpress/wp-content/uploads/

# 挂载zh共享目录
## web01 & web02
mount -t nfs 172.16.1.31:/zh_data /code/wecenter/uploads/
mount -t nfs 172.16.1.31:/zh_data /code/wecenter/uploads/

# 查看挂载
## web01 & web02
df -h

rsync客户端部署使用sersync实现实时同步(nfs)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# 下载sersync
wget http://test.driverzeng.com/other/sersync2.5.4_64bit_binary_stable_final.tar.gz

# 安装依赖
yum install -y rsync inotify-tools

# 创建sersync的安装目录
mkdir /app

# 解压sersync
tar xf /root/sersync2.5.4_64bit_binary_stable_final.tar.gz -C /app/

# 改名
mv /app/GNU-Linux-x86/ /app/sersync

# 配置文件解析
vim /app/sersync/confxml.xml
## 全部改成true
<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>
## 修改rsync配置信息
<sersync>
<localpath watch="/zh_data"(你要监控的目录)>
<remote ip="172.16.1.41"(你要同步的主机IP)name="zh_data"/>(模块名)
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>(rsync的选项,为什么不用加--delete最会帮你执行)
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
(打开密码认证) (匿名用户) 认证密码文件)

# zh_data文件内容
<?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="/zh_data">
<remote ip="172.16.1.41" name="zh_data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<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>

# wp_data文件内容
<?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="/wp_data">
<remote ip="172.16.1.41" name="wp_data"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-az"/>
<auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
<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>

# 创建免密文件并授权
echo '123' > /etc/rsync.password
chmod 600 /etc/rsync.password

# 启动sersync
/app/sersync/sersync2 -rdo wp_data.xml
/app/sersync/sersync2 -rdo zh_data.xml