-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat: Add mailbox_cleaner.py script for automated cleanup #6493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
drsound
wants to merge
2
commits into
mailcow:staging
Choose a base branch
from
drsound:feat/add-mailbox-cleaner
base: staging
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| This script cleans up old messages from specified mailboxes (e.g., Trash, Junk) | ||
| in a Mailcow environment. It can process a single user or all users, and it | ||
| supports dry-run mode. | ||
|
|
||
| Ideally, this script should be run daily via cron. | ||
| """ | ||
|
|
||
| import argparse | ||
| import logging | ||
| import os | ||
| import re | ||
| import subprocess | ||
| import sys | ||
|
|
||
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') | ||
|
|
||
| DEFAULT_DAYS_BACK: int = 30 | ||
| DEFAULT_MAILCOW_DIR: str = "/opt/mailcow-dockerized" | ||
| DEFAULT_MAILBOXES: list[str] = ["Trash", "Junk"] | ||
|
|
||
|
|
||
| def _run_doveadm_command(mailcow_dir: str, user: str | None, command: list[str]) -> str: | ||
| """ | ||
| Runs a doveadm command within the dovecot-mailcow container. | ||
|
|
||
| Args: | ||
| mailcow_dir: The path to the mailcow-dockerized directory. | ||
| user: The email address of the user to run the command for, or None for all users. | ||
| command: The doveadm command to run as a list of strings. | ||
|
|
||
| Returns: | ||
| The standard output of the command. | ||
| """ | ||
| command = ["docker", "compose", "--project-directory", mailcow_dir, "exec", "-T", "dovecot-mailcow", | ||
| "doveadm"] + command | ||
| if user: | ||
| command.extend(["-u", user]) | ||
| logging.debug(f"Executing command: {' '.join(command)}") | ||
| try: | ||
| result = subprocess.run(command, capture_output=True, text=True, check=True) | ||
| return result.stdout.strip() | ||
| except subprocess.CalledProcessError as e: | ||
| logging.error(f"Command execution failed: {' '.join(command)} (return code: {e.returncode})") | ||
| logging.error(f"Stderr: {e.stderr}") | ||
| logging.error(f"Stdout: {e.stdout}") | ||
| raise | ||
|
|
||
|
|
||
| def main() -> None: | ||
| """ | ||
| Main function to parse arguments and execute the cleanup process. | ||
| """ | ||
| parser = argparse.ArgumentParser( | ||
| description="Clean up old messages from specified mailboxes in a Mailcow environment.", | ||
drsound marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
| ) | ||
| group = parser.add_mutually_exclusive_group(required=True) | ||
| group.add_argument("--user", help="Email address of the single user to process.") | ||
| group.add_argument("--all", action="store_true", help="Process all users found via doveadm.") | ||
| parser.add_argument("--days-back", type=int, default=DEFAULT_DAYS_BACK, | ||
| help="Number of days back to consider for message deletion.") | ||
| parser.add_argument("--mailcow-directory", default=DEFAULT_MAILCOW_DIR, | ||
| help="Path to the mailcow-dockerized directory.") | ||
| parser.add_argument("--mailboxes", nargs='+', default=DEFAULT_MAILBOXES, | ||
| help="List of top-level mailboxes (and their subfolders) to process (e.g., Trash Junk).") | ||
| parser.add_argument("--debug", action="store_true", help="Enable debug logging.") | ||
| parser.add_argument("--dry-run", action="store_true", help="Perform a dry run without deleting anything.") | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if args.debug: | ||
| logging.getLogger().setLevel(logging.DEBUG) | ||
|
|
||
| if not os.path.isdir(args.mailcow_directory): | ||
| raise FileNotFoundError( | ||
| f"Mailcow directory '{args.mailcow_directory}' does not exist or is not a directory.") | ||
drsound marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # If --all is specified, get all users | ||
| if args.all: | ||
| doveadm_output = _run_doveadm_command(args.mailcow_directory, None, ["user", "*"]) | ||
| users = [line.strip() for line in doveadm_output.splitlines() if line.strip()] | ||
| # Otherwise, use the specified user | ||
| else: | ||
| try: | ||
| _run_doveadm_command(args.mailcow_directory, None, ["user", args.user]) | ||
| except subprocess.CalledProcessError: | ||
| logging.error(f"User '{args.user}' not found.") | ||
| sys.exit(1) | ||
| users = [args.user] | ||
| logging.info(f"Starting processing for {len(users)} users.") | ||
| logging.debug(f"Users to process: {', '.join(users)}") | ||
| # Iterate over each user | ||
| for user in users: | ||
| # Get all mailboxes for the current user | ||
| logging.info(f"Processing user: '{user}'.") | ||
| doveadm_output = _run_doveadm_command(args.mailcow_directory, user, ["mailbox", "list"]) | ||
| # get all user mailboxes, sorted in reverse order | ||
| mailboxes = sorted([line.strip() for line in doveadm_output.splitlines() if line.strip()], reverse=True) | ||
| logging.info(f"User '{user}' has {len(mailboxes)} mailboxes.") | ||
| logging.debug(f"Mailboxes for user '{user}': {', '.join(mailboxes)}") | ||
| for mailbox in mailboxes: | ||
| # Iterate over each mailbox | ||
| logging.debug(f"Processing mailbox '{mailbox}' for user '{user}'.") | ||
| # Check if the mailbox is a target mailbox | ||
| if not any(re.match(rf"{re.escape(tmb)}(/|$)", mailbox, re.IGNORECASE) for tmb in args.mailboxes): | ||
| logging.debug(f"Skipping mailbox '{mailbox}' for user '{user}' as it is not a target mailbox.") | ||
| continue | ||
| # Expunge old messages from the mailbox | ||
| logging.info( | ||
| f"Expunging messages older than {args.days_back} days from mailbox '{mailbox}' for user '{user}'.") | ||
| if args.dry_run: | ||
| logging.info(f"[DRY-RUN] Skipping expunge command for mailbox '{mailbox}' of user '{user}'.") | ||
| else: | ||
| # Run the expunge command | ||
| _run_doveadm_command(args.mailcow_directory, user, | ||
| ["expunge", "mailbox", mailbox, "savedbefore", f"{args.days_back}d"]) | ||
| # Check if the mailbox is a sub-mailbox | ||
| if "/" not in mailbox: | ||
| logging.debug( | ||
| f"Skipping deletion check for top-level mailbox '{mailbox}' to preserve standard folders.") | ||
| continue | ||
| # Check if the mailbox is empty | ||
| doveadm_output = _run_doveadm_command(args.mailcow_directory, user, ["mailbox", "status", "messages", mailbox]) | ||
| messages_count = int(doveadm_output.split("=")[1]) | ||
| logging.debug(f"Mailbox '{mailbox}' for user '{user}' contains {messages_count} messages.") | ||
| if messages_count > 0: | ||
| logging.info(f"Skipping deletion of mailbox '{mailbox}' for user '{user}' as it is not empty.") | ||
| continue | ||
| # Delete the mailbox if it's empty | ||
| logging.info(f"Deleting mailbox '{mailbox}' for user '{user}' (only if empty).") | ||
| if args.dry_run: | ||
| logging.info(f"[DRY-RUN] Skipping delete command for mailbox '{mailbox}' of user '{user}'.") | ||
| else: | ||
| # As a safeguard, -e flag prevents mailbox deletion in case it's not empty | ||
| _run_doveadm_command(args.mailcow_directory, user, ["mailbox", "delete", "-e", "-s", mailbox]) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.