Files
4server/app/sbin/rotateLogs
T
2026-09-01 15:37:21 -03:00

44 lines
1.2 KiB
Bash
Executable File

#!/bin/bash
# rotateLogs — rotate odoo.log for all UUID containers, keep 4 weeks
# Runs as a daemon: waits for midnight, rotates, then waits for next midnight
RETENTION_DAYS=28
wait_for_midnight() {
ELAPSED=$(( $(date +%H) * 3600 + $(date +%M) * 60 + $(date +%S) ))
SLEEP=$(( 86400 - ELAPSED ))
echo "Waiting ${SLEEP}s until midnight..."
sleep "$SLEEP"
}
while true; do
wait_for_midnight
for log in /4server/data/00*/logs/odoo.log; do
[[ -f "$log" ]] || continue
DIR=$(dirname "$log")
UUID=$(basename "$(dirname "$DIR")")
STAMP=$(date '+%Y-%m-%d_%H-%M')
ARCHIVE="$DIR/odoo.log.$STAMP"
# Copy preserving permissions, then truncate the live log
PERMS=$(stat -c '%a' "$log")
OWNER=$(stat -c '%u:%g' "$log")
cp "$log" "$ARCHIVE"
: > "$log"
# Compress the archive, restore original ownership and permissions
gzip "$ARCHIVE"
chmod "$PERMS" "${ARCHIVE}.gz"
chown "$OWNER" "${ARCHIVE}.gz"
echo "$UUID rotated -> $(basename "${ARCHIVE}.gz")"
# Delete rotated logs older than 4 weeks
find "$DIR" -name 'odoo.log.*.gz' -mtime +$RETENTION_DAYS -exec rm -f {} \; \
-exec echo "$UUID deleted: {}" \;
done
done