docker-postgres/init
2025-11-04 16:09:35 +08:00

171 lines
5.2 KiB
Bash
Executable File
Raw Permalink 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初始化脚本
# 用于初始化或重新初始化PostgreSQL配置环境
# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 切换工作目录到脚本所在目录
cd $SCRIPT_DIR
# 检查必要的命令
check_commands() {
if ! command -v openssl &> /dev/null; then
echo "错误: 未找到openssl命令请先安装"
exit 1
fi
if ! command -v direnv &> /dev/null; then
echo "警告: 未找到direnv命令建议安装以获得最佳体验"
fi
}
# 创建必要的目录结构
create_directories() {
echo "创建必要的目录结构..."
mkdir -p ./data/pgdata
mkdir -p ./data/archived
mkdir -p ./data/backup/first
mkdir -p ./conf
echo "目录结构创建完成"
}
# 初始化配置文件
initialize_files() {
# 提示用户输入数据库密码步骤1
read -s -p "请输入PostgreSQL数据库密码: " postgres_password
echo
read -s -p "请再次输入密码确认: " postgres_password_confirm
echo
# 验证密码一致性
if [ "$postgres_password" != "$postgres_password_confirm" ]; then
echo "错误: 两次输入的密码不一致"
return 1
fi
# 验证密码强度(可选)
if [ ${#postgres_password} -lt 8 ]; then
echo "警告: 密码长度少于8个字符建议使用强密码"
read -p "是否继续使用此密码?(y/n): " continue
if [ "$continue" != "y" ]; then
return 1
fi
fi
# 创建加密文件步骤2
echo "创建加密的密码文件..."
echo "请为加密文件设置一个密码(主密钥):"
echo -n "$postgres_password" | openssl enc -aes-256-cbc -salt -pbkdf2 -iter 10000 -out postgres_password.enc
# 提示用户输入映射端口号步骤3
default_port="5432"
read -p "请输入PostgreSQL映射端口号 [$default_port]: " postgres_port
# 如果用户直接回车,使用默认值
if [ -z "$postgres_port" ]; then
postgres_port="$default_port"
fi
echo "映射端口号设置为: $postgres_port"
# 提示用户输入容器名称步骤4
default_container_name="postgres"
read -p "请输入PostgreSQL容器名称 [$default_container_name]: " postgres_container_name
# 如果用户直接回车,使用默认值
if [ -z "$postgres_container_name" ]; then
postgres_container_name="$default_container_name"
fi
echo "容器名称设置为: $postgres_container_name"
# 检查加密是否成功
if [ $? -ne 0 ]; then
echo "错误: 创建加密文件失败"
return 1
fi
# 设置加密文件权限
chmod 600 postgres_password.enc
echo "加密文件创建成功权限设置为600"
# 创建.envrc文件
echo "创建.envrc配置文件..."
cat > .envrc << EOF
# PostgreSQL配置环境变量
export POSTGRES_PASSWORD=\$(openssl enc -aes-256-cbc -d -pbkdf2 -iter 10000 -in postgres_password.enc)
export POSTGRES_PORT=$postgres_port
export POSTGRES_CONTAINER_NAME=$postgres_container_name
EOF
# 设置.envrc文件权限
chmod 600 .envrc
echo ".envrc文件创建成功权限设置为600"
# 自动执行direnv allow并提供状态反馈
if command -v direnv &> /dev/null; then
echo ""
echo "📝 初始化完成!自动配置环境变量..."
echo "正在执行 direnv allow..."
if direnv allow > /dev/null 2>&1; then
echo "✅ direnv allow 执行成功!环境变量已启用"
else
echo "❌ direnv allow 执行失败,请手动运行 'direnv allow' 来启用环境变量"
fi
else
echo ""
echo "初始化完成建议安装direnv以获得更好的使用体验"
echo " macOS: brew install direnv"
echo " Linux: apt-get install direnv 或 yum install direnv"
fi
return 0
}
# 主函数
main() {
echo "PostgreSQL环境初始化脚本"
echo "==================================="
# 检查必要的命令
check_commands
# 检查文件是否存在
if [ -f "postgres_password.enc" ] && [ -f ".envrc" ]; then
echo ""
echo "检测到postgres_password.enc和.envrc文件已存在"
read -p "是否重新初始化?这将覆盖现有配置!(y/n): " reinitialize
if [ "$reinitialize" != "y" ]; then
echo "初始化取消"
exit 0
fi
# 备份现有文件(可选)
backup_suffix="_bak_$(date +%Y%m%d%H%M%S)"
echo "备份现有文件..."
cp postgres_password.enc "postgres_password.enc$backup_suffix" 2>/dev/null
cp .envrc ".envrc$backup_suffix" 2>/dev/null
echo "备份完成"
fi
# 创建目录结构
create_directories
# 初始化配置文件
while ! initialize_files; do
echo "请重新输入密码..."
done
echo ""
echo "==================================="
echo "初始化成功!"
echo "使用说明:"
echo "1. 使用 './service start' 启动服务"
echo "2. 使用 './service stop' 停止服务"
echo "3. 使用 './service status' 查看服务状态"
echo "4. 使用 './service restart' 重启服务"
}
# 执行主函数
main