Kylin系统的rsync+nfs+lrsync服务
rsync+nfs+lrsync服务

目录
主机信息
| 主机角色 | 外网IP | 内网IP | 主机名 |
|---|---|---|---|
| nfs、lsync | 10.0.0.31 | 176.16.1.31 | nfs |
| 客户端 | 10.0.0.7 | 176.16.1.7 | web01 |
| rsync、nfs | 10.0.0.41 | 172.16.1.41 | backup |
部署流程
1.backup服务器部署rsync
--下载rsync服务
[root@backup ~]# yum install -y rsync
--配置rsync服务
[root@backup ~]# vim /etc/rsyncd.conf
uid = rsync
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
[backup]
path = /backup #存放系统重要文件
[nfs]
path = /nfs #存放用户数据文件
--创建必要文件(组、用户、匿名用户密码文件、模块目录、对应权限)
[root@backup ~]# groupadd -g 666 rsync
[root@backup ~]# useradd -g 666 -u 666 -M -s /sbin/nologin rsync
[root@backup ~]# id rsync
uid=666(rsync) gid=666(rsync) groups=666(rsync)
[root@backup ~]# echo rsync_backup:123 > /etc/rsync.passwd
[root@backup ~]# chmod 600 /etc/rsync.passwd
[root@backup ~]# mkdir /backup /nfs
[root@backup ~]# chown rsync.rsync /backup /nfs
[root@backup ~]# ll -d /backup /nfs /etc/rsync.passwd
drwxr-xr-x 2 rsync rsync 6 Dec 2 16:29 /backup
-rw------- 1 root root 17 Dec 2 16:29 /etc/rsync.passwd
drwxr-xr-x 2 rsync rsync 6 Dec 2 16:29 /nfs
--启动服务
[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyncd
2.web01测试rsync服务
--向两个模块推送内容测试是否成功
[root@web01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
[root@web01 ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::nfs
3.nfs服务器部署nfs服务
--下载nfs服务
[root@nfs ~]# yum install -y nfs-utils
--配置nfs服务(指定运行服务的用户uid、gid)
[root@nfs ~]# vim /etc/exports
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
--创建必要信息(与backup统一用户信息,都使用rsync用户)
[root@nfs ~]# groupadd -g 666 rsync
[root@nfs ~]# useradd -g 666 -u 666 -M -s /sbin/nologin rsync
[root@nfs ~]# id rsync
uid=666(rsync) gid=666(rsync) groups=666(rsync)
[root@nfs ~]# mkdir /data
[root@nfs ~]# chown rsync.rsync /data/
--启动服务
[root@nfs ~]# systemctl start nfs
[root@nfs ~]# systemctl enable nfs
4.web01挂载nfs:/data
--查看nfs共享的目录
[root@web01 ~]# showmount -e 172.16.1.31
Export list for 172.16.1.31:
/data 172.16.1.0/24
--挂载共享目录
[root@web01 ~]# mkdir /imag
[root@web01 ~]# mount -t nfs 172.16.1.31:/data /imag
5.nfs部署lsync服务监控/data目录变化
--安装lsync服务
[root@nfs ~]# yum install -y lsyncd
--配置lsync服务(注意监控目录、推送模块名、同步时间)
[root@nfs ~]# vim /etc/lsyncd.conf
settings {
logfile = "/var/log/lsyncd/lsyncd.log",
statusFile = "/var/log/lsyncd/lsyncd.status",
maxProcesses = 2,
nodaemon = false,
}
sync {
default.rsync,
source = "/data",
target = "rsync_backup@172.16.1.41::nfs",
delete = true,
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsyncd.pwd",
archive = true,
compress = true,
}
}
--创建必要数据文件(密码文件名与lsync配置文件中的一致,其内容为rsync服务匿名用户的密码)
[root@nfs ~]# echo 123 > /etc/rsyncd.pwd
[root@nfs ~]# chmod 600 /etc/rsyncd.pwd
[root@nfs ~]# ll /etc/rsyncd.pwd
-rw------- 1 root root 4 Dec 4 16:54 /etc/rsyncd.pwd
--启动服务
[root@nfs ~]# systemctl start lsyncd
[root@nfs ~]# systemctl enable lsyncd
6.测试服务
--在web:/imag写入内容测试
[root@web01 ~]# cd /imag/
[root@web01 imag]# touch test{1..5}.txt
--查看nfs:/data目录是否有web创建的文件
[root@nfs ~]# cd /data
[root@nfs data]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
--查看backup:/nfs目录是否同步了nfs:/data中的内容
[root@backup ~]# cd /nfs
[root@backup nfs]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
--------------------至此服务部署成功----------------------
解决单点故障的问题
手动部署
- 假设nfs服务器突然宕机无法恢复
[root@nfs data]# ifdown ens36
- 需在backup服务器上部署nfs服务共享/nfs目录
[root@backup ~]# yum install -y nfs-utils
[root@backup ~]# vim /etc/exports
/nfs 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
[root@backup ~]# id rsync
uid=666(rsync) gid=666(rsync) groups=666(rsync)
[root@backup ~]# ll -d /nfs/
drwxr-xr-x 2 rsync rsync 91 Dec 4 16:56 /nfs/
[root@backup ~]# systemctl start nfs
[root@backup ~]# systemctl enable nfs
- web端切换挂载共享存储
--查询共享存储挂载的目录(使用df -h 会夯住,没法看)
[root@web01 ~]# tail -5 /proc/mounts
tmpfs /tmp tmpfs rw,nosuid,nodev 0 0
/dev/sda1 /boot xfs rw,relatime,attr2,inode64,noquota 0 0
sunrpc /var/lib/nfs/rpc_pipefs rpc_pipefs rw,relatime 0 0
tmpfs /run/user/0 tmpfs rw,nosuid,nodev,relatime,size=97088k,mode=700 0 0
172.16.1.31:/data /imag nfs4 rw,relatime,vers=4.2,rsize=131072,wsize=131072,namlen=255,hard,proto=tcp,timeo=600,retrans=2,sec=sys,clientaddr=172.16.1.7,local_lock=none,addr=172.16.1.31 0 0
--先取消挂载
[root@web01 ~]# umount -f /imag
[root@web01 ~]# df -h
--查看backup共享的目录
[root@web01 ~]# showmount -e 172.16.1.41
Export list for 172.16.1.41:
/nfs 172.16.1.0/24
--挂载并查看内容
[root@web01 ~]# mount -t nfs 172.16.1.41:/nfs /imag
[root@web01 ~]# cd /imag/
[root@web01 imag]# ls
test1.txt test2.txt test3.txt test4.txt test5.txt
使用脚本定时任务自动执行
- 在web01端写入脚本
[root@web01 ~]# vim /neterror.sh
#!/bin/bash
ping -c1 -W1 172.16.1.31 &>/dev/null #或者使用
#showmount -e 172.16.1.31 &>/dev/null
if [ $? -ne 0 ];then
umount -lf /img &>/dev/null &
sleep 2
umount -lf /img &>/dev/null
mount -t nfs 172.16.1.41:/nfs /img
fi
- 设置定时任务
[root@web01 ~]# vim /etc/crontab
01 * * * * root /neterror.sh
lsync配置文件详解
settings {
logfile = "/var/log/lsyncd/lsyncd.log", --日志文件所在的位置
statusFile =
"/var/log/lsyncd/lsyncd.status", --状态文件日志所在的位置
maxProcesses = 2, --最大的进程数量,内核的数量保持一致
nodaemon = false, --以守护进程方式来运行
}
sync {
default.rsync, --默认用rsync命令
source = "/data/", --实时监控/data目录 只要发生变化立刻推送到backup
target = "rsync_backup@10.0.0.41::nfs", --通过rsync守护进程命令来推送数据
delete = true, --使用--delete参数来实时同步
delay = 1, --1秒钟同步
rsync = {
binary = "/usr/bin/rsync", --命令所在的位置
password_file = "/etc/rsyncd.pwd", --密码的配置文件
archive = true, -- -a参数
compress = true, -- -z参数
}
}
lrsync配置多个监控目录
- 只需在/etc/lsyncd.conf配置文件中再添加一个模块即可
sync {
default.rsync,
source = "/code/",
target = "rsync_backup@10.0.0.41::code",
delete = true,
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsyncd.pwd",
archive = true,
compress = true,
}
}
- 配置完成后重启生效
systemctl restart lsyncd
本文来自博客园,作者:丁志岩,转载请注明原文链接:https://www.cnblogs.com/dezyan/p/18783236

浙公网安备 33010602011771号