Skip to content

Instantly share code, notes, and snippets.

@tonejito
Created August 24, 2017 17:38
Show Gist options
  • Save tonejito/3ea354665dcac83eba3d20fb4c5f1dcd to your computer and use it in GitHub Desktop.
Save tonejito/3ea354665dcac83eba3d20fb4c5f1dcd to your computer and use it in GitHub Desktop.
Clean those annoying and outdated journald logs
#!/bin/bash
# = ^ . ^ =
# 0700 root:root clean-journald-logs
# Delete the journald logs from /var/log/journal from the last years
#
# Its best to run this via ionice(1)
# ionice -c 3 clean-journald-logs
#
# Andres Hernandez - tonejito
#
# This script is released under the BSD license
# Current date
DATE=$(date '+%Y%m%d-%H%M')
LOG=./clean-$DATE.log
LOG0=./clean-$DATE.0
# Days since new year
DAYS=$(date '+%j')
let "DAYS -= 1"
# Truncate (our) log files before using
for FILE in $LOG $LOG0
do
truncate --size 0 $FILE
done
# Common flags
PRINTF_FORMAT='%T+\t%s\t%#m\t%u:%g\t%p\n'
XARGS_OPTS="-0 -r "
XARGS_TARGET="rm"
# Bail out if we run this on january 1st xD
test -n "$DAYS" -a $DAYS -gt 0 || exit -1
# Magic
find /var/log/journal/????????????????????????????????/ \
-type f -mtime +$DAYS -daystart \
-fprint0 $LOG0 \
-printf $PRINTF_FORMAT | sort -k 1,1 >> $LOG
xargs $XARGS_OPTS $XARGS_TARGET < $LOG0
test -e $LOG0 && rm $LOG0
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment