Skip to content

A Python script for incremental backups of IMAP mailboxes with AWS/Minio S3 Bucket support and GPG encryption

License

Notifications You must be signed in to change notification settings

chris2k20/imapbackup

Β 
Β 

Repository files navigation

πŸ“§ IMAP Backup & Restore

Language grade: Python Docker Pulls License: MIT

The complete open-source solution for backing up, restoring, and migrating IMAP email accounts.

Zero dependencies. Pure Python. Docker ready. S3 compatible. GPG encrypted.


πŸš€ Quick Start

Using Docker (Recommended)

# 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

Using Python

# 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

✨ Features

Core Features

  • πŸ“¦ 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

New Features ✨

  • πŸ“‹ 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

πŸ“š Documentation

Getting Started

Advanced

πŸ“– View Full Documentation


🎯 Common Use Cases

Daily Automated Backups

# 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

Email Server Migration

# 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

Kubernetes CronJob

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

Encrypted Cloud Backups (No Keyring Mounting!)

# 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

Multi-Account Backup πŸ†•

# 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.


πŸ› οΈ Command Line Options

Basic Options

-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

Backup/Restore Modes

-a                   Append mode (default, incremental)
-y                   Overwrite mode (full backup)
-r                   Restore mode (upload to IMAP)

Multi-Account Mode πŸ†•

--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

Folder Selection

-f FOLDERS           Backup specific folders (comma-separated)
--exclude-folders    Exclude specific folders

S3 Storage

--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 Encryption πŸ†•

--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

🌟 Why Choose This Tool?

Battle Tested

  • Since 2007 - Over 15 years of active development
  • Production Ready - Used by thousands worldwide
  • Zero Dependencies - Pure Python, runs anywhere

Feature Rich

  • Incremental Backups - Fast subsequent runs
  • S3 Compatible - Works with all major cloud providers
  • GPG Encryption - Industry-standard security
  • Docker First - Container-native design

Flexible & Modern

  • 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

🚦 Requirements

For Docker

  • Docker installed
  • That's it! Everything else is included

For Python

  • Python 3.6+ (tested with 3.8+)
  • GPG (for encryption): apt install gnupg
  • Dependencies: pip install -r requirements.txt

πŸ’‘ What's New

v2.0 Features πŸ†•

Multi-Account Configuration

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.

Flexible GPG Key Import

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!

S3 Integration

Upload backups to any S3-compatible storage:

  • AWS S3
  • Hetzner Storage Box
  • MinIO (self-hosted)
  • Wasabi, Backblaze B2, DigitalOcean Spaces

Restore Mode

Migrate emails between servers:

python3 imapbackup.py -r -s new-server.com -u user@example.com -e

πŸ”— Links


🌍 Supported Platforms

IMAP Servers

βœ… Gmail / Google Workspace βœ… Microsoft 365 / Outlook.com βœ… ProtonMail βœ… Fastmail, Zoho, Yahoo βœ… iCloud Mail βœ… Self-hosted (Dovecot, Postfix, Zimbra) βœ… Any IMAP-compatible server

S3 Storage

βœ… AWS S3 βœ… Hetzner Storage Box βœ… MinIO (self-hosted) βœ… Wasabi Hot Cloud Storage βœ… Backblaze B2 βœ… DigitalOcean Spaces βœ… Cloudflare R2

Deployment

βœ… Docker / Docker Compose βœ… Kubernetes CronJobs βœ… Python venv βœ… Cron jobs βœ… Systemd services βœ… GitHub Actions / GitLab CI


πŸŽ“ Examples

Backup Gmail to Hetzner S3 (Encrypted)

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

Backup Office 365 to MinIO (Local)

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

Backup Specific Folders Only

docker run --rm -v $(pwd)/backups:/data \
  user2k20/imapbackup \
  -s imap.example.com \
  -u user@example.com \
  -e \
  -f "INBOX,Sent,Important,Projects"

🀝 Contributing

Contributions are welcome! Please feel free to:

  • πŸ› Report bugs via Issues
  • πŸ’‘ Suggest features
  • πŸ”§ Submit pull requests
  • πŸ“– Improve documentation

πŸ“œ License

This project is licensed under the MIT License - see the LICENSE file for details.

Free to use, modify, and distribute. No warranties provided.


πŸ™ Acknowledgments

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.


πŸ“ž Support


⭐ Star this repository if you find it useful!

πŸ“’ Share with others who need email backup solutions!


πŸ“Š Quick Comparison

Feature This Tool Gmail Takeout Mail Clients
Incremental βœ… Yes ❌ No ⚠️ Sometimes
Automated βœ… Yes ❌ Manual ⚠️ Limited
S3 Upload βœ… Yes ❌ No ❌ No
GPG Encryption βœ… Yes ❌ No ❌ No
Restore βœ… Yes ⚠️ Import only βœ… Yes
Docker βœ… Yes ❌ No ❌ No
Standard Format βœ… mbox ⚠️ Proprietary ⚠️ Varies
Open Source βœ… MIT ❌ No ⚠️ Varies

Made with ❀️ by the open-source community

πŸ“– Documentation β€’ 🐳 Docker Hub β€’ πŸ› Report Issue β€’ ⭐ Star on GitHub

About

A Python script for incremental backups of IMAP mailboxes with AWS/Minio S3 Bucket support and GPG encryption

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 98.4%
  • Dockerfile 1.1%
  • Ruby 0.5%