liunx自动部署shell脚本
场景:
前几天要去甲方那边部署系统,感觉要装的东西很多,.net 环境哇,mysql,pg 等等。每次装都要重新去看怎么安装,而且还容易遇到一些问题,于是这次我打算写成 shell 脚本,方便运行
解决:
这里主要放一下我的脚本的内容,完成了部分功能。其他功能等之后有空了在慢慢补充。
首先,看看这个文件夹的情况:
这里由于我在 windows 下进行编写的,为了编译方便我把 sh 后缀先改成 txt。这个 auto_cmd 就是主要程序,如下:
如果在 liunx 上运行遇到格式的错误,可以先进入 vi,然后:set fileformat=unix,然后 qw 保存设置。这样运行就没有格式问题了。
运行的话,就./auto_cmd.sh,这样就可以了。
#!/bin/sh
变量
RED='\033[31m'
GREEN='\033[32m'
YELLOW="\033[33m"
RES='\033[0m'
文件目录
HomePath="root"
计数器
STR_TEMP="success"
OLD_STR="success"
输入变量
input="q"
cmd="1"
supervisor 安装
ini 文件要存放的位置
LOC="/etc/supervisord.d/"
sh 文件要存放的位置
TARGET="/root/"
pg 安装
postgresql 用户的密码
PG_PASSWORD="YTkejiA2216_PG!"
pg 开放的端口
PG_PORT=8000
通过 yum 安装
installYum(){
echo "=====begin install ${1}====="
yum -y install ${1}
STR_TEMP=$(yum list installed | grep ${1})
字符变,则说明已经安装
if [ "$STR_TEMP" = "$OLD_STR" ]; then
echo -e "${RED}=====${1} install failed=====${RES}"
elif [ "$STR_TEMP" = "" ]; then
echo -e "${RED}=====${1} install failed=====${RES}"
else
echo -e "${GREEN}=====${1} install success=====${RES}"
fi
STR_TEMP=OLD_STR
}
遍历所有文件,修改权限
changefile(){
for file in ls $<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
do
dir_or_file=$1"/"$file
if [ -f ${dir_or_file} ]; then
chmod 777 ${dir_or_file}
elif [ -d ${dir_or_file} ]; then
changefile $dir_or_file
else
echo "${dir_or_file}"
fi
done
}
将指定后缀移动到指定位置
参数 1:源位置
参数 2:后缀名
参数 3:目标位置
getfile(){
for file in ls $<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
do
dir_or_file=$1"/"$file
if [ "${file##*.}"x = "$2"x ]; then
echo "====${dir_or_file}====$3===="
cp ${dir_or_file} $3
fi
done
}
换源
change(){
echo "change"
echo -e "${YELLOW}===== 暂未实现 =====${RES}"
}
安装 superviosr
installsuperviosr(){
ARRAY_NAME=("epel-release" "supervisor")
# 安装
for(( i=0;i<${#ARRAY_NAME[@]};i++)) do
installYum ${ARRAY_NAME[i]};
done
# 配置信息
if [ ! -d "$LOC" ]; then
mkdir ${LOC} -p
echo -e "${GREEN}===== 配置文件夹已创建 =====${RES}"
else
echo -e "${GREEN}===== 配置文件夹已创建 =====${RES}"
fi
# 遍历 ini 文件,复制到配置文件夹下
TEMP=$HomePath"/1.supervisor"
TYPE="ini"
SHTYPE="sh"
getfile ${TEMP} ${TYPE} ${LOC}
# 复制 sh 到 root 下
getfile ${TEMP} ${SHTYPE} ${TARGET}
# 开机启动
systemctl enable supervisord
# 启动
supervisord -c /etc/supervisord.conf
systemctl start supervisord
}
安装.net
installdotnet(){
sudo rpm -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm
yum -y install dotnet-sdk-6.0
yum -y install aspnetcore-runtime-6.0
yum -y install dotnet-sdk-3.1
yum -y install aspnetcore-runtime-3.1
}
安装 mysql
installmysql(){
echo "install mysql"
echo -e "${YELLOW}===== 暂未实现 =====${RES}"
}
安装 nginx
installnginx(){
yum install -y nginx
mv /etc/nginx/nginx.conf old_redis.conf
\cp -f ../6.nginx/nginx.conf /etc/nginx
systemctl enable nginx
systemctl start nginx
}
安装 redis
installredis(){
yum -y install epel-release
yum update
yum -y install redis
mv /etc/redis.conf old_redis.conf
\cp -f ../4.redis/redis.conf /etc
systemctl enable redis
systemctl start redis
# TODO:需要检测是否安装,是否启动
}
安装 pg
installpg(){
echo "################################################"
echo "# Please enter your choise: #"
echo "# (0) 安装 postgresql 12 #"
echo "# (1) 安装 pg_corn(定时器插件) #"
echo "# (2) 安装 timescaledb(时序数据库) #"
echo "# (3) 安装 Pgpool(高可用方案) #"
echo "################################################"
read cmd
case $cmd in
"0")
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql12-server
# 初始化文件夹
if [ ! -d "/var/lib/pgsql/12/data" ]; then
/usr/pgsql-12/bin/postgresql-12-setup initdb
chmod 700 -R /var/lib/pgsql/12/data
else
echo -e "${GREEN}===== 已经初始化过了 =====${RES}"
fi
# 开机自启动
systemctl enable postgresql-12
# 启动服务
systemctl start postgresql-12
# 修改密码
sudo -u postgres psql -U postgres -d postgres -c "alter user postgres with password '${PG_PASSWORD}';exit;"
# 配置端口
firewall-cmd --add-port=${PG_PORT}/tcp --permanent
firewall-cmd --reload
# 移动配置文件
cp ${HomePath}/5.postgresql/pg_hba.conf /var/lib/pgsql/12/data/pg_hba.conf
cp ${HomePath}/5.postgresql/postgresql.conf /var/lib/pgsql/12/data/postgresql.conf
# 配置完成
systemctl restart postgresql-12
;;
"1")
echo -e "${YELLOW}===== 暂未实现 =====${RES}"
;;
"2")
echo -e "${YELLOW}===== 暂未实现 =====${RES}"
;;
"3")
echo -e "${YELLOW}===== 暂未实现 =====${RES}"
;;
esac
}
安装 grafana
installgrafana(){
echo "################################################"
echo "# Please enter your choise: #"
echo "# (0) 安装 node export(性能数据采集) #"
echo "# (1) 安装 prometeus(性能监控系统) #"
echo "# (2) 安装 grafana(数据可视化系统) #"
echo "# (3) 安装 loki(日志管理系统) #"
echo "# (4) 安装 protail(日志采集器) #"
echo "# (5) 安装 sendmail(grafana 发送邮件) #"
echo "################################################"
read cmd
case $cmd in
"0")
mkdir -p /usr/local/node_exporter
# 下载安装包
wget https://github.com/prometheus/node_exporter/releases/download/v1.1.1/node_exporter-1.1.1.linux-amd64.tar.gz
tar -zxvf node_exporter-1.1.1.linux-amd64.tar.gz
mv node_exporter-1.1.1.linux-amd64 /usr/local/node_exporter
# 创建服务
cp -f ../8.grafana/node_exporter.service /usr/lib/systemd/system
# 启动
systemctl daemon-reload
systemctl start node_exporter.service
systemctl enable node_exporter.service
# TODO:需要检测是否安装,是否启动
;;
"1")
mkdir -p /usr/local/prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.27.1/prometheus-2.27.1.linux-amd64.tar.gz
tar -xzvf prometheus-2.27.1.linux-amd64.tar.gz
mv prometheus-2.27.1.linux-amd64 /usr/local/prometheus
cp -f ../8.grafana/prometheus.sh /usr/local/prometheus
cp -f ../8.grafana/prometheus.service /usr/lib/systemd/system
systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
# TODO:需要检测是否安装,是否启动
;;
"2")
wget https://dl.grafana.com/enterprise/release/grafana-enterprise-8.0.0-1.x86_64.rpm
yum -y install grafana-enterprise-8.0.0-1.x86_64.rpm
systemctl daemon-reload
systemctl enable grafana-server
systemctl start grafana-server
# TODO:需要检测是否安装,是否启动
;;
"3")
mkdir -p /usr/local/loki
cp -f ../8.grafana/loki-linux-amd64 /usr/local/loki
cp -f ../8.grafana/loki-local-config.yaml /usr/local/loki
cp -f ../8.grafana/loki.sh /usr/local/loki
cp -f ../8.grafana/loki.service /usr/lib/systemd/system
# TODO:需要检测是否安装,是否启动
;;
"4")
mkdir -p /usr/local/promtail
cp -f ../8.grafana/promtail-linux-amd64 /usr/local/promtail
cp -f ../8.grafana/promtail-local-config.yaml /usr/local/promtail
cp -f ../8.grafana/promtail.sh /usr/local/promtail
cp -f ../8.grafana/promtail.service /usr/lib/systemd/system
# TODO:需要检测是否安装,是否启动
;;
"4")
yum install -y sendmail
systemctl enable sendmail
systemctl start sendmail
# TODO:需要手动修改/etc/grafana/grafana.ini
;;
esac
}
安装系统常用指令
installcmd(){
echo "################################################"
echo "# Please enter your choise: #"
echo "# (0) 安装 vim #"
echo "# (1) 安装 git #"
echo "# (2) 安装 lsof #"
echo "# (3) 安装 gcc #"
echo "# (4) 安装 make #"
echo "# (t) 手动输入安装 #"
echo "################################################"
read input
</span><span style="color: rgba(0, 0, 255, 1)">case</span> $input <span style="color: rgba(0, 0, 255, 1)">in</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
echo </span>-e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${YELLOW}=====暂未实现=====${RES}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
echo </span>-e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${YELLOW}=====暂未实现=====${RES}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installYum lsof
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
echo </span>-e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${YELLOW}=====暂未实现=====${RES}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
echo </span>-e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${YELLOW}=====暂未实现=====${RES}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">t</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
echo </span>-e <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">${YELLOW}=====暂未实现=====${RES}</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
;;
esac
}
菜单
menu(){
source ~/.bashrc
echo "################################################"
echo "# Please enter your choise: #"
echo "# (0) 换源 #"
echo "# (1) 安装系统常用指令 #"
echo "# (2) 安装 supervisor #"
echo "# (3) 安装.net 环境 #"
echo "# (4) 安装 mysql #"
echo "# (5) 安装 postgresql 和插件 #"
echo "# (6) 安装 nginx #"
echo "# (7) 安装 redis #"
echo "# (8) 安装 grafana #"
echo "# (q) Exit Menu #"
echo "################################################"
read input
</span><span style="color: rgba(0, 0, 255, 1)">case</span> $input <span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)">
# 换源
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
change
;;
# 安装常用指令
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installcmd
;;
# 安装supervior
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installsuperviosr
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installdotnet
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">4</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installmysql
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">5</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installpg
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">6</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installnginx
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">7</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installredis
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">8</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
installgrafana
;;
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">q</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
exit;;
esac
}
主程序
HomePath=$(pwd)
changefile ${HomePath}
while menu
do
menu
done
通过这次部署,对 shell 脚本有了一定的了解。但是本人能力有限,有问题欢迎互相讨论,然后完整的文件夹可以私信我。
想下次用 qt 实现一个跨平台的部署系统,带 GUI,可以可视化配置,正好最近想学 qt。
参考:
https://www.runoob.com/linux/linux-shell-array.html