Skip to content

Instantly share code, notes, and snippets.

@noah
Forked from cs278/dovecot-maildir-compress.sh
Last active November 30, 2022 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noah/c9379348af7479bc19de173f379cc5d4 to your computer and use it in GitHub Desktop.
Save noah/c9379348af7479bc19de173f379cc5d4 to your computer and use it in GitHub Desktop.
Compresses email in maildir format with check for already-compressed files
#!/bin/bash
# Enable zlib in Dovecot alongside automatic writing of new emails in gzipped format
## Convert Existing Emails using process described with the pseudocode here: https://wiki.dovecot.org/Plugins/Zlib
##
##
store=.
compress=bzip2
find "$store" -type d -name "cur" | while read maildir;
do
tmpdir=$(cd "$maildir/../tmp" &>/dev/null && pwd) || exit 1
#find=$(find "$maildir" -type f -name "*,S=*" -mtime +30 ! -name "*,*:2,*,*Z*" -printf "%f\n")
#find=$(find "$maildir" -type f -name "*,S=*" ! -name "*,*:2,*,*Z*" -printf "%f\n")
find=$(find "$maildir" -type f ! -name "*,*:2,*,*Z*" -printf "%f\n")
if [ -z "$find" ];
then
continue
fi
echo "Before compression:"
du -h "$maildir"
echo "$find" | while read filename;
do
srcfile="$maildir/$filename"
tmpfile="$tmpdir/$filename"
# if the file is not already compressed...
"$compress" -q -t "$srcfile" > /dev/null 2>&1
if [ $? -ne 0 ]; then
$compress --best --stdout "$srcfile" > "$tmpfile" &&
# Copy over some things
chown --reference="$srcfile" "$tmpfile" &&
chmod --reference="$srcfile" "$tmpfile" &&
touch --reference="$srcfile" "$tmpfile"
fi
done
echo "$find" | while read filename;
do
srcfile=$maildir/$filename
tmpfile=$tmpdir/$filename
"$compress" -q -t "$srcfile" > /dev/null 2>&1
if [ $? -ne 0 ]; then
flags=$(echo $filename | awk -F:2, '{print $2}')
if echo $flags | grep ',';
then
newname=$filename"Z"
else
newname=$filename",Z"
fi
dstfile=$maildir/$newname
if [ -f "$srcfile" ] && [ -f "$tmpfile" ];
then
#echo "$srcfile -> $dstfile"
mv "$tmpfile" "$srcfile" &&
mv "$srcfile" "$dstfile"
else
rm -f "$tmpfile"
fi
fi
done
echo "After compression:"
du -h "$maildir"
find "$maildir" -type f -name dovecot.index\* -delete
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment