36 lines
1.2 KiB
Bash
36 lines
1.2 KiB
Bash
#!/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 清理完成"
|