This commit is contained in:
2026-05-26 15:20:00 +08:00
committed by milky0217
parent 8d77a240da
commit d03acf0add
7 changed files with 856 additions and 2 deletions

View File

@@ -0,0 +1,35 @@
#!/bin/bash
# ===========================================
# 清理过期的 refresh_token
# 用途: 删除 refresh_tokens 表中已过期的记录
# 建议: 通过 cron 每天执行一次
# Crontab 示例:
# 0 3 * * * /root/rust/rust_backend/scripts/cleanup_refresh_tokens.sh >> /root/rust/rust_backend/logs/cleanup.log 2>&1
# ===========================================
set -euo pipefail
DB_CONTAINER="1Panel-postgresql-FtMo"
DB_NAME="${1:-milkydata}"
DB_USER="milkydata"
LOG_PREFIX="[$(date '+%Y-%m-%d %H:%M:%S')]"
echo "$LOG_PREFIX 开始清理过期的 refresh_token..."
# 检查表是否存在
table_exists=$(docker exec "$DB_CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" -tAc \
"SELECT 1 FROM information_schema.tables WHERE table_name = 'refresh_tokens'" 2>/dev/null || echo "0")
if [ "$table_exists" != "1" ]; then
echo "$LOG_PREFIX 表 refresh_tokens 不存在,跳过清理"
exit 0
fi
# 删除过期记录
deleted=$(docker exec "$DB_CONTAINER" psql -U "$DB_USER" -d "$DB_NAME" -tAc \
"DELETE FROM refresh_tokens WHERE expires_at <= NOW(); SELECT COUNT(*);" 2>/dev/null || echo "0")
echo "$LOG_PREFIX 已删除 ${deleted} 条过期的 refresh_token"
echo "$LOG_PREFIX 清理完成"