Skip to content

Instantly share code, notes, and snippets.

@jesbrd
Forked from slimflem/mongodb_logrotate.cron
Created August 6, 2016 18:17
Show Gist options
  • Save jesbrd/80be0598bf573ce22279d7b452124914 to your computer and use it in GitHub Desktop.
Save jesbrd/80be0598bf573ce22279d7b452124914 to your computer and use it in GitHub Desktop.
Rotate and compress mongodb logs
#!/bin/bash
# To be used as a cron job weekly (or other) placed under /etc/cron.weekly
# Will use mongodb's logrotate function, but then will compress and keep at most 12 weeks worth of rotate logs.
killall -SIGUSR1 `cat /var/run/mongodb/mongod.pid`
gzip /var/log/mongodb/*20[0-9][0-9]-*-[0-9][0-9]
logs_total=`ls -tx1 /var/log/mongodb/ | egrep "20[0-9][0-9]-.*\.gz" | wc -l`
if [ "$logs_total" -gt 12 ]; then
logs_difference=`expr $logs_total - 12`
logs_to_remove=`ls -tx1 /var/log/mongodb/ | egrep "20[0-9][0-9]-.*\.gz" | tail -n $logs_difference`
for log_file in $logs_to_remove; do
rm -f /var/log/mongodb/$log_file
done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment