docker-postgres/service
2025-11-12 14:03:29 +08:00

186 lines
4.8 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# PostgreSQL服务管理脚本
# 用法: ./service start|stop|status|restart
# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 切换工作目录到脚本所在目录
cd $SCRIPT_DIR
# 容器名称从环境变量读取默认为postgres
container_name=${POSTGRES_CONTAINER_NAME:-postgres}
# 检查并加载环境变量
load_env_variables() {
if command -v direnv &> /dev/null; then
echo "使用direnv加载环境变量..."
# 尝试使用direnv加载环境变量
eval "$(direnv export bash)"
# 检查POSTGRES_PASSWORD是否设置如果没有设置尝试执行direnv allow
if [ -z "$POSTGRES_PASSWORD" ] && [ -f ".envrc" ]; then
echo "检测到POSTGRES_PASSWORD未设置尝试执行direnv allow..."
direnv allow
# 再次加载环境变量
eval "$(direnv export bash)"
fi
else
echo "警告: direnv未安装使用当前环境变量"
fi
# 检查POSTGRES_PASSWORD环境变量是否设置
if [ -z "$POSTGRES_PASSWORD" ]; then
echo "错误: POSTGRES_PASSWORD环境变量未设置"
echo "请确保已安装direnv并运行 'direnv allow'"
echo "提示: 可能是加密密码输入错误,请重新输入正确的密码"
return 1
fi
return 0
}
# 启动服务
start_service() {
echo "启动PostgreSQL服务..."
# 加载环境变量
if ! load_env_variables; then
exit 1
fi
# 声明变量
conf_dir="./conf"
conf_files=($(ls $conf_dir 2>/dev/null || echo ""))
target_dir="./data/pgdata"
# 创建目录结构
mkdir -p ./data/pgdata
mkdir -p ./data/archived
mkdir -p ./data/backup/first
# 启动docker容器
docker compose up -d
# 等待容器启动并复制配置文件
if [ ${#conf_files[@]} -gt 0 ]; then
echo "等待容器初始化并复制配置文件..."
for file in "${conf_files[@]}"
do
while [ ! -f "$target_dir/$file" ]; do
echo "等待容器 '$container_name' 初始化..."
sleep 5
done
cp "$conf_dir/$file" "$target_dir/$file"
echo "已复制 $file$conf_dir$target_dir"
done
fi
echo "PostgreSQL服务启动完成!"
}
# 停止服务
stop_service() {
echo "停止PostgreSQL服务..."
# 必须成功加载环境变量(包括密码验证)才能继续
if ! load_env_variables; then
echo "错误: 密码验证失败,无法继续操作"
exit 1
fi
# 使用docker compose down停止服务
docker compose down
# 如果docker compose命令失败尝试直接通过容器名称停止
if [ $? -ne 0 ]; then
echo "尝试直接停止容器..."
docker stop $container_name > /dev/null 2>&1
docker rm $container_name > /dev/null 2>&1
fi
echo "PostgreSQL服务已停止."
}
# 检查服务状态
status_service() {
echo "检查PostgreSQL服务状态..."
# 必须成功加载环境变量(包括密码验证)才能继续
if ! load_env_variables; then
echo "错误: 密码验证失败,无法继续操作"
exit 1
fi
# 重新获取容器名称,确保使用最新的环境变量值
updated_container_name=${POSTGRES_CONTAINER_NAME:-postgres}
# 检查容器是否正在运行
if docker ps | grep -q "$updated_container_name"; then
echo "PostgreSQL服务正在运行."
echo "访问地址: localhost:${POSTGRES_PORT:-25001}"
echo "容器名称: $updated_container_name"
return 0
else
echo "PostgreSQL服务未运行."
return 1
fi
}
# 重启服务
restart_service() {
echo "重启PostgreSQL服务..."
# 先停止服务(会验证密码)
stop_service
# 停止成功后,重新验证密码并启动服务
if [ $? -eq 0 ]; then
echo "正在启动PostgreSQL服务..."
start_service
else
echo "错误: 服务停止失败,无法重启"
exit 1
fi
}
# 显示帮助信息
show_help() {
echo "用法: ./service [command]"
echo "命令:"
echo " start 启动PostgreSQL服务"
echo " stop 停止PostgreSQL服务"
echo " status 检查PostgreSQL服务状态"
echo " restart 重启PostgreSQL服务"
echo " help 显示此帮助信息"
}
# 检查命令参数
if [ $# -eq 0 ]; then
echo "错误: 请指定命令"
show_help
exit 1
fi
# 执行对应的命令
case "$1" in
start)
start_service
;;
stop)
stop_service
;;
status)
status_service
;;
restart)
restart_service
;;
help)
show_help
;;
*)
echo "错误: 未知命令 '$1'"
show_help
exit 1
;;
esac