Skip to content

Instantly share code, notes, and snippets.

@fabriziomello
Created May 30, 2014 00:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabriziomello/0667852afccf65dcb6fe to your computer and use it in GitHub Desktop.
Save fabriziomello/0667852afccf65dcb6fe to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# O pgbadger no modo incremental gera um indice (arquivos .bin)
# para fazer o parser do log, e não possui um mecanismo interno
# de limpeza de arquivos que não estão mais em uso.
#
# Este script efetua a limpeza dos .bin obsoletos, ou seja,
# aqueles que são de dias anteriores aos correspondentes a
# semana corrente.
#
# Na discussao abaixo contém informações:
# https://listes.dalibo.com/pipermail/pgbadger/2014-May/000045.html
#
# Dependencies:
# . seq
# . egrep
# . du
#
PGBADGER_OUTPUT_DIR="/var/www/pgbadger"
CURRENT_WEEK_DAYS=""
SEP=""
for i in $(seq 0 6)
do
DATE=$(date --d="last Sunday+$i days" +%Y-%m-%d)
CURRENT_WEEK_DAYS="${CURRENT_WEEK_DAYS}${SEP}${DATE}"
SEP="|"
done
CURRENT_WEEK_DAYS="($CURRENT_WEEK_DAYS)"
echo $CURRENT_WEEK_DAYS
echo "Directory size before... "
du -hs $PGBADGER_OUTPUT_DIR
for file in $(find $PGBADGER_OUTPUT_DIR -name "*.bin" | sort)
do
echo $file | egrep "$CURRENT_WEEK_DAYS" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo "Keeping file $file ..."
else
echo "Removing file $file ..."
rm -f $file
fi
done
echo "Directory size after... "
du -hs $PGBADGER_OUTPUT_DIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment