Files
MathApp/ops/applogs/archive-app-log.sh

34 lines
1.6 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
# روی ۲۴۹، هر شب (zareian-applog-archive.timer). app.log را به logs/archive/ می‌برد و فشرده می‌کند.
# برنامه با LOG_FILE_ROTATION=external از WatchedFileHandler استفاده می‌کند و پس از جابه‌جایی خودش app.log تازه
# می‌سازد؛ خطی که در همان لحظه نوشته شود در فایلِ جابه‌جاشده می‌ماند، گم نمی‌شود.
# این‌جا فقط ۳۰ روز می‌ماند (دیسکِ ۲۴۹ پر است) و فقط فایلی پاک می‌شود که ۲۴۷ گرفتنش را تأیید کرده
# (`.shipped`)؛ نسخه‌ی همیشگی روی ۲۴۷ است (pull-app-logs.sh). فقط زیرِ ~/mathapp-v2/logs می‌نویسد.
set -euo pipefail
LOGS=/home/administrator/mathapp-v2/logs
ARCH="$LOGS/archive"
mkdir -p "$ARCH"
chmod 750 "$ARCH"
stamp=$(date -u +%Y%m%dT%H%M%SZ)
# app.log.1..5 از RotatingFileHandlerِ پیشین (پیش از LOG_FILE_ROTATION=external) هم یک بار بایگانی می‌شوند.
for f in "$LOGS"/app.log.[0-9]*; do
[ -e "$f" ] || continue
n=${f##*.}
mv "$f" "$ARCH/app-$stamp-old$n.log"
done
if [ -s "$LOGS/app.log" ]; then
mv "$LOGS/app.log" "$ARCH/app-$stamp.log"
sleep 5 # نوشتنِ در جریانِ workerها روی فایلِ قدیمی تمام شود
fi
for f in "$ARCH"/app-*.log; do
[ -e "$f" ] || continue
gzip -9 "$f"
chmod 640 "$f.gz"
done
find "$ARCH" -name 'app-*.log.gz' -mtime +30 | while read -r gz; do
[ -e "$gz.shipped" ] && rm -f -- "$gz" "$gz.shipped"
done
echo "archived $(ls "$ARCH"/app-*.log.gz 2>/dev/null | wc -l) files in $ARCH"