You are on page 1of 3

Automation for log rotation: Post 3 of 5 [Script]

#!/bin/bash

# --------------------------------------------------------------------
# Script Name: mongodb_log_rotate_and_cleanup.sh
# Author: Shivam Agihotri
# Date: (29 FEB 2024)
# Description: Rotates MongoDB logs and cleans up older versions.
# --------------------------------------------------------------------

# Define MongoDB connection details (improve security with secrets management)


MONGODB_USERNAME="<USERNAME>"
MONGODB_PASSWORD="<PASSWORD>" # Consider using environment variables or a
secure store

# Trigger MongoDB log rotation


mongo_command="db.adminCommand({ logRotate: 1 })"
mongosh --eval "$mongo_command" -u "$MONGODB_USERNAME" -p
"$MONGODB_PASSWORD"

# Wait for log rotation to complete (adjust based on expected rotation time)
sleep 120

# Define search criteria for old log files


log_dir="/var/log/mongodb/"
log_prefix="mongod.log.2024"
max_file_age=5 # Files older than 5 days will be removed

# Find and delete old log files


find "$log_dir" -name "$log_prefix*" -mtime +"$max_file_age" -exec rm -rf {} \;

echo "MongoDB log rotation and cleanup completed."


Explanation

#!/bin/bash: This line is called a shebang and indicates that the script should be
executed using the Bash shell.

Comments: Lines starting with # are comments and are ignored by the shell. They are
used to provide information about the script, such as its purpose, author, and date.

MONGODB_USERNAME="<USERNAME>" and
MONGODB_PASSWORD="<PASSWORD>": These lines define variables to store the
MongoDB username and password. These credentials are used to authenticate with the
MongoDB instance.

mongo_command="db.adminCommand({ logRotate: 1 })": This line defines a variable


mongo_command which stores the MongoDB command to rotate logs. The logRotate
command is a MongoDB command to initiate log rotation.

mongosh --eval "$mongo_command" -u "$MONGODB_USERNAME" -p


"$MONGODB_PASSWORD": This line executes the MongoDB command stored in the
mongo_command variable using the mongosh shell. It passes the MongoDB username
and password for authentication.

sleep 120: This line pauses the script execution for 120 seconds (2 minutes) to allow
time for the log rotation process to complete. The duration of the sleep can be adjusted
based on the expected time for log rotation.

log_dir="/var/log/mongodb/", log_prefix="mongod.log.2024", and max_file_age=5:


These lines define variables to store the directory where MongoDB log files are located
(log_dir), the prefix of the log file names (log_prefix), and the maximum age (in days) of
log files to retain (max_file_age).

find "$log_dir" -name "$log_prefix*" -mtime +"$max_file_age" -exec rm -rf {} \;: This
line uses the find command to search for log files in the specified directory (log_dir) that
match the specified prefix (log_prefix) and are older than the specified maximum age
(max_file_age). It then executes the rm -rf command to remove those files.

echo "MongoDB log rotation and cleanup completed.": This line prints a message to
indicate that the MongoDB log rotation and cleanup process has been completed.
If you want to execute this script on every Sunday at 6pm then you can update like this
in crontab:
0 18 * * 0 /path/to/your/script.sh

You can customize it as per your need.

Explanation:

0: Minute (0 represents 6pm)


18: Hour (24-hour format; 18 corresponds to 6pm)
*: Day of month (ignored since we specify day of week)
*: Month (ignored since we specify day of week)
0: Day of week (0 or 7 both represent Sunday)
/path/to/your/script.sh: The path to your script file

Follow Shivam Agnihotri on LinkedIn for useful DevOps Content.

Join DevOps Ocean Group: https://www.linkedin.com/groups/9189158/

If you are looking for a Dedicated 1:1 session with me to boost your DevOps
Productivity, then please book a session from here: https://topmate.io/shivam_agnihotri

You might also like