docker-postgres/start

41 lines
965 B
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
# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 切换工作目录到脚本所在目录
cd $SCRIPT_DIR
# 声明变量
container_name="postgres"
conf_dir="./conf"
conf_files=($(ls $conf_dir))
target_dir="./data/pgdata"
# 创建目录结构
mkdir -p ./data/pgdata
mkdir -p ./data/archived
mkdir -p ./data/backup/first
# 启动docker容器
docker compose up -d
# 等待5秒确保容器已经启动
# sleep 5
# 遍历conf目录下的文件
for file in "${conf_files[@]}"
do
while [ ! -f "$target_dir/$file" ]; do
echo "Waiting for the container \"$container_name\" to be initialized..."
sleep 5
done
cp "$conf_dir/$file" "$target_dir/$file"
echo "Copied $file from $conf_dir to $target_dir"
done
docker exec -it "$container_name" chown postgres:postgres -R $target_dir
docker exec -it "$container_name" chmod 700 -R $target_dir
# 重启容器
docker restart "$container_name"