#!/bin/bash

# Init variables
MONGODB_LOGROTATE_FILE="/etc/logrotate.d/mongodb"

# MongoDB LogRotate file creation
echo "Creating MongoDB logrotate configuration..." #############################
echo '/var/log/mongodb/*.log {
  daily
  rotate 5
  compress
  dateext
  missingok
  notifempty
  sharedscripts
  copytruncate
  postrotate
    /bin/kill -SIGUSR1 `cat /var/lib/mongo/mongod.lock 2> /dev/null` 2> /dev/null || true
  endscript
}' > $MONGODB_LOGROTATE_FILE
echo "Creating MongoDB logrotate configuration... Done" ########################

# MongoDB LogRotate test
echo "Start MongoDB logrotate..."
RESULT=`logrotate -f -v $MONGODB_LOGROTATE_FILE; echo $?`
if [ $RESULT -ne 0 ]; then
  echo "MongoDB Logrotate failed. Please check your configuration."
  exit 1
else
  echo "MongoDB logrotate succeed."
  exit 0
fi
