[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 如果只需要备份 不需要同步 数据需要同步 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 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 
 
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 yum install -y rsync vim /etc/rsyncd.conf uid = www gid = www port = 873 fake super = yes  use chroot  = no max connections = 200 timeout  = 600ignore 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.passwdchmod  600 /etc/rsync.passwdmkdir  /{wp,zh}_datachown  www:www /{wp,zh}_datasystemctl 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 yum install -y nfs-utils yum install -y nfs-utils 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) 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) groupadd www -g 666 useradd www -g 666 -u 666 -s /sbin/nologin/ -M mkdir  /{wp,zh}_datachown  www:www /{wp,zh}_datasystemctl start nfs && systemctl enable  nfs 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 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  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 groupadd www -g 666 && useradd www -g 666 -u 666 -s /sbin/nologin/ -M 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 mysqladmin -uroot password '123'  mysql -uroot -p123 MariaDB [(none)]> create database wordpress charset utf8; MariaDB [(none)]> create database wecenter charset utf8; 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 
部署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 yum install -y nfs-utils showmount -e 172.16.1.31 mkdir  /code/wordpress/wp-content/uploadsmkdir  /code/wordpress/wp-content/uploadsmount -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/ mount -t nfs 172.16.1.31:/zh_data /code/wecenter/uploads/ mount -t nfs 172.16.1.31:/zh_data /code/wecenter/uploads/ 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 wget http://test.driverzeng.com/other/sersync2.5.4_64bit_binary_stable_final.tar.gz yum install -y rsync inotify-tools mkdir  /apptar xf /root/sersync2.5.4_64bit_binary_stable_final.tar.gz -C /app/ mv  /app/GNU-Linux-x86/ /app/sersyncvim /app/sersync/confxml.xml <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" (你要同步的主机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" /> (打开密码认证) (匿名用户) 认证密码文件) <?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> <?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.passwordchmod  600 /etc/rsync.password/app/sersync/sersync2 -rdo wp_data.xml /app/sersync/sersync2 -rdo zh_data.xml