Dovecot 从 postfix 压缩现有的 maildir 邮件

Dovecot compress existing maildir mails from postfix

我从其他地方获取了代码片段并创建了一个脚本来压缩现有的 maildir 电子邮件(Dovecot 需要停止,因为我不检查锁)。

#!/bin/bash
store=/var/vmail/mydomain.com
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=*" ! -name "*,*:2,*,*Z*" -printf "%f\n")
  if [ -z "$find" ]; then
    echo continue
    continue
  fi

  echo "$find" | while read filename; do

    if file "$maildir/$filename" | grep -q "SMTP mail, "; then

      echo 'Uncompressed' "$filename"

      flags=$(echo $filename | awk -F:2, '{print }')

      if echo $flags | grep ','; then
        newname=$filename"Z"
      else
        newname=$filename",Z"
      fi

      srcfile="$maildir/$filename"
      tmpfile="$tmpdir/$filename"
      dstfile="$maildir/$newname"

      gzip -c "$srcfile" > "$tmpfile" &&
      chown --reference="$srcfile" "$tmpfile" &&
      chmod --reference="$srcfile" "$tmpfile" &&
      touch --reference="$srcfile" "$tmpfile"

      if [ -f "$srcfile" ] && [ -f "$tmpfile" ]; then
        mv "$tmpfile" "$srcfile" &&
        mv "$srcfile" "$dstfile"
      else
        rm -f "$tmpfile"
      fi

    elif file "$maildir/$filename" | grep -q "gzip compressed data"; then
      echo 'Compressed' "$filename"
    else
      echo 'Unknown type'
    fi

  done
done

但是我意识到我的入站邮件没有被找到,因为并非所有电子邮件都符合 -name "*,S=*"

的查找标准

例子

find "/var/vmail/mydomain.com" -type d -name "cur" | while read maildir; do find "$maildir" -type f -name "*,S=*" ! -name "*,*:2,*,*Z*" -printf "%f\n"; done

会找到这些:

/var/vmail/mydomain.com/Maildir/.Sent/cur
1580000001.M10001P10008.ip-172-1-1-100,S=14037,W=14302:2,Sa
1580000002.M20002P10009.ip-172-1-1-100,S=3784,W=3888:2,S

但不是这些:

/var/vmail/mydomain.com/Maildir/.INBOX/cur
1580000003.Vca01I80f58M300003.ip-172-1-1-100:2,S
1580000004.Vca01I80de7M400004.ip-172-1-1-100:2,Sa
1580000005.Vca01I85252M400005.ip-172-1-1-100:2,RS
1580000006.Vca01I83ffeM400006.ip-172-1-1-100:2,Sb
1580000007.Vca01I8159aM400007.ip-172-1-1-100:2,RSa
1580000008.Vca01I84015M400008.ip-172-1-1-100:2,Sab

我可以轻松更改查找命令以匹配入站邮件,但由于查找条件来自其他地方,我想知道是否有显式 "*,S=*"?

的原因

Ps。请注意,邮件不是通过 Dovecot 接收的,入站邮件由 postfix 提取,后者将邮件直接保存到相关目录,下面的 postfix 变量作为示例。

virtual_mailbox_base: /var/vmail
virtual_mailbox_maps: ldap lookup, %d/%u/Maildir/

您可以考虑在文件名中搜索子字符串 :2,

Dovecot: Maildir filename extensions

The standard filename definition is: ":2,". […]
* ,S=: contains the file size. Getting the size from the filename avoids doing a stat(), which may improve the performance. This is especially useful with Maildir++ quota.
* ,W=: contains the file's RFC822.SIZE, ie. the file size with linefeeds being CR+LF characters. […]

大写字母表示"standard flags",小写字母表示"local/user flags".