Skip to content

Instantly share code, notes, and snippets.

@sitano
Last active August 29, 2015 14:21
Show Gist options
  • Save sitano/906963bf419df78a296c to your computer and use it in GitHub Desktop.
Save sitano/906963bf419df78a296c to your computer and use it in GitHub Desktop.
glog rotation + gzip
#!/bin/bash
NAME="service"
LIMIT="8000"
DELETELIMIT="500"
# Packing
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) && cd $DIR
FILES=`ls -A1Lct ${NAME}.* | grep -v '.sh$' | grep -v '.zip$' | grep -v '.gz$'`
TYPES=`echo "$FILES" | grep -E '.[A-Z]+$' | wc -l`
LINKS=`ls -A1Lct ${NAME}.* | grep -E '.*[A-Z]+$' | xargs -I % readlink % | sort`
FILES=`echo "$FILES" | grep -Ev '.[A-Z]+$' | sort`
# Explicitly skip current + one more bunch of logs cause they can be
# weakly ordered
TOPACK=`comm -3 <(echo "$FILES") <(echo "$LINKS")`
if [ ! -z "$TOPACK" ]; then
echo "$TOPACK" | xargs gzip
fi
# Cleanup
while [ `ls -sk ${NAME}.*.gz | awk 'BEGIN{s=0}{s=s+\$0}END{print int(s/1024)}'` -gt "${LIMIT}" -a "${DELETELIMIT}" -gt "0" ]; do
DELETELIMIT=`expr $DELETELIMIT - 1`
FILE=`ls -L1ct ${NAME}.* | tail -n 1`
if [ -f "${FILE}" ]; then
rm "${DIR}/${FILE}" > /dev/null 2>&1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment