Created
April 1, 2016 19:28
-
-
Save slimflem/e4eaeb73e5bd72a1cc33891d0aa46d81 to your computer and use it in GitHub Desktop.
Rotate and compress mongodb logs
This file contains hidden or 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
#!/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