Last active
April 22, 2024 18:00
-
-
Save tkaefer/6ab9dcb4d560d16b1c5cb7012a5e5171 to your computer and use it in GitHub Desktop.
Archive mail by year and month with dovecot, see https://tkaefer.de/blog/2022/01/04/archive-mail-by-year-and-month-with-dovecot/
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
#!/bin/bash | |
USER="email@example.com" | |
# The path in dovecote mail folder to put the subfolders to | |
ARCHIVE_ROOT="INBOX.Archives" | |
# The paths of the folders to be processed during archive | |
BOXES_TO_ARCHIVE=("INBOX.Archive" "Archive*") | |
# Define from what - relatine to now - date in the past the archiving should be started | |
# use date "-d '...' " notation | |
# eg for initial imports this could be set to "-20 year" | |
ARCHIVE_START_DATE="-3 month" | |
# Define until which - relative to now - date mails should be archived, mails younger that date that date are omitted | |
# use date "-d '...' " notation | |
RETENTION="-1 month" | |
# Check if coreutils date is present in Alpine Linux | |
# "-/+ x month" operations in date are not supported in busybox date implementation | |
# Therefore coreutils date is required | |
date '+%Y-%m-%d' -d "+1 month" | |
if [ $? -ne 0 ]; then | |
# Install coreutils in Alpine Linux | |
apk add --no-cache coreutils | |
fi | |
date '+%Y-%m-%d' -d "+1 month" | |
if [ $? -ne 0 ]; then | |
# install failed | |
echo "installing coreutils date failed - exiting" | |
exit 1 | |
fi | |
# Search for all existing subfolders in BOXES_TO_ARCHIVE | |
BOXES=() | |
IFS=$'\n' | |
for BOX in ${BOXES_TO_ARCHIVE[@]}; do | |
BOXES+=($(doveadm mailbox list -u ${USER} ${BOX})) | |
done | |
ENDDATE=$(date '+%s' -d "${RETENTION}") | |
isodateiter="$(date '+%Y-%m-%d' -d "${ARCHIVE_START_DATE}")" | |
while [[ $(date +%s -d $isodateiter) -le $ENDDATE ]]; do | |
YEAR="${isodateiter:0:4}" | |
MONTH="${isodateiter:5:2}" | |
SINCE="${YEAR}-${MONTH}-01" | |
BEFORE="$(date '+%Y-%m-%d' -d "$isodateiter +1 month")" | |
for BOX in ${BOXES[@]}; do | |
# echo "# Check if there is anything to archive in BOX ${BOX} for period between BEFORE and SINCE" | |
# echo "doveadm search -u ${USER} MAILBOX ${BOX} SENTBEFORE ${BEFORE} SENTSINCE ${SINCE} | wc -l" | |
# Check if there is anything to archive in BOX ${BOX} for period between BEFORE and SINCE | |
echo "checking for mails in ${BOX} SINCE ${SINCE} AND BEFORE ${BEFORE}" | |
if [ $(doveadm search -u ${USER} MAILBOX ${BOX} SENTBEFORE ${BEFORE} SENTSINCE ${SINCE} | wc -l) -gt 0 ]; then | |
# Create and subscribe ARCHIVE subfolder if it doesn't exist | |
# echo "# Create and subscribe ARCHIVE subfolder if it doesn't exist" | |
ARCHIVE="${ARCHIVE_ROOT}.${YEAR}.${MONTH}" | |
doveadm mailbox status -u ${USER} messages ${ARCHIVE} >/dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
echo "creating new ARCHIVE Folder ${ARCHIVE}" | |
doveadm mailbox create -u ${USER} ${ARCHIVE} | |
doveadm mailbox subscribe -u ${USER} ${ARCHIVE} | |
fi | |
# Move the mails to ARCHIVE subfolder | |
# echo "# Move the mails to ARCHIVE subfolder" | |
echo "Move mails to ARCHIVE folder ${ARCHIVE}" | |
doveadm move -u ${USER} ${ARCHIVE} mailbox ${BOX} SENTSINCE ${SINCE} SENTBEFORE ${BEFORE} | |
fi | |
done | |
isodateiter="${BEFORE}" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment