RHEL7(Centos7)下使用shell脚本一键部署服务

今天配置服务觉得有些麻烦,想着写一个能一键配置nfs服务端的shell脚本,当然在安装之前需要配置一个yum源,所以我一并将这些功能写在了一个shell脚本里。

脚本如下:

#author:Roya
#script name:RyumAnfs
#creation time:2020-01-23
#action:One click deployment of Yum source and NFS server
#!/bin/bash
echo "yum means Configure Yum source and nfs means Configure NFS server." #介绍yum和nfs的意思
read -p "please input yum or nfs: " a  #请输入yum或者nfs
case $a in #使用case语句判断输入
yum)
echo "Prepare to configure Yum source...."
sleep 1  #等一秒执行下面命令
echo "One moment please"
sleep 5
echo "/dev/cdrom /mnt iso9660 defaults 0 0" >> /etc/fstab && mount -a &> /dev/null && echo 'mount success!' # 将挂载信息写入fstab
echo '[rhel]
name=rhel
baseurl=file:///mnt
gpgcheck=0
enabled=1' >> /etc/yum.repos.d/rhel.repo #yum源的配置文件
yum makecache &> /dev/null
if [ $? -eq 0 ]
then 
echo "Yum source configuration successfully!"
else echo "Yum source configuration failed!"
fi
yum repolist | grep repolist
;;
nfs)
yum install -y nfs-utils &> /dev/null #安装nfs服务端
if [ $? -eq 0 ]
then echo 'NFS service installed successfully!'
read -p 'Please enter NFS directory path you want to share: (Tips:/nfsdir)' nfsdir #输入nfs共享的目录
sleep 1 
read -p 'Please enter the IP address of the host allowed to be shared: (Tips:192.168.1.1 or 192.168.1.*)' nfsIP #输入允许共享的主机IP地址或者网段
sleep 1
read -p 'Please enter the permission of the shared host: (Tips:sync,rw,ro)' nfspwr #输入允许共享的主机的权限
sleep 1
echo "Configuring..... Please wait."
sleep 10
if [ -e $nfsdir ] #判断nfs共享目录是否存在
then echo 'File Exists'
else mkdir $nfsdir
chmod -Rf 777 $nfsdir
fi
echo "$nfsdir $nfsIP($nfspwr)" > /etc/exports 
systemctl restart nfs-server 
if [ $? -eq 0 ] 
then 
IP=`ifconfig | awk -F ' ' 'NR==2{print$2}'` #使用awk命令提取出IP地址
exportfs -r 
showmount -e $IP
if [ $? -eq 0 ] #判断服务是否配置正确
then echo "NFS server has been configured successfully!"
else echo "NFS server has been configured failed!"
fi
fi
systemctl restart rpcbind 
systemctl enable rpcbind &> /dev/null
systemctl enable nfs-server &> /dev/null #加入到开机自启动
firewall-cmd --add-service=nfs --permanent &> /dev/null
firewall-cmd --add-service=rpc-bind --permanent &> /dev/null
firewall-cmd --reload &> /dev/null
else echo 'NFS service installion failed!'
fi
;;
*) #输入yum或者nfs以外的任何字符都退出脚本
exit 0
;;
esac

需要注意的是本地yum源已经配置的情况下使用此脚本会报错,这些判断功能我会在之后进行完善。目前测试没有什么问题,测试结果如下

chmod +x RyumAnfs.sh \\需要加一个执行权限
一键配置本地yum源
在这里插入图片描述
配置nfs服务端
在这里插入图片描述
在客户端只需挂载即可

echo "IP地址:共享目录 挂载目录 nfs defaults 0 0" >> /etc/fstab
mount -a

本文到此也就结束了,希望能对大家有所帮助,由于水平有限,在很多地方肯定有些不足之处,如有纰漏或者错误,还请斧正,定当改进。文章写的很不容易,大家的评论和点赞就是我的最大动力,谢谢支持!

发布了11 篇原创文章 · 获赞 46 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/gd_9988/article/details/104075800