Best practices for managing MailShrink across multi-server hosting environments, automated maintenance cronjobs, and fleet-wide storage optimization.
MailShrink compiles to a single self-contained static binary with zero runtime dependencies. You can distribute and update it across thousands of cPanel, DirectAdmin, or custom Linux mail servers using a lightweight Ansible task.
This playbook downloads the latest binary directly from GitHub Releases, installs it to /usr/local/bin, verifies execution, and runs the built-in Dovecot zlib pre-flight check:
---
- name: Deploy MailShrink to Mail Servers
hosts: mail_servers
become: yes
vars:
mailshrink_version: "v2026.08.15"
mailshrink_arch: "{{ 'arm64' if ansible_architecture == 'aarch64' else 'amd64' }}"
tasks:
- name: Download MailShrink binary
ansible.builtin.get_url:
url: "https://github.com/nemke82/mailshrink/releases/download/{{ mailshrink_version }}/mailshrink-linux-{{ mailshrink_arch }}"
dest: "/usr/local/bin/mailshrink"
mode: '0755'
owner: root
group: root
- name: Verify MailShrink version
ansible.builtin.command: /usr/local/bin/mailshrink version
register: mailshrink_ver
changed_when: false
- name: Run Dovecot readiness pre-flight check
ansible.builtin.command: /usr/local/bin/mailshrink check
register: dovecot_check
changed_when: false
failed_when: false
- name: Print Dovecot readiness status
ansible.builtin.debug:
msg: "{{ dovecot_check.stdout }}"
Because MailShrink is compiled with CGO_ENABLED=0, it works seamlessly on AlmaLinux 8/9, Rocky Linux, CentOS, Ubuntu 20.04/22.04/24.04, and Debian without installing Go, Python modules, or pip packages.
When managing servers with hundreds of accounts and hundreds of gigabytes of Maildir storage, MailShrink provides multi-threaded scanning and structured JSON output for scriptable storage auditing.
By default, MailShrink uses 4 parallel workers. On servers with NVMe or high-speed disk arrays, you can scale concurrency up to speed up discovery:
# Scan all mailboxes under /home with 16 parallel threads
mailshrink analyze --path /home -j 16
# Scan only emails older than 2 years
mailshrink analyze --older-than 2y
Using the --json flag, you can pipe MailShrink results into jq or reporting pipelines:
# Find the top 5 domains by reclaimable disk space
mailshrink analyze --json | jq '.estimates | to_entries | sort_by(-.value.EstimatedSavings) | .[0:5][] | {domain: .key, savings_mb: (.value.EstimatedSavings / 1048576 | round)}'
Generate a spreadsheet report of compressible mailboxes for customer billing or quota reviews:
# Generate CSV from plan output
echo "Account,Folder,Period,Size_Bytes,Est_Savings_Bytes" > /var/log/mailshrink_report.csv
mailshrink plan --json | jq -r '.[] | "\(.account),\(.folder),\(.period),\(.size),\(.estimate.EstimatedSavings // 0)"' >> /var/log/mailshrink_report.csv
Compressing old emails should be a hands-off, recurring maintenance task. MailShrink's atomic design, advisory locking, and mtime preservation ensure it can run safely in the background on live production servers.
To guarantee that compression never causes I/O spikes or slows down active IMAP/SMTP deliveries, always run cronjobs with nice -n 19 (lowest CPU priority) and ionice -c 3 (idle disk I/O class):
# /etc/cron.d/mailshrink - Weekly automated Dovecot Maildir compression
# Runs every Sunday at 02:30 AM
30 2 * * 0 root /usr/bin/ionice -c 3 /usr/bin/nice -n 19 /usr/local/bin/mailshrink compress \
--path /home \
--older-than 1y \
--apply \
>> /var/log/mailshrink.log 2>&1
Different folders accumulate data at different rates. Here are proven production recipes:
mailshrink compress --folder Sent --older-than 6m --apply
mailshrink compress --older-than 2y --apply
MailShrink inspects the magic bytes of every email before compressing. If a file is already gzip-compressed, MailShrink skips it instantly (0 ms). Running a weekly cronjob is ultra-fast because it only touches newly aged emails.
Transferring massive Maildirs across servers (e.g., during cPanel-to-cPanel transfers, DirectAdmin migrations, or rsync syncs) is often bottlenecked by millions of small files and heavy disk usage.
mailshrink check on both source and target servers to ensure Dovecot's zlib plugin is enabled on both sides.mailshrink compress --path /home --older-than 6m --apply
rsync -aHAX. The transfer completes 25% to 40% faster, saving gigabytes of network bandwidth and disk on the destination.If your monitoring system (e.g., Zabbix, Prometheus, Datadog, or Munin) detects that a mail partition has crossed 90% utilization, you can trigger MailShrink automatically to reclaim critical disk space before service degradation occurs.
#!/usr/bin/env bash
# Emergency disk space reclamation for Dovecot Maildir
set -euo pipefail
DISK_USAGE=$(df -h /home | awk 'NR==2 {print $5}' | tr -d '%')
THRESHOLD=90
if [ "$DISK_USAGE" -ge "$THRESHOLD" ]; then
logger -t mailshrink "Disk usage on /home is ${DISK_USAGE}%. Triggering emergency MailShrink compression..."
# Compress Sent items older than 90 days
/usr/local/bin/mailshrink compress --path /home --folder Sent --older-than 90d --apply
# Compress any mail older than 1 year
/usr/local/bin/mailshrink compress --path /home --older-than 1y --apply
NEW_USAGE=$(df -h /home | awk 'NR==2 {print $5}' | tr -d '%')
logger -t mailshrink "Emergency compression completed. New disk usage: ${NEW_USAGE}%."
fi