Jamyy's Weblog

在 Linux 命令列備份 Gmail 郵件 - Part 2: the IMAP way

by Jamyy on 十月.15, 2013, under Linux

前提: 於 Gmail "啟用 IMAP"

  1. 在 CentOS 6 使用 imapsync 將 Gmail 郵件同步到指定郵箱
  2. 在 Linux Mint 15 使用 mailsync 雙向同步郵件內容
  3. 在 Linux Mint 15 使用 mbsync 雙向同步郵件內容
  4. 在 Linux Mint 15 使用 imapcopy 將 Gmail 郵件匯入指定郵箱

回顧: 在 Linux 命令列備份 Gmail 郵件 - Part 1: the POP way

CentOS 6 - imapsync
$ su -c "rpm -Uvh http://mirror01.idc.hinet.net/EPEL/6/i386/epel-release-6-8.noarch.rpm"
$ su -c "yum -y install imapsync"
$ imapsync \
--host1 imap.gmail.com --user1 gmailaccount --password1 secret --ssl1
--host2 my.linux.host --user2 myaccount --password2 secret

說明: 將 host1、host2 參數內容對調就是把 my.linux.host 郵件備份到 Gmail
參考: Migrate mail from one server to another with imapsync - Linux Brigade

Linux Mint 15 - mailsync
$ sudo apt-get install mailsync
$ vi ~/.mailsync

store gmail {
        server  {imap.gmail.com/user=account/ssl}
        ref     {imap.gmail.com}
        pat     %
        passwd  secret
}

# 遠端伺服器
store remote {
        server  {my.linux.host/user=account/ssl/novalidate-cert}
        ref     {my.linux.host}
        pat     %
        passwd  secret
}

# 本機 ~/mail
store local {
        pat     mail/%
        prefix  mail/
}

channel A gmail remote {
        msinfo mail/synchronization-info
}

channel B gmail local {
        msinfo mail/synchronization-info
}

同步 gamil ←→ remote
$ mailsync -n -D -m A

同步 gamil ←→ local
$ mailsync -n -D -m B

說明: mailsync 會進行雙向同步, 若只要單向傳遞, 其中一個 store 目標沒有郵件即可
參考: codesink: Move all your emails to Gmail

Linux Mint 15 - mbsync
$ mkdir ~/.certs
$ openssl s_client -connect imap.gmail.com:993 -showcerts

將第一組的 -----BEGIN CERTIFICATE----- 到 -----END CERTIFICATE----- 複製起來
存入 ~/.certs/imap.gmail.com.pem

$ openssl s_client -connect my.linux.host:993 -showcerts

將第一組的 -----BEGIN CERTIFICATE----- 到 -----END CERTIFICATE----- 複製起來
存入 ~/.certs/my.linux.host.pem

$ sudo apt-get install isync
$ vi ~/.mbsyncrc

IMAPAccount gmail
Host imap.gmail.com
User account
Pass secret
UseIMAPS yes
CertificateFile ~/.certs/imap.gmail.com.pem

IMAPAccount myhost
Host my.linux.host
User account
Pass secret
UseIMAPS yes
CertificateFile ~/.certs/my.linux.host.pem

IMAPStore gmail-remote
Account gmail

IMAPStore gmail-local
Account myhost

Channel gmail
Master :gmail-remote:
Slave :gmail-local:
Patterns * ![Gmail]* "[Gmail]/Sent Mail" "[Gmail]/Starred" "[Gmail]/All Mail"

$ yes | mbsync gmail

說明: 管道傳遞 yes 是為了自動回答 Accept this certificate anyway? [no]
參考: isync - ArchWiki

Linux Mint 15 - imapcopy
$ sudo apt-get install openssl stunnel imapcopy
$ openssl req -new -x509 -days 3650 -nodes -out stunnel.pem -keyout stunnel.pem
$ vi stunnel-to-gmail.conf

cert = stunnel.pem
foreground=yes

; Some performance tunings
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1

; Some debugging stuff useful for troubleshooting
debug = 7

; Use it for client mode
client = yes

; Service-level configuration

[imap]
accept = 1143
connect = imap.gmail.com:993
retry = yes

$ sudo stunnel4 stunnel-to-gmail.conf
$ cp /usr/share/doc/imapcopy/examples/ImapCopy.cfg ./
$ vi ImapCopy.cfg

SourceServer localhost
SourcePort 1143
DestServer my.linux.host
DestPort 143
DenyFlags "\Recent"
converttimezone "UTC" "+0800"
converttimezone "UT"  "+0800"
Copy "account-gmail" "password-gmail" "account-myhost" "password-myhost"

$ imapcopy

說明: imapcopy 只是 "複製" 並非 "同步", 重複執行 imapcopy 指令會產生重複郵件
參考: Migrating one Gmail account to another - Akom's Tech Ruminations



:,