-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to remove old seen messages
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,41 @@ | ||
#!/usr/bin/env python3 | ||
"""Remove seen messages that are older than two days | ||
if maildir has more than 80 MB of messages.""" | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
|
||
def getdirsize(path): | ||
return sum(f.stat().st_size for f in path.glob("**/*") if f.is_file()) | ||
|
||
|
||
def parse_dovecot_seen(path): | ||
return "S" in path.name.split(":2,")[-1] | ||
|
||
|
||
def main(): | ||
now = time.time() | ||
|
||
mailhome = Path(sys.argv[1]) | ||
|
||
for p in mailhome.iterdir(): | ||
dirsize = getdirsize(p / "cur") + getdirsize(p / "new") | ||
if dirsize < 80000000: | ||
continue | ||
|
||
removed_bytes = 0 | ||
for mailpath in (p / "cur").iterdir(): | ||
seen = parse_dovecot_seen(mailpath) | ||
stat = mailpath.stat() | ||
size = stat.st_size | ||
if seen and now > stat.st_mtime + 2 * 24 * 3600: | ||
removed_bytes += size | ||
mailpath.unlink(missing_ok=True) | ||
|
||
if removed_bytes > 0: | ||
(p / "maildirsize").unlink(missing_ok=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |