Created
January 7, 2015 11:16
-
-
Save tomgidden/4f13fd13f6917fc20c14 to your computer and use it in GitHub Desktop.
zsh-based log compilation and zipping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
LOGDIR=`pwd`/../log | |
# Log files start with... | |
LOGPREFIX=$LOGDIR/test | |
# Logs for days where at least part of the day is more than $ZIPAFTER hours ago will be zipped. | |
# Note the subtlety: if this is set to 24, then _all_ of yesterday will be zipped, not just the | |
# hours up until 24 hours before the current time. | |
ZIPAFTER=48 | |
# Zsh extended globbing is used for the (Nmh+24) modifier. Yeah, you could just use 'find', but | |
# where's the fun in that? | |
setopt extendedglob | |
# Concatenate files from the same day. However, we need to get all files | |
# for a day, even if some are "too young": some might be more than $ZIPAFTER | |
# hours, while others aren't. | |
for f in ${LOGPREFIX}.20??????-*.log(Nmh+$ZIPAFTER); do | |
# The glob in the 'for' above is fixed by this point, even if/when we | |
# subsequently remove the file. So, we have to check if $f _still_ | |
# exists. | |
if [[ -f $f ]]; then | |
# Remove the trailing time: we'll concatenate these files before zipping | |
BASEFILE=${f%-*.log} | |
# Clear the log for the day so we start fresh | |
echo -n > $BASEFILE.log | |
# As $f still exists, it means this is the first run, so we build | |
# the log for the entire day. The result is that we will include | |
# all files from the day, NOT just the ones that are more than 24 | |
# hours old. | |
for g in ${BASEFILE}-*.log; do | |
# Concatenate the log | |
echo "# $g" >> $BASEFILE.log | |
cat $g >> $BASEFILE.log | |
# And remove the file. This bit's why the 'if' is necessary above. | |
rm $g | |
done | |
# Zip it | |
bzip2 -9 $BASEFILE.log | |
# And move it to the old dir if there is one. | |
if [[ -d $LOGDIR/old ]]; then | |
mv $BASEFILE.log.bz2 $LOGDIR/old | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment