The complete open-source solution for backing up, restoring, and migrating IMAP email accounts.
Zero dependencies. Pure Python. Docker ready. S3 compatible. GPG encrypted.
# Simple backup
docker run --rm -v $(pwd)/backups:/data \
user2k20/imapbackup \
-s imap.example.com -u user@example.com -e
# With S3 upload and GPG encryption (no keyring needed!)
docker run --rm -v $(pwd)/backups:/data \
user2k20/imapbackup \
-s imap.gmail.com -u myemail@gmail.com -e \
--s3-upload \
--s3-endpoint=https://s3.hetzner.cloud \
--s3-bucket=backups \
--s3-access-key=$S3_KEY \
--s3-secret-key=$S3_SECRET \
--gpg-encrypt \
--gpg-recipient=backup@example.com \
--gpg-import-key=https://example.com/keys/public.asc
# Clone and setup
git clone https://github.com/chris2k20/imapbackup.git
cd imapbackup
python3 -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
# Run backup
python3 imapbackup.py -s imap.example.com -u user@example.com -e
- π¦ Zero Dependencies - Pure Python, no external libraries required
- π Incremental Backups - Only downloads new messages on subsequent runs
- π³ Docker Ready - Pre-built image:
user2k20/imapbackup
- βοΈ S3 Compatible - Works with AWS S3, MinIO, Hetzner, Wasabi, Backblaze
- π GPG Encryption - Encrypt backups with flexible key import
- π§ Universal IMAP - Works with Gmail, Office 365, ProtonMail, and any IMAP server
- π Restore Mode - Upload mbox files back to IMAP servers
- π Standard mbox Format - Import into any email client
- π Multi-Account Backup - Backup multiple accounts with YAML configuration
- π Flexible GPG Key Import - Import keys from files, URLs, or environment variables
- π« No Keyring Mounting - Encrypt without mounting
~/.gnupg
in Docker - βΈοΈ Kubernetes Ready - Perfect for cloud-native deployments
- π Safe Operations - Only performs read operations on source IMAP
- Docker Setup Guide - Get started with Docker in 5 minutes
- Python Setup Guide - Virtual environment installation
- Backup Guide - Complete backup strategies
- Restore Guide - How to restore emails
- Multi-Account Backup - Backup multiple accounts with YAML config π
- S3 Configuration - Configure cloud storage
- GPG Encryption - Secure your backups
- GPG Key Import - Flexible key management π
# Cron job for daily backups at 2 AM
0 2 * * * docker run --rm -v /backups:/data user2k20/imapbackup \
-s imap.example.com -u user@example.com -p @/root/.password -e \
--s3-upload --s3-bucket=backups --s3-access-key=$KEY --s3-secret-key=$SECRET
# Step 1: Backup from old server
python3 imapbackup.py -s old.server.com -u user@old.com -e
# Step 2: Restore to new server
python3 imapbackup.py -r -s new.server.com -u user@new.com -e
apiVersion: batch/v1
kind: CronJob
metadata:
name: email-backup
spec:
schedule: "0 2 * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: backup
image: user2k20/imapbackup:latest
args:
- -s
- imap.example.com
- -u
- user@example.com
- -e
- --s3-upload
- --gpg-encrypt
- --gpg-import-key=env:GPG_PUBLIC_KEY
# Export your public key once
export GPG_PUBLIC_KEY=$(cat ~/keys/public.asc)
# Backup with encryption - no GPG keyring mount needed!
docker run --rm \
-v $(pwd)/backups:/data \
-e GPG_PUBLIC_KEY \
user2k20/imapbackup \
-s imap.gmail.com -u myemail@gmail.com -e \
--s3-upload --s3-bucket=secure-backups \
--gpg-encrypt --gpg-recipient=backup@example.com \
--gpg-import-key=env:GPG_PUBLIC_KEY
# Create config.yaml
cat > config.yaml <<EOF
global:
basedir: ./backups
s3:
enabled: true
endpoint: https://s3.hetzner.cloud
bucket: email-backups
gpg:
enabled: true
recipient: backup@example.com
accounts:
- name: personal-gmail
server: imap.gmail.com
user: myemail@gmail.com
pass: env:GMAIL_PASSWORD
- name: work-office365
server: outlook.office365.com
user: user@company.com
pass: env:WORK_PASSWORD
EOF
# Backup all accounts - auto-detects config.yaml
python3 imapbackup.py
# Or explicitly specify config file
python3 imapbackup.py --config=config.yaml
Note: If config.yaml
or config.yml
exists in the current directory, it will be automatically used when running without arguments.
See Multi-Account Setup Guide for complete documentation.
-s HOST IMAP server hostname
-u USER Username/email address
-p PASS Password or @/path/to/file
-e Use SSL/TLS (recommended)
-d DIR Directory for mbox files
-a Append mode (default, incremental)
-y Overwrite mode (full backup)
-r Restore mode (upload to IMAP)
--config=FILE Load settings from YAML config file
Allows backing up multiple accounts
See config.example.yaml for format
Auto-detection: If no arguments are provided,
the script automatically looks for config.yaml
or config.yml in the current directory
-f FOLDERS Backup specific folders (comma-separated)
--exclude-folders Exclude specific folders
--s3-upload Enable S3 integration
--s3-endpoint=URL S3 endpoint URL
--s3-bucket=NAME Bucket name
--s3-access-key=KEY Access key
--s3-secret-key=KEY Secret key
--s3-prefix=PATH Optional path prefix
--gpg-encrypt Enable GPG encryption/decryption
--gpg-recipient=EMAIL GPG key ID or email
--gpg-import-key=SOURCE Import public key from:
- File: /path/to/key.asc
- URL: https://example.com/key.asc
- Env: env:GPG_PUBLIC_KEY
- Since 2007 - Over 15 years of active development
- Production Ready - Used by thousands worldwide
- Zero Dependencies - Pure Python, runs anywhere
- Incremental Backups - Fast subsequent runs
- S3 Compatible - Works with all major cloud providers
- GPG Encryption - Industry-standard security
- Docker First - Container-native design
- No Keyring Mounting π - Import GPG keys from anywhere
- Kubernetes Ready - Perfect for cloud deployments
- Environment Variables - 12-factor app compliant
- CI/CD Friendly - Easy automation
- Docker installed
- That's it! Everything else is included
- Python 3.6+ (tested with 3.8+)
- GPG (for encryption):
apt install gnupg
- Dependencies:
pip install -r requirements.txt
Backup multiple email accounts with a single YAML configuration file:
- Global Settings: Define common S3, GPG, and connection settings once
- Account-Specific Overrides: Customize per-account as needed
- Auto Directory Structure: Each account gets its own subdirectory
- Auto S3 Prefixes: Automatic prefix generation per account
- Flexible Passwords: Support for direct, file-based, and environment variables
See Multi-Account Setup Guide for complete documentation.
No more mounting GPG keyrings! Import public keys from:
- Files:
--gpg-import-key=/etc/public-key.asc
- URLs:
--gpg-import-key=https://example.com/key.asc
- Environment:
--gpg-import-key=env:GPG_PUBLIC_KEY
Perfect for Docker, Kubernetes, and CI/CD pipelines!
Upload backups to any S3-compatible storage:
- AWS S3
- Hetzner Storage Box
- MinIO (self-hosted)
- Wasabi, Backblaze B2, DigitalOcean Spaces
Migrate emails between servers:
python3 imapbackup.py -r -s new-server.com -u user@example.com -e
- π Documentation - Complete guides and examples
- π³ Docker Hub - Pre-built images
- π Issues - Report bugs
- π€ Contributing - How to contribute
β Gmail / Google Workspace β Microsoft 365 / Outlook.com β ProtonMail β Fastmail, Zoho, Yahoo β iCloud Mail β Self-hosted (Dovecot, Postfix, Zimbra) β Any IMAP-compatible server
β AWS S3 β Hetzner Storage Box β MinIO (self-hosted) β Wasabi Hot Cloud Storage β Backblaze B2 β DigitalOcean Spaces β Cloudflare R2
β Docker / Docker Compose β Kubernetes CronJobs β Python venv β Cron jobs β Systemd services β GitHub Actions / GitLab CI
docker run --rm -v $(pwd)/backups:/data \
user2k20/imapbackup \
-s imap.gmail.com \
-u myemail@gmail.com \
-e \
--s3-upload \
--s3-endpoint=https://s3.eu-central-1.hetzner.cloud \
--s3-bucket=gmail-backups \
--s3-access-key=$HETZNER_KEY \
--s3-secret-key=$HETZNER_SECRET \
--gpg-encrypt \
--gpg-recipient=backup@example.com \
--gpg-import-key=https://raw.githubusercontent.com/you/keys/main/public.asc
python3 imapbackup.py \
-s outlook.office365.com \
-u user@company.com \
-e \
--s3-upload \
--s3-endpoint=http://minio.local:9000 \
--s3-bucket=email-backups \
--s3-access-key=minioadmin \
--s3-secret-key=minioadmin
docker run --rm -v $(pwd)/backups:/data \
user2k20/imapbackup \
-s imap.example.com \
-u user@example.com \
-e \
-f "INBOX,Sent,Important,Projects"
Contributions are welcome! Please feel free to:
- π Report bugs via Issues
- π‘ Suggest features
- π§ Submit pull requests
- π Improve documentation
This project is licensed under the MIT License - see the LICENSE file for details.
Free to use, modify, and distribute. No warranties provided.
Originally developed around 2007 by Rui Carmo and actively maintained ever since.
Special thanks to all contributors who have helped make this tool better over the years.
- π Documentation: docs/README.md
- π Issues: GitHub Issues
- π¬ Discussions: GitHub Discussions
β Star this repository if you find it useful!
π’ Share with others who need email backup solutions!
Feature | This Tool | Gmail Takeout | Mail Clients |
---|---|---|---|
Incremental | β Yes | β No | |
Automated | β Yes | β Manual | |
S3 Upload | β Yes | β No | β No |
GPG Encryption | β Yes | β No | β No |
Restore | β Yes | β Yes | |
Docker | β Yes | β No | β No |
Standard Format | β mbox | ||
Open Source | β MIT | β No |
Made with β€οΈ by the open-source community
π Documentation β’ π³ Docker Hub β’ π Report Issue β’ β Star on GitHub