From d91aa666cccf379e0aefe14535d4be4f6aada1c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=80=97=E5=AD=90?= Date: Thu, 10 Aug 2023 01:56:01 +0800 Subject: [PATCH] feat: postgresql install script --- scripts/postgresql/install.sh | 156 ++++++++++++++++++++++++++++++-- scripts/postgresql/uninstall.sh | 37 ++++++++ 2 files changed, 187 insertions(+), 6 deletions(-) diff --git a/scripts/postgresql/install.sh b/scripts/postgresql/install.sh index d4b92a0c..c5a0e22f 100644 --- a/scripts/postgresql/install.sh +++ b/scripts/postgresql/install.sh @@ -18,10 +18,154 @@ limitations under the License. ' HR="+----------------------------------------------------" -postgresqlVersion="$1" -setup_Path="/www" -postgresqlPath="${setup_Path}/server/postgresql" -os_Version=$(cat /etc/redhat-release | sed -r 's/.* ([0-9]+)\.?.*/\1/') -ipLocation=$(curl -s https://ip.ping0.cc/geo) +ARCH=$(uname -m) +memTotal=$(LC_ALL=C free -m | grep Mem | awk '{print $2}') +OS=$(source /etc/os-release && { [[ "$ID" == "debian" ]] && echo "debian"; } || { [[ "$ID" == "centos" ]] || [[ "$ID" == "rhel" ]] || [[ "$ID" == "rocky" ]] || [[ "$ID" == "almalinux" ]] && echo "centos"; } || echo "unknown") +downloadUrl="https://dl.cdn.haozi.net/panel/postgresql" +setupPath="/www" +postgresqlPath="${setupPath}/server/postgresql" +postgresqlVersion="" +postgresqlPassword=$(cat /dev/urandom | head -n 16 | md5sum | head -c 16) +cpuCore=$(cat /proc/cpuinfo | grep "processor" | wc -l) -# TODO +if [[ "${1}" == "15" ]]; then + postgresqlVersion="15.3" +else + echo -e $HR + echo "错误:不支持的 PostgreSQL 版本!" + exit 1 +fi + +# 安装依赖 +if [ "${OS}" == "centos" ]; then + dnf makecache -y + dnf groupinstall "Development Tools" -y + dnf install make gettext zlib-devel readline-devel libicu-devel libxml2-devel libxslt-devel openssl-devel systemd-devel -y +elif [ "${OS}" == "debian" ]; then + apt update + apt install build-essential make gettext zlib1g-dev libreadline-dev libicu-dev libxml2-dev libxslt-dev libssl-dev libsystemd-dev -y +else + echo -e $HR + echo "错误:耗子Linux面板不支持该系统" + exit 1 +fi + +postgresqlUserCheck=$(cat /etc/passwd | grep postgres) +if [ "${postgresqlUserCheck}" == "" ]; then + groupadd postgres + useradd -g postgres postgres +fi + +# 准备目录 +rm -rf ${postgresqlPath} +mkdir -p ${postgresqlPath} +cd ${postgresqlPath} + +# 下载源码 +wget -T 120 -O ${postgresqlPath}/postgresql-${postgresqlVersion}.tar.gz ${downloadUrl}/postgresql-${postgresqlVersion}.tar.gz +tar -zxvf postgresql-${postgresqlVersion}.tar.gz +rm -f postgresql-${postgresqlVersion}.tar.gz +mv postgresql-${postgresqlVersion} src + +# 编译 +cd src +./configure --prefix=${postgresqlPath} --enable-nls='zh_CN en' --with-icu --with-ssl=openssl --with-systemd --with-libxml --with-libxslt +if [ "$?" != "0" ]; then + echo -e $HR + echo "错误:PostgreSQL 编译初始化失败,请截图错误信息寻求帮助。" + rm -rf ${postgresqlPath} + exit 1 +fi +make -j${cpuCore} +if [ "$?" != "0" ]; then + echo -e $HR + echo "错误:PostgreSQL 编译失败,请截图错误信息寻求帮助。" + rm -rf ${postgresqlPath} + exit 1 +fi +make install +if [ "$?" != "0" ]; then + echo -e $HR + echo "错误:PostgreSQL 安装失败,请截图错误信息寻求帮助。" + rm -rf ${postgresqlPath} + exit 1 +fi + +rm -rf ${postgresqlPath}/src + +# 配置 +mkdir -p ${postgresqlPath}/data +mkdir -p ${postgresqlPath}/logs +chown -R postgres:postgres ${postgresqlPath} +chmod -R 700 ${postgresqlPath} + +echo "export PATH=${postgresqlPath}/bin:\$PATH" >> /etc/profile +source /etc/profile + +mkdir -p /home/postgres +cd /home/postgres +if [ -f /home/postgres/.bash_profile ]; then + echo "export PGHOME=${postgresqlPath}" >> /home/postgres/.bash_profile + echo "export PGDATA=${postgresqlPath}/data" >> /home/postgres/.bash_profile + echo "export PATH=${postgresqlPath}/bin:\$PATH " >> /home/postgres/.bash_profile + echo "MANPATH=$PGHOME/share/man:$MANPATH" >> /home/postgres/.bash_profile + echo "LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH" >> /home/postgres/.bash_profile + source /home/postgres/.bash_profile +fi +if [ -f /home/postgres/.profile ]; then + echo "export PGHOME=${postgresqlPath}" >> /home/postgres/.profile + echo "export PGDATA=${postgresqlPath}/data" >> /home/postgres/.profile + echo "export PATH=${postgresqlPath}/bin:\$PATH " >> /home/postgres/.profile + echo "MANPATH=$PGHOME/share/man:$MANPATH" >> /home/postgres/.profile + echo "LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH" >> /home/postgres/.profile + source /home/postgres/.profile +fi + +# 初始化 +su - postgres -c "${postgresqlPath}/bin/initdb -D ${postgresqlPath}/data" +if [ "$?" != "0" ]; then + echo -e $HR + echo "错误:PostgreSQL 初始化失败,请截图错误信息寻求帮助。" + rm -rf ${postgresqlPath} + exit 1 +fi + +# 配置慢查询日志 +cat >> ${postgresqlPath}/data/postgresql.conf << EOF +logging_collector = on +log_destination = 'stderr' +log_directory = '${postgresqlPath}/logs' +log_filename = 'postgresql-%Y-%m-%d.log' +log_statement = all +log_min_duration_statement = 5000 +EOF + +# 写入服务 +cat > /etc/systemd/system/postgresql.service << EOF +[Unit] +Description=PostgreSQL database server +Documentation=man:postgres(1) +After=network-online.target +Wants=network-online.target + +[Service] +Type=notify +User=postgres +ExecStart=${postgresqlPath}/bin/postgres -D ${postgresqlPath}/data +ExecReload=/bin/kill -HUP $MAINPID +KillMode=mixed +KillSignal=SIGINT +TimeoutSec=infinity + +[Install] +WantedBy=multi-user.target +EOF + +# 启动服务 +systemctl daemon-reload +systemctl enable postgresql +systemctl start postgresql + +panel writePlugin postgresql${1} ${postgresqlVersion} + +echo -e "${HR}\nPostgreSQL-${1} 安装完成\n${HR}" diff --git a/scripts/postgresql/uninstall.sh b/scripts/postgresql/uninstall.sh index e69de29b..402ace5b 100644 --- a/scripts/postgresql/uninstall.sh +++ b/scripts/postgresql/uninstall.sh @@ -0,0 +1,37 @@ +#!/bin/bash +export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:$PATH + +: ' +Copyright 2022 HaoZi Technology Co., Ltd. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +' + +HR="+----------------------------------------------------" + +systemctl stop postgresql +systemctl disable postgresql +rm -rf /etc/systemd/system/postgresql.service +systemctl daemon-reload +pkill -9 postgresql +rm -rf /www/server/postgresql + +userdel -r postgres +groupdel postgres + +sed -i '/export PATH=\/www\/server\/postgresql/d' /etc/profile +source /etc/profile + +panel deletePlugin postgresql${1} + +echo -e "${HR}\nPostgreSQL-${1} 卸载完成\n${HR}"