NFS主从同步Rsync、sersync2

准备工作检查selinux 防火墙

bash 复制代码
#关闭 selinux
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
#关闭防火墙
systemctl stop firewalld;systemctl disable firewalld

1.安装nfs相关包

bash 复制代码
# 所有节点安装nfs相关包
yum install nfs-utils -y
systemctl enable nfs-utils --now

2.主节点启动nfs服务

bash 复制代码
systemctl enable nfs-server.service --now

3.主节点创建目录并共享

bash 复制代码
# 创建nfs共享目录
mkdir /data
echo "/data *(rw,sync,insecure,no_subtree_check,no_root_squash)" /etc/exports

# 重启nfs服务
systemctl restart nfs-server.service

# 在其他节点showmount测试
showmount -e 172.25.47.18
Export list for xxx.xxx.xxx.xxx:
/data *

#其他节点测试挂载
mkdir /haha
mount -t nfs 主节点IP地址:/data /haha
umount /haha

4.从节点创建目录

bash 复制代码
mkdir /data

5.配置 rsync

此文档使用以下IP和目录举例,使用时请根据实际情况进行更换
bash 复制代码
主节点:172.16.164.14
从节点:172.16.164.15
同步目录 /data
Bash 复制代码
yum install rsync -y

vi /etc/rsyncd.conf(从节点执行)

bash 复制代码
uid = root
gid = root
use chroot = yes
max connections = 200
log file=/var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
exclude = lost+found/
transfer logging = yes
timeout = 600
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[data]
path = /data
auth users = rsync
secrets file = /etc/rsync_salve.pass
hosts allow = 172.16.164.14
read only = no

6.配置认证文件

从节点执行

格式为:用户:用户密码

bash 复制代码
echo 'rsync:password' > /etc/rsync_salve.pass
chmod 600 /etc/rsync_salve.pass
主节点执行

这里写入从节点配置的密码

bash 复制代码
echo "password" > /etc/rsync.pass
chmod 600 /etc/rsync.pass

7.目录授权

主节点和从节点执行
bash 复制代码
chown -R root:root /data

8.启动服务

从节点执行
bash 复制代码
systemctl enable rsyncd.service
systemctl start rsyncd.service

9.同步测试

主节点执行
bash 复制代码
rsync -arv /data/ rsync@172.16.164.15::data --password-file=/etc/rsync.pass

#如果想在目标目录中删除那些在源目录中不存在的文件可使用
#rsync -avz --progress --delete /data/ rsync@172.16.164.15::data --password-file=/etc/rsync.pass

10.使用 sersync2 配置自动同步(主节点执行)

10.1 安装 sersync2
bash 复制代码
#下载
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5.4_64bit_binary_stable_final.tar.gz
#解压
tar -zxvf sersync*_64bit_binary_stable_final.tar.gz && mv GNU-Linux-x86/ /usr/local/sersync && cd /usr/local/sersync
10.2 修改配置文件
!!注意更换下面的IP地址哦(改为从节点的)!!
bash 复制代码
sed -ri '24s#<localpath watch="/opt/tongbu">#<localpath watch="/data">#g' confxml.xml

sed -ri '25s#<remote ip="127.0.0.1" name="tongbu1"/>#<remote ip="172.16.164.15" name="data"/>#g' confxml.xml

sed -ri '31s#<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>#<auth start="true" users="rsync" passwordfile="/etc/rsync.pass"/>#g' confxml.xml

sed -ri '33s#<timeout start="false" time="100"/><!-- timeout=100 -->#<timeout start="true" time="100"/><!-- timeout=100 -->#g' confxml.xml
10.3 启动
将 sersync2 纳入 system管理
bash 复制代码
cat > /etc/systemd/system/sersync2.service << EOF
[Unit]
Description=Sersync2 Service
After=network.target

[Service]
RemainAfterExit=yes
ExecStart=/usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
ExecStop=/usr/bin/pkill sersync2
ExecReload=/usr/bin/pkill sersync2 && /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
Restart=on-abnormal

[Install]
WantedBy=multi-user.target
EOF
启动 sersync2
bash 复制代码
systemctl daemon-reload
systemctl enable sersync2
systemctl start sersync2

11.测试主从是否同步

bash 复制代码
#主节点执行
echo 123 >/data/test.tt

#从节点执行
cat /data/test.txt
#如果看到结果是123即表示成功搭建了NFS主从
相关推荐
xuanyu2233 分钟前
Linux常用指令
linux·运维·人工智能
KookeeyLena51 小时前
云手机可以挂在服务器使用吗?
运维·服务器·智能手机
老汉忒cpp1 小时前
手动部署并测试内网穿透(ssh 和 nginx)
运维·服务器
有时间要学习1 小时前
Linux——应用层自定义协议与序列化
linux·服务器·网络
阑梦清川1 小时前
简明linux系统编程--互斥锁--TCP--UDP初识
linux·udp·tcp·互斥锁·信号·解锁·加锁
hardStudy_h1 小时前
Linux——常用系统设置和快捷键操作指令
linux·运维·服务器
我叫啥都行2 小时前
计算机基础知识复习9.7
运维·服务器·网络·笔记·后端
XF鸭2 小时前
TCP 和 UDP 协议的区别?
linux
qq 1778036223 小时前
智能新时代,游戏盾守护顺畅体验
运维·服务器·网络·游戏·云计算·ddos·ip
Flying_Fish_roe3 小时前
linux-安全管理-防火墙与网络安全
linux·数据库·oracle