docker-postgres/clear

39 lines
1.1 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
# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
# 切换工作目录到脚本所在目录
cd $SCRIPT_DIR
# 确认提示
read -p "将终止postgres服务并删除data目录下的所有的数据库文件、日志、备份、归档删除操作不可逆是否继续(YES/no): " confirm
if [ "$confirm" != "YES" ]; then
echo "退出脚本"
exit 1
fi
container_name="postgres"
# 判断名为postgres的容器是否存在
if [ "$(docker ps -q -f name=${container_name})" ]; then
# 如果容器存在则执行docker compose down
docker compose down
fi
echo "clear ./data/pgdata/ ..."
rm -rf ./data/pgdata/*
echo "clear ./data/archived/ ..."
rm -rf ./data/archived/*
echo "clear ./data/backup/first/ ..."
rm -rf ./data/backup/first/*
echo "clear ./data/wal_backup/ ..."
rm -rf ./data/wal_backup/*
# 获取data/backup目录下除了first目录以外的所有目录和文件
backup_files=$(ls -d ./data/backup/* | grep -v "first")
echo "clear ./data/backup/ ..."
# 删除data/backup目录下除了first目录以外的其他所有目录和文件
for file in $backup_files; do
rm -rf "$file"
done