build/packages/bsp/common/usr/lib/armbian/armbian-truncate-logs

42 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
#
# Copyright (c) Authors: https://www.armbian.com/authors
#
# This file is licensed under the terms of the GNU General Public
# License version 2. This program is licensed "as is" without any
# warranty of any kind, whether express or implied.
#
# truncate, save and clean logs if they get over 75% of the /var/log size
# working only when armbian-ramlog is enabled
treshold=75 # %
JOURNAL_SIZE=5M # size to shrink systemd-journal
[ -f /etc/default/armbian-ramlog ] && . /etc/default/armbian-ramlog
[ "$ENABLED" != true ] && exit 0
if [ -z "`grep ^Storage=volatile /etc/systemd/journald.conf`" -a -z "$(ls -A /run/log/journal)" ] ;then
journalctl --relinquish-var # make sure to do volatile logging until the next flush
fi
logusage=$(df /var/log/ --output=pcent | tail -1 |cut -d "%" -f 1)
if [ $logusage -ge $treshold ]; then
# remove journal backup files created by armbian-ramdisk in case of duplicate journals directories
rm -rf /var/log.hdd/journal-*
# write to SD
/usr/lib/armbian/armbian-ramlog write >/dev/null 2>&1
# rotate logs on "disk"
/usr/sbin/logrotate --force /etc/logrotate.conf
# truncate
/usr/bin/find /var/log -name '*.log' -or -name '*.xz' -or -name 'lastlog' -or -name 'messages' -or -name 'debug' -or -name 'syslog' | xargs -r truncate --size 0
/usr/bin/find /var/log -name 'btmp' -or -name 'wtmp' -or -name 'faillog' -or -name 'firewalld' | xargs -r truncate --size 0
/usr/bin/find /var/log -name 'mail.err' -or -name 'mail.info' -or -name 'mail.warning' | xargs -r truncate --size 0
# remove
/usr/bin/find /var/log -name '*.[0-9]' -or -name '*.gz' | xargs -r rm -f
# vacuum systemd-journald
[ -d /var/log/journal ] && journalctl --quiet --vacuum-size=${JOURNAL_SIZE}
# remove old archived journal files modified more than 1 day ago
[ -d /var/log.hdd/journal ] && find /var/log.hdd/journal -ctime 1 \( -name '*@*' -o -name '*~' \) -delete
fi