Linux:Sersync+Rsync+LNP实时同步

环境

角色IP地址主机名
rsync服务器10.0.0.41backup
共享存储与实时同步10.0.0.31nfs
web服务器10.0.0.7web01

web的页面及数据都保存在nfs上的共享存储上,我们通过web上传的文件,会自动同步到backup中

NFS服务器

NFS服务部署

1、安装软件

yum install -y nfs-utils

已经安装过的显示:
在这里插入图片描述

2、编写配置文件

  • 编辑NFS服务配置文件
vim /etc/exports
  • 内容
#共享目录为:/backup
#允许访问IP:* 所有IP
#访问权限:rw 读写
#访问者身份为:UID与GID为666的本地用户
/backup  *(rw,all_squash,anonuid=666,anongid=666)

在这里插入图片描述

3、创建自定义程序用户

  • 创建
groupadd -g666 www
useradd -u666 -g666 -M -s /sbin/nologin www
  • 查看
id www

在这里插入图片描述

4、创建共享目录,并修改归属

  • 创建
mkdir -p /backup
chown -R www.www /backup
  • 查看
ll -d /backup

在这里插入图片描述

5、启动或重启nfs

  • 启动nfs服务和设置为开机自启
systemctl start nfs
systemctl enable nfs
  • 重新挂载(重新加载配置文件)和显示共享目录
exportfs -rv

在这里插入图片描述

6、显示NFS服务器的共享信息

showmount -e

在这里插入图片描述

Sersync部署

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

安装过的显示:
在这里插入图片描述

安装sersync
1、获取sersync包
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz

在这里插入图片描述

2、解压
tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

在这里插入图片描述

3、将解压后的路径放在常用的路径
mkdir -p /app
mv GNU-Linux-x86/ /app/sersync
ls /app/sersync

在这里插入图片描述

4、修改sersync的配置文件,和当前环境匹配
  • 编辑confxml.xml文件
vim /app/sersync/confxml.xml
  • 文件内容
...
	<fileSystem xfs="true"/>
...
    <inotify>
		<delete start="true"/>
		<createFolder start="true"/>
		<createFile start="true"/>
		<closeWrite start="true"/>
		<moveFrom start="true"/>
		<moveTo start="true"/>
		<attrib start="false"/>
		<modify start="false"/>
    </inotify
    <sersync>
    	<localpath watch="/backup">
	    	<remote ip="10.0.0.41" name="backup"/>
		</localpath>
		<rsync>
	    	<commonParams params="-az"/>
	    	<auth start="true" users="rsync_user" passwordfile="/etc/rsync.password"/>
	    	...
		</rsync>
		...
    </sersync>
5、创建rsync客户端认证的密码文件
echo 1 > /etc/rsync.password
chmod 600 /etc/rsync.password

在这里插入图片描述

6、运行sersync
/app/sersync/sersync2 -do /app/sersync/confxml.xml

Web01服务器

1、安装软件

  • nginx
  1. 下载RPM包
wget http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm
  1. 安装
yum localinstall -y nginx-1.20.2-1.el7.ngx.x86_64.rpm
  • php
  1. 准备php72.tar.gz包,并解压
tar xf php72.tar.gz
  1. 安装
yum localinstall -y php72/*

2、创建自定义程序用户

  • 创建
groupadd -g666 www
useradd -u666 -g666 -M -s /sbin/nologin www
  • 查看
id www

在这里插入图片描述

3、编写nginx、php-fpm配置文件

#修改运行用户,当运行NGINX时,进程所使用的用户,则进程拥有该用户对文件或目录的操作权限。
sed -ri '/^user/c user www;' /etc/nginx/nginx.conf
#修改php-fpm运行用户
sed -ri '/^(user|group)/s#apache#www#' /etc/php-fpm.d/www.conf
#修改上传文件大小的最大值
sed -ri '/^upload/c upload_max_filesize = 20M' /etc/php.ini
#修改通过表单POST给PHP的所能接收的最大值,包括表单里的所有值。
sed -ri '/^post/c post_max_size = 20M' /etc/php.ini

在这里插入图片描述

4、编写站点upload.chen.com的配置文件

  • 编辑文件
vim /etc/nginx/conf.d/upload.conf
  • 文件内容
server {
	server_name upload.chen.com;
	listen 80;
	charset utf-8,gbk;
	client_max_body_size 20M;

	location / {
		root /web/upload/;
		index index.html;
		access_log logs/upload_access.log main;
		error_log logs/upload_error.log;
	}

	location ~ \.php$ {
		root /web/upload/;
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}
}

5、创建web目录和nginx的日志目录

  • 创建nginx的日志目录
mkdir -p /etc/nginx/logs/
  • 创建web目录
mkdir -p /web/upload

6、挂载NFS

  • 查看nfs服务器的共享目录
showmount -e 10.0.0.31

在这里插入图片描述

  • 将nfs服务器共享目录写入永久挂载文件
echo "10.0.0.31:/backup /web/upload nfs defaults 0 0" >> /etc/fstab
  • 重新加载挂载配置文件
mount -a

在这里插入图片描述

7、重启或启动nginx、php-fpm服务

  • 重启服务
systemctl restart nginx php-fpm
  • 开机自启
systemctl enable nginx php-fpm

在这里插入图片描述

8、上传文件

  • 解压文件到指定目录
unzip kaoshi.zip -d /web/upload/

在这里插入图片描述

Backup服务器

1、安装软件

yum install -y rsync

在这里插入图片描述

2、编写配置文件

  • 编写rsync配置文件
vim /etc/rsyncd.conf
  • 文件内容
#全局模块
#运行进程的用户
uid = www
#运行进程的用户组
gid = www
#监听端口
port = 873
#无需让rsync以root身份运行,允许存储文件的完整属性
fake super = yes
#关闭假根功能,无法切换路径
use chroot = no
#最大连接数
max connections = 200
#超时时间
timeout = 600
#忽略错误信息
ignore errors
#对备份数据可读写
read only = false
#不允许查看模块信息
list = false
#定义虚拟用户,作为连接认证用户
auth users = rsync_user
#定义rsync服务用户连接认证密码文件路径
secrets file = /etc/rsync.password
#定义日志文件路径
log file = /var/log/rsyncd.log
#局部模块
#定义模块信息
[backup]
#模块注释信息
comment = welcome to softeem backup!
#定义接收备份数据目录
path = /backup

在这里插入图片描述

3、创建自定义程序用户

  • 创建
groupadd -g666 www
useradd -u666 -g666 -M -s /sbin/nologin www
  • 查看
id www

在这里插入图片描述

4、创建共享目录,并修改归属

  • 创建目录
mkdir -p /backup
  • 修改归属
chown -R www.www /backup

在这里插入图片描述

5、创建自定义认证文件,并改权限

  • 创建认证文件
echo "rsync_user:1" >/etc/rsync.password
  • 修改权限,避免出现权限问题
chmod 600 /etc/rsync.password

在这里插入图片描述

6、启动或重启rsyn

  • 重启
systemctl restart rsyncd
  • 开机自启
systemctl enable rsyncd

在这里插入图片描述

测试

1、通过web上传文件

访问站点

upload.chen.com

在这里插入图片描述

2、通过nfs修改文件

vim /backup/2022-10-19/77_chen.txt

在这里插入图片描述

3、通过backup监控文件变化

watch cat /backup/2022-10-20/77_chen.txt

在这里插入图片描述

4、修改nfs上文件的内容

echo 88888888 >> /backup/2022-10-19/77_chen.txt

观察backup监控的变化
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值