Email Backup: Comparing offlineimap, mbsync, and doveadm
Backing up a complete IMAP account with thousands of messages across nested folders requires automation to preserve folder structure and avoid manual errors. This guide covers three modern tools and provides detailed setup for the most flexible option.
Choosing your backup tool
offlineimap — Most flexible for complex folder structures and label handling. Active maintenance resumed, widely packaged.
mbsync (isync) — Lightweight and fast, excellent for large accounts (500k+ messages). Minimal configuration, efficient sync algorithm.
doveadm — Built-in to Dovecot servers, useful if backing up directly from your mail server. Best for local or nearby systems.
imapync — Simple one-way sync, good for straightforward backups, but less flexible for folder mapping.
This guide uses offlineimap for its maturity and control, but includes mbsync alternatives where applicable.
Installing offlineimap
On Fedora/RHEL/CentOS:
sudo dnf install offlineimap
On Debian/Ubuntu:
sudo apt install offlineimap
On Arch:
sudo pacman -S offlineimap
Verify installation:
offlineimap --version
Basic configuration
Create ~/.offlineimaprc:
[general]
accounts = MyIMAP
maxsyncaccount = 1
autorefresh = 0
socktimeout = 30
[Account MyIMAP]
localrepository = Local
remoterepository = Remote
synclabels = yes
readonly = true
[Repository Local]
type = Maildir
localfolders = ~/Mail/backup
createfolders = yes
sep = /
[Repository Remote]
type = IMAP
remotehost = imap.example.com
remoteuser = your.email@example.com
remotepass = your_password_here
ssl = yes
sslcacertfile = /etc/ssl/certs/ca-certificates.crt
keepalive = 60
timeout = 30
Key configuration points:
readonly = true— Prevents accidental writes to the IMAP server during backupssl = yes— Required for modern servers; verify your CA certificate pathcreatefolders = yes— Creates local directories matching remote folder structuresocktimeout = 30— Prevents hangs on slow connectionskeepalive = 60— Maintains connection stability; increase to 120 for large syncs
Secure credential handling
Never store plaintext passwords in config files. Use a separate password file:
[Repository Remote]
remotepassfile = ~/.config/offlineimap/password
Create the credentials file:
mkdir -p ~/.config/offlineimap
echo "your_actual_password" > ~/.config/offlineimap/password
chmod 600 ~/.config/offlineimap/password
For OAuth2 or app-specific passwords, use the same approach — offlineimap accepts any credential string in the password file.
Running your first backup
Start the sync:
offlineimap
For a one-time run without interactive prompts:
offlineimap -o -u Quiet
Monitor with verbose IMAP debugging:
offlineimap -d IMAP
Watch progress in another terminal:
watch -n 5 'find ~/Mail/backup -type f | wc -l'
Handling large accounts
For accounts with 100k+ messages, optimize sync performance:
Add to [Repository Remote]:
maxage = 90
timeout = 60
keepalive = 120
maxage = 90 syncs only messages from the last 90 days initially. Remove this line in subsequent runs to fetch remaining older messages.
Run overnight in a persistent session:
nohup offlineimap -u Quiet > ~/offlineimap.log 2>&1 &
Or use tmux:
tmux new-session -d -s mailsync 'offlineimap'
Working with backed-up emails
Emails are stored in Maildir format (each message as a separate file). Browse with mutt:
mutt -f ~/Mail/backup/INBOX
Or use neomutt, Thunderbird, or Evolution with your backup directory as a local account.
Convert to mbox format for archival:
find ~/Mail/backup -type d -mindepth 1 -exec bash -c 'cat "$0"/cur/* 2>/dev/null > "${0}.mbox"' {} \;
Archiving for long-term storage
After successful sync, compress the backup:
tar --lz4 -cf mail-backup-$(date +%Y%m%d).tar.lz4 ~/Mail/backup
Or with gzip:
tar --gzip -cf mail-backup-$(date +%Y%m%d).tar.gz ~/Mail/backup
Verify archive integrity:
tar -tf mail-backup-*.tar.lz4 | head -20
Incremental backups with cron
Set up daily syncs to capture new messages:
0 2 * * * /usr/bin/offlineimap -u Quiet 2>&1 | logger -t offlineimap
This runs at 2 AM daily, logs output to syslog. Subsequent runs only download new or changed messages, making incremental backups fast.
For monitoring:
journalctl -t offlineimap -n 20
Alternative: Using mbsync for faster large backups
If offlineimap is too slow for your account, try mbsync:
Install:
sudo apt install isync # or 'dnf install isync'
Create ~/.mbsyncrc:
IMAPAccount example
Host imap.example.com
User your.email@example.com
PassCmd "pass email/example"
SSLType IMAPS
IMAPStore remote
Account example
MaildirStore local
Path ~/Mail/backup/
Inbox ~/Mail/backup/INBOX
SubFolders Verbatim
Channel example
Far :remote:
Near :local:
Patterns *
Create Near
Sync Pull
Remove Near
Expunge Near
SyncState *
Run:
mbsync -a
mbsync is often 3-5x faster than offlineimap for initial large syncs.
Troubleshooting
Sync hangs:
[Repository Remote]
timeout = 60
keepalive = 120
SSL certificate errors:
Verify the correct CA bundle path for your system:
- Fedora/RHEL:
/etc/pki/tls/certs/ca-bundle.crt - Debian/Ubuntu:
/etc/ssl/certs/ca-certificates.crt - Arch:
/etc/ca-certificates/extracted/tls-ca-bundle.pem
Or fetch the server’s certificate directly:
openssl s_client -connect imap.example.com:993 -showcerts < /dev/null 2>/dev/null | grep -A 20 'BEGIN CERTIFICATE' > server.crt
Then reference it:
sslcacertfile = /path/to/server.crt
Folder mapping issues:
Manually map folders if automatic detection fails:
[Repository Remote]
nametrans = lambda foldername: foldername.replace('[Gmail]/', 'backup-gmail-').replace('All Mail', 'archive')
Connection timeouts on flaky networks:
Use autoresume = yes to resume incomplete syncs:
[general]
autoresume = yes
Check process logs:
tail -f ~/.offlineimap/logs/IMAP.Remote
