The mattermost-retention.sh
script assists in managing and cleaning up your Mattermost server to retain only a specific number of days' worth of messages and files. The steps performed by the script include:
-
Environment Setup: The script sets up the essential environment variables required for database connections, such as database name, user, password, host, and other related configurations like the Mattermost hostname, retention days, and Mattermost data directory.
-
Container Identification: Based on the Docker images used by the PostgreSQL and Mattermost services, the script identifies the running containers' IDs. This makes the script flexible, not relying on fixed container names.
-
Cron Job Management: For automation, the script first removes any existing cron jobs related to itself and then schedules a new one to run at 8 AM UTC daily.
-
Retention Time Calculation: The script computes the timestamp that corresponds to the specified retention days in the past.
-
Database Interactions: Depending on whether you're using PostgreSQL or MySQL (based on your configuration), the script communicates with the database running inside a Docker container to:
- Fetch file paths of files created before the retention date and save these paths in a temporary file.
- Delete posts and file information that were created before the retention date.
-
File Deletion: The script accesses the file paths from the temporary file and erases each one from the Mattermost server running within a Docker container.
-
Cleanup: The script gets rid of the temporary file and removes any empty directories in the Mattermost data path.
By executing these steps, the script ensures that the Mattermost server retains only messages and files from the specified retention days, allowing you to manage storage more efficiently.
Before executing the script, update the environment variables at the beginning of the mattermost-retention.sh
to match your configuration.
To make the script executable, modify its permissions using:
chmod +x mattermost-retention.sh
After this, you can run the script with:
./mattermost-retention.sh
After adding mattermost-retention.sh
to the crontab and it has executed, you can check the logs to verify if it operated successfully or if there were any issues.
You can find the logs for mattermost-retention.sh
in the directory from which you initiated the script.
I’m Vladimir Mikhalev, the Docker Captain, but my friends can call me Valdemar.
🌐 My website with detailed IT guides
🎬 Follow me on YouTube
🐦 Follow me on Twitter
🎨 Follow me on Instagram
🐘 Follow me on Mastodon
🧊 Follow me on Bluesky
🎸 Follow me on Facebook
🎥 Follow me on TikTok
💻 Follow me on LinkedIn
🐈 Follow me on GitHub
👾 Chat with IT pros on Discord
📧 Reach me at ask@sre.gg
💎 Support on GitHub
🏆 Support on Patreon
🥤 Support on BuyMeaCoffee
🍪 Support on Ko-fi
💖 Support on PayPal