Dock-MultiCtrl/prj-deploy/file/script/data_cleanup.sh

37 lines
1.0 KiB
Bash
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
# 要检查的目录
TARGET_DIR="/data/dj_multictrl_data"
# 最低剩余容量(单位 GiB
THRESHOLD=50
# 当空间不足时,每次清理几个文件(按最旧)
DELETE_COUNT=5
# 当前剩余容量GiB
AVAILABLE=$(df -BG "$TARGET_DIR" | awk 'NR==2 {gsub("G","",$4); print $4}')
# 日志文件
LOG_FILE="/var/log/disk_cleanup.log"
echo "$(date '+%F %T') 当前剩余空间: ${AVAILABLE}G" >> "$LOG_FILE"
if [ "$AVAILABLE" -lt "$THRESHOLD" ]; then
echo "$(date '+%F %T') 空间低于 ${THRESHOLD}G开始清理..." >> "$LOG_FILE"
# 找出最旧的文件并删除
find "$TARGET_DIR" -type f -printf "%T@ %p\n" \
| sort -n \
| head -n "$DELETE_COUNT" \
| awk '{print $2}' \
| while read FILE; do
echo "$(date '+%F %T') 删除文件: $FILE" >> "$LOG_FILE"
rm -f "$FILE"
done
echo "$(date '+%F %T') 清理结束" >> "$LOG_FILE"
else
echo "$(date '+%F %T') 空间充足,无需清理" >> "$LOG_FILE"
fi