Professional Documents
Culture Documents
#!/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.
# --------------------------------------------------------------------
# Wait for log rotation to complete (adjust based on expected rotation time)
sleep 120
#!/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.
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.
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
Explanation:
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