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主从
相关推荐
花嫁代二娃4 小时前
Linux:环境变量
linux
乌托邦的逃亡者5 小时前
Docker的/var/lib/docker/目录占用100%的处理方法
运维·docker·容器
ldj20205 小时前
Jenkins 流水线配置
运维·jenkins
古希腊数通小白(ip在学)7 小时前
stp拓扑变化分类
运维·服务器·网络·智能路由器
l1x1n09 小时前
Vim 编辑器常用操作详解(新手快速上手指南)
linux·编辑器·vim
12点一刻9 小时前
搭建自动化工作流:探寻解放双手的有效方案(2)
运维·人工智能·自动化·deepseek
未来之窗软件服务9 小时前
东方仙盟AI数据中间件使用教程:开启数据交互与自动化应用新时代——仙盟创梦IDE
运维·人工智能·自动化·仙盟创梦ide·东方仙盟·阿雪技术观
ajassi200010 小时前
开源 python 应用 开发(三)python语法介绍
linux·python·开源·自动化
o不ok!10 小时前
Linux面试问题-软件测试
linux·运维·服务器
DaxiaLeeSuper11 小时前
Prometheus+Grafana+node_exporter监控linux服务器资源的方案
linux·grafana·prometheus