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主从
相关推荐
王道长服务器 | 亚马逊云16 分钟前
AWS Elemental MediaConvert:视频转码不再难
linux·服务器·网络·云计算·音视频·aws
Jm_洋洋20 分钟前
【Linux系统编程】程序替换:execve(execl、execlp、execle、execv、execvp、execvpe)
linux·运维·c语言·开发语言·程序人生
HIT_Weston28 分钟前
14、【Ubuntu】【VSCode】VSCode 断联问题分析:hostname(二)
linux·vscode·ubuntu
冲上云霄的Jayden1 小时前
bash执行脚本 CondaError: Run ‘conda init‘ before ‘conda activate‘
linux·ubuntu·conda·bash·init·activate
驱动探索者1 小时前
影石Insta360发展史:从深圳公寓到全球影像创新标杆
linux
Wang's Blog1 小时前
Linux小课堂: SSH 免密登录原理与实现之基于公钥认证的安全连接机制
linux·安全·ssh
戴草帽的大z2 小时前
交叉编译FFmpeg:从x264到RK3588部署实战
linux·ffmpeg·rk3588·h.264·aarch64
qiuiuiu4132 小时前
正点原子RK3568学习日志19- Linux错误处理 字符驱动框架完全体
linux·服务器·学习·ubuntu
如果是君2 小时前
【git使用】ubuntu下利用git工具提交一个工程
linux·git·ubuntu
橘颂TA2 小时前
【Linux】 层层递进,抽丝剥茧:调度队列、命令行参数、环境变量
linux·运维·服务器·c/c++