单机一键部署ssh+rsync+nfs+lsync
执行准备
主机信息
主机角色 |
外网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 |
秘钥信息
[root@web01 ~]
[root@web01 ~]
[root@web01 ~]
脚本文件rnl.sh
创建文件
[root@web01 ~]
编写文件
ssh 172.16.1.41 '
yum install -y rsync &> /dev/null
cat > /etc/rsyncd.conf <<EOF
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
[code]
path = /code
[img]
path = /img
EOF
groupadd -g 666 rsync
useradd -g 666 -u 666 -M -s /sbin/nologin rsync
echo rsync_backup:123 > /etc/rsync.passwd
chmod 600 /etc/rsync.passwd
mkdir /code /img
chown rsync.rsync /code /img
systemctl start rsyncd &> /dev/null
systemctl enable rsyncd &> /dev/null
exit
' &> /dev/null
echo '已成功为backup(172.16.1.41)部署rsync服务,共两个模块:[code]和[img]'
echo '--------------------------------------------------------------'
ssh 172.16.1.31 '
#使用web01为nfs服务器部署nfs服务
yum install -y nfs-utils &> /dev/null
cat > /etc/exports<<EOF
/data 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
EOF
groupadd -g 666 rsync
useradd -g 666 -u 666 -M -s /sbin/nologin rsync
mkdir /data
chown rsync.rsync /data/
systemctl start nfs &> /dev/null
systemctl enable nfs &> /dev/null
exit
' &> /dev/null
echo '已成功为NFS(172.16.1.31)部署nfs服务,共享目录为/data'
echo '--------------------------------------------------------------'
ssh 172.16.1.31 '
#使用web01为nfs服务器部署lsync服务
yum install -y lsyncd &> /dev/null
cat > /etc/lsyncd.conf<<EOF
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::img",
delete = true,
delay = 1,
rsync = {
binary = "/usr/bin/rsync",
password_file = "/etc/rsyncd.pwd",
archive = true,
compress = true,
}
}
EOF
echo 123 > /etc/rsyncd.pwd
chmod 600 /etc/rsyncd.pwd
systemctl start lsyncd &> /dev/null
systemctl enable lsyncd &> /dev/null
exit
' &> /dev/null
echo '已成功为NFS(172.16.1.31)部署lsync服务,监控目录为本机的/data'
echo '若/data发生变化将会发送给172.16.1.41的/img目录'
echo '--------------------------------------------------------------'
mkdir /imag
mount -t nfs 172.16.1.31:/data /imag
echo '已成功挂载172.16.1.31:/data目录于本机的/imag目录'
echo '--------------------------------------------------------------'
echo '****rsync+nfs+lsync 服务部署完成,进行测试吧!****'
执行文件
[root@web01 ~]
已成功为172.16.1.41部署rsync服务,共两个模块:code和img
已成功为172.16.1.31部署nfs服务,共享目录为/data
已成功为172.16.1.31部署lsync服务,监控目录为/data,发生变化将会发送给172.16.1.41的/img目录
已成功挂载172.16.1.31:/data目录于本机的/imag目录
rsync+nfs+lsync 服务部署完成,进行测试吧!
测试服务
[root@web01 ~]
[root@web01 ~]
-rw-r--r-- 1 666 666 0 Dec 5 20:30 /imag/shell.txt
[root@nfs ~]
-rw-r--r-- 1 rsync rsync 0 Dec 5 20:30 shell.txt
[root@backup ~]
-rw-r--r-- 1 rsync rsync 0 Dec 5 20:30 shell.txt
解决单点故障脚本nfs.sh
使用web01为backup服务器部署nfs服务并监控故障
脚本文件
vim nfs.sh
编写脚本
ssh 172.16.1.41 '
yum install -y nfs-utils &> /dev/null
cat > /etc/exports << EOF
/img 172.16.1.0/24(rw,sync,all_squash,anonuid=666,anongid=666)
EOF
systemctl start nfs
systemctl enable nfs
exit
'
cat >> /neterror.sh << EOF
#!/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 /imag &>/dev/null &
sleep 2
umount -lf /imag &>/dev/null
mount -t nfs 172.16.1.41:/img /imag
fi
EOF
cat >> /etc/crontab <<EOF
01 * * * * root sh /neterror.sh
EOF